Coder Social home page Coder Social logo

franmontiel / localechanger Goto Github PK

View Code? Open in Web Editor NEW
116.0 116.0 23.0 287 KB

An Android library to programmatically set the Locale of an app and persist the configuration.

License: Apache License 2.0

Java 100.00%
android android-library i18n language locale localization

localechanger's People

Contributors

franmontiel avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

localechanger's Issues

Activity does not restart if redeclared as singleTop in Manifest

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);
        }
    }

How to save selected language in LocalePreference

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.

Activity does not automatically re-create

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.

image

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();
        }
    }
}

NightMode not working anymore since LocaleChangerAppCompatDelegate inroduction in v1.1

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).

Layout Direction not changing

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.

Change Language with Application-Level Context

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?

Allow to use custom LocalePersistor

There are two problems with current LocalePersistor:

  1. 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.

  2. 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.

java.lang.NullPointerException

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 ....

Change language without restarting activity

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.

UnsupportedLocaleException

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.

Val cannot be reassigned in kotlin

override fun attachBaseContext(newBase: Context?) {
newBase = LocaleChanger.configureBaseContext(newBase)
super.attachBaseContext(newBase)
}
}

newBase : Val cannot be reassigned in kotlin

not working on pre nougat devices

As you can see below image ,
I have changed the language to Arabic, the date has been changed, but only english resources has been loaded

Untitled

Locale β€œar” language change not working with Huawei Devices but working with others

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.

Delegate is null

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)

ActionBar title is not shown in the correct language

When the title is defined in the label attribute in the Manifest a Locale change does not affect to the ActionBar title:

  • In versions pre-Nougat the problem only appears when the Locale is changed but after a configuration change or app restart it will show the correct title.
  • From Nougat the problem appears always and the title of the ActionBar is never affected by the Locale change.

To workaround this issue the title can be set programmatically.

Feedback BUG

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.