Search in sources :

Example 1 with I18nModule

use of org.olat.core.util.i18n.I18nModule in project OpenOLAT by OpenOLAT.

the class VelocityRenderDecorator method getLanguages.

public Languages getLanguages() {
    I18nManager i18nMgr = CoreSpringFactory.getImpl(I18nManager.class);
    I18nModule i18nModule = CoreSpringFactory.getImpl(I18nModule.class);
    Collection<String> enabledKeysSet = i18nModule.getEnabledLanguageKeys();
    Map<String, String> langNames = new HashMap<String, String>();
    Map<String, String> langTranslators = new HashMap<String, String>();
    String[] enabledKeys = ArrayHelper.toArray(enabledKeysSet);
    String[] names = new String[enabledKeys.length];
    for (int i = 0; i < enabledKeys.length; i++) {
        String key = enabledKeys[i];
        String langName = i18nMgr.getLanguageInEnglish(key, i18nModule.isOverlayEnabled());
        langNames.put(key, langName);
        names[i] = langName;
        String author = i18nMgr.getLanguageAuthor(key);
        langTranslators.put(key, author);
    }
    ArrayHelper.sort(enabledKeys, names, true, true, true);
    return new Languages(enabledKeys, langNames, langTranslators);
}
Also used : I18nModule(org.olat.core.util.i18n.I18nModule) HashMap(java.util.HashMap) I18nManager(org.olat.core.util.i18n.I18nManager)

Example 2 with I18nModule

use of org.olat.core.util.i18n.I18nModule in project OpenOLAT by OpenOLAT.

the class LocaleNegotiator method getNegotiatedLocale.

/**
 * Try first to find a locale which match language, country and variant, then
 * match language and country and the final ty it with language only
 */
public static Locale getNegotiatedLocale(Locale loc) {
    I18nModule i18nModule = CoreSpringFactory.getImpl(I18nModule.class);
    Map<String, Locale> allLocales = i18nModule.getAllLocales();
    Collection<String> enabledLanguageKeys = i18nModule.getEnabledLanguageKeys();
    String lang = loc.getLanguage();
    // search a direct match first de_CH_bs...
    for (String enabledLanguageKey : enabledLanguageKeys) {
        if (lang.startsWith(enabledLanguageKey)) {
            Locale locale = allLocales.get(enabledLanguageKey);
            if (locale.equals(loc)) {
                return locale;
            }
        }
    }
    // search a match language + country
    for (String enabledLanguageKey : enabledLanguageKeys) {
        Locale locale = allLocales.get(enabledLanguageKey);
        if (locale.getLanguage().equals(loc.getLanguage()) && locale.getCountry().equals(loc.getCountry())) {
            return locale;
        }
    }
    // search a match language
    for (String enabledLanguageKey : enabledLanguageKeys) {
        Locale locale = allLocales.get(enabledLanguageKey);
        if (locale.getLanguage().equals(loc.getLanguage())) {
            return locale;
        }
    }
    return null;
}
Also used : I18nModule(org.olat.core.util.i18n.I18nModule) Locale(java.util.Locale)

Example 3 with I18nModule

use of org.olat.core.util.i18n.I18nModule in project openolat by klemens.

the class LocaleNegotiator method getNegotiatedLocale.

/**
 * Try first to find a locale which match language, country and variant, then
 * match language and country and the final ty it with language only
 */
public static Locale getNegotiatedLocale(Locale loc) {
    I18nModule i18nModule = CoreSpringFactory.getImpl(I18nModule.class);
    Map<String, Locale> allLocales = i18nModule.getAllLocales();
    Collection<String> enabledLanguageKeys = i18nModule.getEnabledLanguageKeys();
    String lang = loc.getLanguage();
    // search a direct match first de_CH_bs...
    for (String enabledLanguageKey : enabledLanguageKeys) {
        if (lang.startsWith(enabledLanguageKey)) {
            Locale locale = allLocales.get(enabledLanguageKey);
            if (locale.equals(loc)) {
                return locale;
            }
        }
    }
    // search a match language + country
    for (String enabledLanguageKey : enabledLanguageKeys) {
        Locale locale = allLocales.get(enabledLanguageKey);
        if (locale.getLanguage().equals(loc.getLanguage()) && locale.getCountry().equals(loc.getCountry())) {
            return locale;
        }
    }
    // search a match language
    for (String enabledLanguageKey : enabledLanguageKeys) {
        Locale locale = allLocales.get(enabledLanguageKey);
        if (locale.getLanguage().equals(loc.getLanguage())) {
            return locale;
        }
    }
    return null;
}
Also used : I18nModule(org.olat.core.util.i18n.I18nModule) Locale(java.util.Locale)

