aemsling-models

How to get LanguageManager class in SlingModels?


I am declaring LanguageManager class but not getting how to instantiate it.

 ResourceResolver res = resource.getResourceResolver();
   **LanguageManager languagemanager= null;**

    final PageManager pageManager = resource.getResourceResolver().adaptTo(
            PageManager.class);
    final Page currentPage = pageManager.getContainingPage(resource);
    String currentPagePath = currentPage.getPath();
    SlingBindings bindings = (SlingBindings) request
            .getAttribute(SlingBindings.class.getName());

    for (final Page page : languagemanager.getLanguageRoots(res,
            currentPagePath)) {
        languagePages.put(page.getLanguage(false), page);
    }

Solution

  • There is an OSGi service (com.day.cq.wcm.core.impl.LanguageManagerImpl) that implements the LanguageManager interface, so you should be able to just reference it with the @Inject annotation from within your Sling model.

    @Inject    
    private final LanguageManager languageManager;