Class LocaleState

java.lang.Object
com.transifex.txnative.LocaleState

public class LocaleState extends Object
Keeps track of the locale-related information for the application, such as supported locales, source, current and resolved locale.
  • Constructor Details

    • LocaleState

      public LocaleState(@NonNull Context applicationContext, @Nullable String sourceLocale, @Nullable String[] appLocales, @Nullable Locale currentLocale)
      Creates a LocaleState instance.
      Parameters:
      applicationContext - The application context.
      sourceLocale - The locale of the source language, defaults to "en" if null is provided.
      appLocales - A list of all locales supported by the application, including the source locale; defaults to [sourceLocale] if null is provided.
      currentLocale - Set to null to use the system's locale, or set to a specific locale if your app uses its own locale.
      See Also:
  • Method Details

    • getSourceLocale

      @NonNull public String getSourceLocale()
      The source locale.

      This is the locale that is used as the source when uploading the files to Transifex.

      See Also:
    • getAppLocales

      @NonNull public String[] getAppLocales()
      The app's supported locales, including the source locale.
      See Also:
    • getTranslatedLocales

      @NonNull public String[] getTranslatedLocales()
      The app's supported locales without the source locale.

      The array can be empty.

    • getCurrentLocale

      @NonNull public Locale getCurrentLocale()
      The current locale as provided by Android or the the locale set using setCurrentLocale(Locale).
    • getResolvedLocale

      @Nullable public String getResolvedLocale()
      The resolved locale is one of the app locales that best matches the current locale.

      The matching follows Android's resource resolution strategy. At first, a locale that has the same language and region is searched for. If this fails, a locale that has the same language but no region defined is searched for. If this fails, a locale that has the same language but a different region is searched for. If this fails, the resolved locale is null.

      Returns:
      The resolved locale or null if no app locale matches the current locale.
    • setCurrentLocale

      public void setCurrentLocale(@Nullable Locale currentLocale)
      Set the locale used in the app.

      If set to null, the SDK will automatically use the one selected at Android's settings when the app starts and will update to another locale if it's changed by the user while the app runs.

      If your app has its own implementation for selecting locale, you should call this method when initializing the SDK and when the locale changes.

      Warning: if you set a custom locale, your app should also call the necessary Android methods for updating the locale there as well. If not, the SDK may not work correctly in plurals and some other cases.

      Parameters:
      currentLocale - Set to null to use the system's locale or to a specific locale, if your app uses its own locale.
    • isSourceLocale

      public boolean isSourceLocale()
      Returns true if the source locale matches the resolved locale. If it's different or the resolved locale is null, it returns false.