Example 4 with I18nModule

use of org.olat.core.util.i18n.I18nModule in project openolat by klemens.

the class LocalizedXSLTransformer method getInstance.

/**
 * Get a localized transformer instance.
 *
 * @param locale The locale for this transformer instance
 * @return A localized transformer
 */
// cluster_ok only in VM
public static LocalizedXSLTransformer getInstance(Locale locale) {
    I18nModule i18nModule = CoreSpringFactory.getImpl(I18nModule.class);
    LocalizedXSLTransformer instance = instanceHash.get(i18nModule.getLocaleKey(locale));
    if (instance == null) {
        Translator trans = Util.createPackageTranslator(QTI12ResultDetailsController.class, locale);
        LocalizedXSLTransformer newInstance = new LocalizedXSLTransformer(trans);
        // see javadoc of ConcurrentHashMap
        instance = instanceHash.putIfAbsent(i18nModule.getLocaleKey(locale), newInstance);
        if (instance == null) {
            // newInstance was put into the map
            instance = newInstance;
        }
    }
    return instance;
}
Also used : I18nModule(org.olat.core.util.i18n.I18nModule) Translator(org.olat.core.gui.translator.Translator)

Example 5 with I18nModule

use of org.olat.core.util.i18n.I18nModule in project OpenOLAT by OpenOLAT.

the class I18nWebService method getTranslation.

/**
 * Return the translation of the key. If the "locale" parameter is not specified, the method
 * try to use the "locale" of the user and if it hasn't, take the default locale.
 * @response.representation.200.mediaType text/plain
 * @response.representation.200.doc The translation of the package + key
 * @response.representation.200.example OK
 * @param packageName The name of the package
 * @param key The key to translate
 * @param localeKey The locale (optional)
 * @param request The HTTP request (optional)
 * @return
 */
@GET
@Path("{package}/{key}")
@Produces(MediaType.TEXT_PLAIN)
public Response getTranslation(@PathParam("package") String packageName, @PathParam("key") String key, @QueryParam("locale") String localeKey, @Context HttpServletRequest request) {
    I18nManager i18n = CoreSpringFactory.getImpl(I18nManager.class);
    I18nModule i18nModule = CoreSpringFactory.getImpl(I18nModule.class);
    Locale locale = null;
    if (StringHelper.containsNonWhitespace(localeKey)) {
        locale = i18n.getLocaleOrDefault(localeKey);
    } else {
        UserRequest ureq = RestSecurityHelper.getUserRequest(request);
        if (ureq != null && ureq.getLocale() != null) {
            locale = ureq.getLocale();
        }
    }
    if (locale == null) {
        locale = i18nModule.getDefaultLocale();
    }
    boolean overlayEnabled = i18nModule.isOverlayEnabled();
    String val = i18n.getLocalizedString(packageName, key, EMPTY_ARRAY, locale, overlayEnabled, true);
    return Response.ok(val).build();
}
Also used : I18nModule(org.olat.core.util.i18n.I18nModule) Locale(java.util.Locale) UserRequest(org.olat.core.gui.UserRequest) I18nManager(org.olat.core.util.i18n.I18nManager) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Aggregations

I18nModule (org.olat.core.util.i18n.I18nModule)8 Locale (java.util.Locale)4 I18nManager (org.olat.core.util.i18n.I18nManager)4 HashMap (java.util.HashMap)2 GET (javax.ws.rs.GET)2 Path (javax.ws.rs.Path)2 Produces (javax.ws.rs.Produces)2 UserRequest (org.olat.core.gui.UserRequest)2 Translator (org.olat.core.gui.translator.Translator)2