franmontiel / localechanger Goto Github PK
View Code? Open in Web Editor NEWAn Android library to programmatically set the Locale of an app and persist the configuration.
License: Apache License 2.0
An Android library to programmatically set the Locale of an app and persist the configuration.
License: Apache License 2.0
Both ActivityRecreationHelper
and auto activity restart via onResume
fails to recreate activity if launchMode is declared as SingleTop
android:launchMode="singleTop"
This is because we are starting activity while the old one is still ongoing.
A simple fix would be finishing activity first before starting the new one.
ex
in ActivityRecreationHelper
public static void recreate(Activity activity, boolean animate) {
Intent restartIntent = new Intent(activity, activity.getClass());
Bundle extras = activity.getIntent().getExtras();
if (extras != null) {
restartIntent.putExtras(extras);
}
activity.finish();
if (animate) {
ActivityCompat.startActivity(
activity,
restartIntent,
ActivityOptionsCompat
.makeCustomAnimation(activity, android.R.anim.fade_in, android.R.anim.fade_out)
.toBundle()
);
} else {
activity.startActivity(restartIntent);
activity.overridePendingTransition(0, 0);
}
}
When I select language from spinner save in configuration and close the app and now open again this app, So spinner is again initializing but I want saved language. any help from your side appreciable.
When the system language changes, I use the following code to initialize, But when I return to the previous Activity, Activity does not automatically re-create, Causes the language not to be fully updated. This is because "previousLocale" is empty, So this also leads to another problem. When I change the language of the system and then return to the application, the application does not automatically recreate the activity, When you return to the app for the second time, the activity is re-created.
The second time back to the APP language will be changed is because: When the language changes and back to the application, then first call "onResume" but now the system language configuration is not updatedLocale.setDefault(newLocale);
, so the activity is not reset, , then call "SampleApplication"-> "onConfigurationChanged", then The system language config was updated, So when you first return to the app, the language will not be updated. When you return to the app for the second time, the activity will be recreated and the language will be updated.
public class LocaleChangeReceiver extends BroadcastReceiver {
public final static String INTENT = "android.intent.action.LOCALE_CHANGED";
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(INTENT)) {
LocaleChanger.resetLocale();
}
}
}
I am using AppCompatDelegate.setDefaultNightMode(b ? AppCompatDelegate.MODE_NIGHT_YES : AppCompatDelegate.MODE_NIGHT_NO);
to toggle between dark and normal mode in my app's settings. It seems the new LocaleChangerAppCompatDelegate
is causing this functionality to malfunction (the app stays in weird state and colors are not correct).
If I remove this from MainActivity it works well:
private LocaleChangerAppCompatDelegate localeChangerAppCompatDelegate;
@NonNull
@Override
public AppCompatDelegate getDelegate() {
if (localeChangerAppCompatDelegate == null) {
localeChangerAppCompatDelegate = new LocaleChangerAppCompatDelegate(super.getDelegate());
}
return localeChangerAppCompatDelegate;
}
I am using LocaleChanger 1.1 and AppCompat 1.2.0 (latest).
Tested on Android 11.
Related issue zeugma-solutions/locale-helper-android#15 and akexorcist/Localization#64
I implemented 2 languages English and Arabic and in first screen 2 button for switch now here UI direction not change on same screen but in next screen it's working.
This library will not work if I use the application context to get the string.
I'm planning to call LocaleChanger.configureBaseContext()
directly from my Application
since I think it can be used globally through my application without passing any context from activity.
From the documentation of the function,
/**
* This method should be used inside the Activity attachBaseContext.
* The returned Context should be used as argument for the super method call.
*
* @param context
* @return the resulting context that should be provided to the super method call.
*/
public static Context configureBaseContext(Context context) {
return delegate.configureBaseContext(context);
}
It stated that This method should be used inside the Activity attachBaseContext
and not mentiong about application context.
My question is it safe that I created helper function from my application
// inside Application class
public Context getContextForLanguage() {
return LocaleChanger.configureBaseContext(this);
}
and use that helper funtion to get the string from somewhere else? (Like below)
(application as MyApplication).getContextForLanguage().getString(R.string.something)
What are the drawbacks of this approach?
There are two problems with current LocalePersistor:
At the moment LocalePreference uses LocalePersistor.class.getPackage().getName() as SharedPreference name. The problem is that if you use Proguard this class name can be different. So in the next app update there is a high probability that desired shared preference name will not exists.
Another problem is that there is no way to specify your own storage (e.g. I do not want to have any storage in LocalChanger library, I have my own storage in the app).
It would be really useful if it would be possible to use custom LocalePersistor.
Hi, i have issue with Dual Display. i create Dual Display using Presentation https://developer.android.com/reference/android/app/Presentation. Every i make Dual Display, it will always dismissed since LocaleChanger wrape baseContext.
I got this message when my Dual Display has been dimissed
Presentation is being dismissed because the display metrics have changed since it was created.
Are you guys can help my issue?
is there a way to manually set a string using the appropriate locale?
Example if i changed my app language to Spanish get spanish string and manually add for not stetted views.
java.lang.RuntimeException:
at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2981)
at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:3042)
at android.app.ActivityThread.-wrap14 (ActivityThread.java)
at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1639)
at android.os.Handler.dispatchMessage (Handler.java:102)
at android.os.Looper.loop (Looper.java:154)
at android.app.ActivityThread.main (ActivityThread.java:6780)
at java.lang.reflect.Method.invoke (Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:1496)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1386)
Caused by: java.lang.NullPointerException:
at com.franmontiel.localechanger.LocaleChanger.configureBaseContext (LocaleChanger.java:119)
at com.eakteam.networkmanager.paneli_kryesor.attachBaseContext (paneli_kryesor.java:201)
at android.app.Activity.attach (Activity.java:6884)
at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2894)
at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:3042)
at android.app.ActivityThread.-wrap14 (ActivityThread.java)
at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1639)
at android.os.Handler.dispatchMessage (Handler.java:102)
at android.os.Looper.loop (Looper.java:154)
at android.app.ActivityThread.main (ActivityThread.java:6780)
at java.lang.reflect.Method.invoke (Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:1496)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1386)
MAIN APP
public class MainApplication extends Application {
public static final List<Locale> SUPPORTED_LOCALES =
Arrays.asList(
new Locale("en", "US"),
new Locale("sq", "AL")
);
@Override
public void onCreate() {
super.onCreate();
LocaleChanger.initialize(getApplicationContext(), SUPPORTED_LOCALES);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
LocaleChanger.onConfigurationChanged();
}
}
MAIN ACTIVITY
@Override
protected void attachBaseContext(Context newBase) {
newBase = LocaleChanger.configureBaseContext(newBase);
super.attachBaseContext(newBase);
}
I'm using Crashlytics from Fabric too if this is needed to know ....
I have a method that translates my application from English to Swedish and back again if the user so wishes. However, I don't really like the fact that the activity restarts every time because it's giving me a hard time with the savedInstaceState and I've had several crashes because of this.
Hi,
I am getting UnsupportedLocaleException on some phones:
java.lang.IllegalArgumentException:
at com.franmontiel.localechanger.LocaleChangerDelegate.setLocale (LocaleChangerDelegate.java:77)
at com.franmontiel.localechanger.LocaleChanger.setLocale (LocaleChanger.java:99)
at com.crypter.cryptocyrrency.ui.FragmentSettings.lambda$onViewCreated$3$FragmentSettings (FragmentSettings.java:213)
at com.crypter.cryptocyrrency.ui.FragmentSettings$$Lambda$3.onCheckedChanged (Unknown Source)
at android.widget.CompoundButton.setChecked (CompoundButton.java:157)
at android.widget.Switch.setChecked (Switch.java:1079)
at android.widget.Switch.toggle (Switch.java:1074)
at android.widget.CompoundButton.performClick (CompoundButton.java:121)
at android.view.View$PerformClick.run (View.java:22473)
at android.os.Handler.handleCallback (Handler.java:761)
at android.os.Handler.dispatchMessage (Handler.java:98)
at android.os.Looper.loop (Looper.java:156)
at android.app.ActivityThread.main (ActivityThread.java:6523)
at java.lang.reflect.Method.invoke (Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:942)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:832)
Caused by: com.franmontiel.localechanger.UnsupportedLocaleException:
at com.franmontiel.localechanger.LocaleResolver.resolve (LocaleResolver.java:57)
at com.franmontiel.localechanger.LocaleChangerDelegate.setLocale (LocaleChangerDelegate.java:70)
I initialize the library like this:
List<Locale> supported_locales = new ArrayList<>();
supported_locales.add(new Locale("en"));
supported_locales.add(new Locale("de"));
supported_locales.add(new Locale("es"));
LocaleChanger.initialize(getApplicationContext(), supported_locales);
App is translated into German and Spanish. It seems the exception occurs when user tries to set the language to english.
override fun attachBaseContext(newBase: Context?) {
newBase = LocaleChanger.configureBaseContext(newBase)
super.attachBaseContext(newBase)
}
}
newBase : Val cannot be reassigned in kotlin
Android 4.0 Error
I'm setting android:supportsRtl="true"
in manifest and calling setLocale("ar")
;
Locale locale = new Locale(lang);
LocaleChanger.setLocale(locale);
Intent refresh = new Intent(getContext(), MainActivity.class);
getActivity().finish();
refresh.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(refresh);
It's working perfectly with my samsung devices but not with Huawei devices . It's switching sides and the locale.default is giving me "ar" as language but for some reason it doesn't seem to detect the ar string.xml
Any idea why this is happening and a possible fix?
Edit:
I must add that in debug type it works with the Huawei devices but not in release or production type.
I'm getting this exception on orientation changes:
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.franmontiel.localechanger.LocaleChangerDelegate.onConfigurationChanged()' on a null object reference com.franmontiel.localechanger.LocaleChanger.onConfigurationChanged(LocaleChanger.java:126)
my.app.App.onConfigurationChanged(App.java:86)
android.app.ActivityThread.performConfigurationChanged(ActivityThread.java:4909)
When the title is defined in the label attribute in the Manifest a Locale change does not affect to the ActionBar title:
To workaround this issue the title can be set programmatically.
In Android 7.0+ devices in the use of this library to switch multi-language, some text switch to the correct language, but some text switch to the current system language, Some did not switch to the correct language
could u please support AndroidX for the library .. thanks
A declarative, efficient, and flexible JavaScript library for building user interfaces.
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. πππ
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google β€οΈ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.