Package ninja.i18n
Interface Lang
-
- All Known Implementing Classes:
LangImpl
public interface Lang
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description void
clearLanguage(Result result)
Clears the current language.Optional<String>
getLanguage(Context context, Optional<Result> result)
Retrieve the current language or null if not set.Locale
getLocaleFromStringOrDefault(Optional<String> language)
application.conf usually contains the following: application.languages=en,de This little helper converts a language code like (en, en-US etc) to a Java locale.boolean
isLanguageDirectlySupportedByThisApplication(String language)
application.conf usually contains the following: application.languages=en,de This little helper checks if the language is supported.Result
setLanguage(String locale, Result result)
Force a language in Ninja framwork.
-
-
-
Method Detail
-
getLanguage
Optional<String> getLanguage(Context context, Optional<Result> result)
Retrieve the current language or null if not set. It will try to determine the language by: 1) Checking if result contains a forced language 2) Checking if context has a NINJA_LANG cookie with a forced language 3) Getting the first language from the Accept-Language header- Returns:
- The current language (fr, ja, it ...) - may be absent
-
setLanguage
Result setLanguage(String locale, Result result)
Force a language in Ninja framwork. This is usually done by a cookie NINJA_LANG. This overrides any Accept-Language languages.- Parameters:
locale
- (fr, ja, it ...)
-
clearLanguage
void clearLanguage(Result result)
Clears the current language. This will trigger resolving language from request (Accept lang) if not manually set. Note: The language is set by a cookie. To delete a cookie the max-age is set to 0. It can therefore be the case that the lang cookie still exists in the thread. Make sure your module / app handles this properly.- Parameters:
Result
- result clear language commands merged into result.
-
isLanguageDirectlySupportedByThisApplication
boolean isLanguageDirectlySupportedByThisApplication(String language)
application.conf usually contains the following: application.languages=en,de This little helper checks if the language is supported.- Parameters:
language
- The language to check (en, en-US etc)- Returns:
- true if supported directly, false if not
-
getLocaleFromStringOrDefault
Locale getLocaleFromStringOrDefault(Optional<String> language)
application.conf usually contains the following: application.languages=en,de This little helper converts a language code like (en, en-US etc) to a Java locale. It takes null as input and falls back to the default language. By convention this is the first language of application.languages. In the case of application.languages=en,de The default language is "en".- Parameters:
language
- The language to check (en, en-US etc)- Returns:
- The Java locale or a default locale based on the first language in your application.languages configuration.
-
-