Class DynamicLocaleUtils

java.lang.Object
com.pranavpandey.android.dynamic.locale.DynamicLocaleUtils

public class DynamicLocaleUtils extends Object
Helper class to perform various locale operations.
  • Constructor Details

    • DynamicLocaleUtils

      public DynamicLocaleUtils()
  • Method Details

    • getLocaleManager

      @Nullable public static LocaleManager getLocaleManager(@Nullable Context context)
      Returns the locale manager on API 33.
      Parameters:
      context - The context to be used.
      Returns:
      The locale manager on API 33.
    • getLayoutDirection

      public static int getLayoutDirection(@Nullable Context context)
      Returns the layout direction for the selected locale.
      Parameters:
      context - The context to be used.
      Returns:
      The layout direction for the selected locale.
      See Also:
    • isLayoutRtl

      public static boolean isLayoutRtl(@Nullable Context context)
      Checks whether the layout direction is RTL (right-to-left).
      Parameters:
      context - The context to be used.
      Returns:
      true if the layout direction is RTL (right-to-left).
      See Also:
    • isLayoutRtl

      public static boolean isLayoutRtl()
      Checks whether the layout direction is RTL (right-to-left).
      Returns:
      true if the layout direction is RTL (right-to-left).
      See Also:
    • toLocale

      @Nullable public static Locale toLocale(@Nullable String locale)
      Get locale from the locale string in the format: language,region.
      Parameters:
      locale - The locale string to be converted.
      Returns:
      The converted locale from the locale string.

      Return null for the default locale value.

      See Also:
    • getDefaultLocale

      @NonNull public static Locale getDefaultLocale(@Nullable Context context, @Nullable String[] supportedLocales)
      Get the default locale language from the supported locales.
      Parameters:
      context - The context to get the configuration.
      supportedLocales - The supported locales.
      Returns:
      The default locale according to the supported locales.
    • getLocale

      @NonNull public static Locale getLocale(@Nullable Locale locale, @NonNull Locale defaultLocale)
      Returns the locale after performing safety checks.
      Parameters:
      locale - The locale to be checked.
      defaultLocale - The default locale if current locale does not passes checks.
      Returns:
      The locale after performing safety checks.
    • setLocale

      @NonNull public static Context setLocale(@NonNull Context context, boolean activity, @Nullable Locale locale, float fontScale, boolean system)
      Set the locale for a given context.
      Parameters:
      context - The context to set the locale.
      activity - true if the context an instance of Activity.
      locale - The locale to be used for the context resources.
      fontScale - The font scale to be used for the context resources.
      system - true to set application locales on API 33 and above.
      Returns:
      The modified context after applying the locale.
      See Also:
    • setLocale

      @NonNull public static Context setLocale(@NonNull Context context, boolean activity, @Nullable Locale locale, float fontScale)
      Set the locale for a given context.
      Parameters:
      context - The context to set the locale.
      activity - true if the context an instance of Activity.
      locale - The locale to be used for the context resources.
      fontScale - The font scale to be used for the context resources.
      Returns:
      The modified context after applying the locale.
      See Also:
    • setLocale

      @NonNull public static Context setLocale(@NonNull Context context, @Nullable Locale locale, float fontScale, boolean system)
      Set the locale for a given context.
      Parameters:
      context - The context to set the locale.
      locale - The locale to be used for the context resources.
      fontScale - The font scale to be used for the context resources.
      system - true to set application locales on API 33 and above.
      Returns:
      The modified context after applying the locale.
      See Also:
    • setLocale

      @NonNull public static Context setLocale(@NonNull Context context, @Nullable Locale locale, float fontScale)
      Set the locale for a given context.
      Parameters:
      context - The context to set the locale.
      locale - The locale to be used for the context resources.
      fontScale - The font scale to be used for the context resources.
      Returns:
      The modified context after applying the locale.
      See Also:
    • setLocale

      @NonNull public static Configuration setLocale(@NonNull Configuration config, @Nullable Locale locale, float fontScale)
      Set the locale for a given configuration.
      Parameters:
      config - The config to set the locale.
      locale - The locale to be used for the context resources.
      fontScale - The font scale to be used for the context resources.
      Returns:
      The modified context after applying the locale.
    • getCurrentLocale

      @NonNull public static Locale getCurrentLocale(@Nullable Context context)
      Returns the current locale for the supplied context.
      Parameters:
      context - The context to get the resources.
      Returns:
      The current locale for the supplied context.