Class DynamicLocaleUtils
java.lang.Object
com.pranavpandey.android.dynamic.locale.DynamicLocaleUtils
Helper class to perform various locale operations.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic LocalegetCurrentLocale(Context context) Returns the current locale for the supplied context.static LocalegetDefaultLocale(Context context, String[] supportedLocales) Get the default locale language from the supported locales.static intgetLayoutDirection(Context context) Returns the layout direction for the selected locale.static LocaleReturns the locale after performing safety checks.static LocaleManagergetLocaleManager(Context context) Returns the locale manager on API 33.static booleanChecks whether the layout direction is RTL (right-to-left).static booleanisLayoutRtl(Context context) Checks whether the layout direction is RTL (right-to-left).static ContextSet the locale for a given context.static ContextSet the locale for a given context.static ContextSet the locale for a given context.static ContextSet the locale for a given context.static ConfigurationsetLocale(Configuration config, Locale locale, float fontScale) Set the locale for a given configuration.static LocaleGet locale from the locale string in the format:language,region.
-
Constructor Details
-
DynamicLocaleUtils
public DynamicLocaleUtils()
-
-
Method Details
-
getLocaleManager
Returns the locale manager on API 33.- Parameters:
context- The context to be used.- Returns:
- The locale manager on API 33.
-
getLayoutDirection
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
Checks whether the layout direction is RTL (right-to-left).- Parameters:
context- The context to be used.- Returns:
trueif 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:
trueif the layout direction is RTL (right-to-left).- See Also:
-
toLocale
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
nullfor 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
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-trueif the context an instance ofActivity.locale- The locale to be used for the context resources.fontScale- The font scale to be used for the context resources.system-trueto 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-trueif the context an instance ofActivity.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-trueto 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
Returns the current locale for the supplied context.- Parameters:
context- The context to get the resources.- Returns:
- The current locale for the supplied context.
-