如何在 SlingModels 中获取 LanguageManager class?

How to get LanguageManager class in SlingModels?

我正在声明 LanguageManager class 但不知道如何实例化它。

 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);
    }

有一个 OSGi 服务 (com.day.cq.wcm.core.impl.LanguageManagerImpl) 实现了 LanguageManager 接口,因此您应该能够在 Sling 模型中使用 @Inject 注释引用它。

@Inject    
private final LanguageManager languageManager;