Package ninja.i18n
Class LangImpl
- java.lang.Object
-
- ninja.i18n.LangImpl
-
-
Constructor Summary
Constructors Constructor Description LangImpl(NinjaProperties ninjaProperties)
-
Method Summary
All Methods Instance Methods Concrete 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.
-
-
-
Constructor Detail
-
LangImpl
@Inject public LangImpl(NinjaProperties ninjaProperties)
-
-
Method Detail
-
clearLanguage
public void clearLanguage(Result result)
Description copied from interface:Lang
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.- Specified by:
clearLanguage
in interfaceLang
-
setLanguage
public Result setLanguage(String locale, Result result)
Description copied from interface:Lang
Force a language in Ninja framwork. This is usually done by a cookie NINJA_LANG. This overrides any Accept-Language languages.- Specified by:
setLanguage
in interfaceLang
- Parameters:
locale
- (fr, ja, it ...)
-
getLanguage
public Optional<String> getLanguage(Context context, Optional<Result> result)
Description copied from interface:Lang
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- Specified by:
getLanguage
in interfaceLang
- Returns:
- The current language (fr, ja, it ...) - may be absent
-
isLanguageDirectlySupportedByThisApplication
public boolean isLanguageDirectlySupportedByThisApplication(String language)
Description copied from interface:Lang
application.conf usually contains the following: application.languages=en,de This little helper checks if the language is supported.- Specified by:
isLanguageDirectlySupportedByThisApplication
in interfaceLang
- Parameters:
language
- The language to check (en, en-US etc)- Returns:
- true if supported directly, false if not
-
getLocaleFromStringOrDefault
public Locale getLocaleFromStringOrDefault(Optional<String> language)
Description copied from interface:Lang
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".- Specified by:
getLocaleFromStringOrDefault
in interfaceLang
- 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.
-
-