Class DynamicLocaleUtils
java.lang.Object
com.pranavpandey.android.dynamic.locale.DynamicLocaleUtils
Helper class to perform various locale operations.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic Locale
getCurrentLocale
(Context context) Returns the current locale for the supplied context.static Locale
getDefaultLocale
(Context context, String[] supportedLocales) Get the default locale language from the supported locales.static int
getLayoutDirection
(Context context) Returns the layout direction for the selected locale.static Locale
Returns the locale after performing safety checks.static LocaleManager
getLocaleManager
(Context context) Returns the locale manager on API 33.static boolean
Checks whether the layout direction is RTL (right-to-left).static boolean
isLayoutRtl
(Context context) Checks whether the layout direction is RTL (right-to-left).static Context
Set the locale for a given context.static Context
Set the locale for a given context.static Context
Set the locale for a given context.static Context
Set the locale for a given context.static Configuration
setLocale
(Configuration config, Locale locale, float fontScale) Set the locale for a given configuration.static Locale
Get 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:
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
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
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 ofActivity
.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 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
-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
Returns the current locale for the supplied context.- Parameters:
context
- The context to get the resources.- Returns:
- The current locale for the supplied context.
-