Changing of Application Locale does not take full effect in android? - android

I am making an application in two languages English and in Indonesian.
For that i have created two strings file in res folder (values-en and values-in)
In one of my activity I select lanuage and change the locale.I am doing this using following code-
passing language id to this method-
public void setLocale(String lang) {
Locale myLocale = new Locale(lang);
Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = myLocale;
res.updateConfiguration(conf, dm);
}
But after changing the language to indonesian strange thing happened. My Some of the text changed to indonesian and some changed to english.
for example- I have two Buttons (Submit and Upload Receipt) in activity.
SUBMIT btn has-
in english it's text is SUBMIT and in indonesian it is KIRIM
<string name="submit_btn_text">Submit</string> for english
<string name="submit_btn_text">Kirim</string> for indonesian
and UPLOAD RECEIPT btn has-
in english it's text is Upload Receipt and in indonesian it is Upload Penerimaan
<string name="upload_receipt">Upload Receipt</string> for english
<string name="upload_receipt">Upload Penerimaan</string> for indonesian
But in indonesian
Submit btn text chanes to Kirim
but Upload Receipt btn remain "Upload Receipt". It doesn't change to Upload Penerimaan.
How this is happened as other text chages as per locale.
Also I am facing same problem in all dialogs pop ups.Text remain in english in all dialogs.
What should be done?

try restarting your activity as suggested by other answers, check Ben's answer How do I restart an Android Activity
public void reload() {
Intent intent = getIntent();
overridePendingTransition(0, 0);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
finish();
overridePendingTransition(0, 0);
startActivity(intent);
}

Related

How to implement localization from fragment?

I am switching application language English to Arabic and vice versa from a fragment.
The code below works, it loads the values-ar\strings.xml and the language is changed from english to arabic. The app's UI elements doesn't change immediately based on the updated language settings, RTL alignment. The change is only reflected when application is closed and reopened.
Locale locale = new Locale("ar");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getActivity().getResources().updateConfiguration(config, getActivity().getResources().getDisplayMetrics());
getActivity().recreate()
It worked, add this to the activity holding the fragment and call it instead of getActivity().recreate().
public void restartActivity() {
Intent intent = getIntent();
finish();
startActivity(intent);
}
Add this line in the manifest file.
android:configChanges="locale|orientation"

Android developer change all app screens language on button click

I know how to use locale and strings.xml for global locale, in addition I created 3 buttons on rhe main activity with 3 different languages on them, how can I switch the whole strings in all screens in one click, how can I do that?
Step1:
Use a preference to store the locale language and country, if you want this language switching is persistent.
Step2:
Try a function like this
public void switchLocale(Locale locale) {
Configuration config = getBaseContext().getResources().getConfiguration();
if (locale != null && !config.locale.equals(locale)) {
Locale.setDefault(locale);
Configuration conf = new Configuration(config);
conf.locale = locale;
getBaseContext().getResources().updateConfiguration(conf,
getBaseContext().getResources().getDisplayMetrics());
}
}
Step3:
Restart your current activity.
Hope this helps.

Change app language at run time in Android

I am trying to change the language of my Android app with no success.
I have:
/res/
. values/
. . string.xml
. . values-es/
. . . strings.xml
Both strings files contains the same but the content is translated according to the language code.
The default language is english but I want to do some tests and use the spanish language.
I use the code written in this previous question:
Change app language programmatically in Android
Here is my LoginActivity(launcher)'s onCreate method:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setting default screen to login.xml
setContentView(R.layout.login);
Resources res = getBaseContext().getResources();
// Change locale settings in the app.
DisplayMetrics dm = res.getDisplayMetrics();
android.content.res.Configuration conf = res.getConfiguration();
conf.locale = new Locale("es");
res.updateConfiguration(conf, dm);
TextView registerScreen = (TextView) findViewById(R.id.link_to_register);
// Listening to register new account link
registerScreen.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Switching to Register screen
Intent i = new Intent(getApplicationContext(), RegisterActivity.class);
startActivity(i);
}
});
Button mainMenu = (Button) findViewById(R.id.btnLogin);
// Listening to register new account link
mainMenu.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Switching to Register screen
Intent i = new Intent(getApplicationContext(), MainMenuActivity.class);
startActivity(i);
}
});
}
My target is Android 4.4.
However, this is not working on my Nexus 4 - Android 4.4, it always uses English language.
The values-es folder should be on the same level as the values folder. Your example above looks like you have values-es inside the values folder.

Wrong language fonts at multi language app

i'm trying to make a multilanguage app, English and Greek.
All i want is 2 buttons at the first screen that u will choose language and then all the other app will bve at that language. I have make a simple test project with 2 buttons and a paragraph, when u push the english button the text will be english, when u push the greek button the text will be greek.
My code
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_page2);
Button english = (Button) findViewById(R.id.english);
english.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Locale locale = new Locale("en_UK");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getApplicationContext().getResources().updateConfiguration(config, null);
Intent intent = new Intent(v.getContext(), MainActivity.class);
startActivityForResult(intent, 0);
}
});
Button greek = (Button) findViewById(R.id.greek);
greek.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Locale locale = new Locale("el_GR");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getApplicationContext().getResources().updateConfiguration(config, null);
Intent intent = new Intent(v.getContext(), MainActivity.class);
startActivityForResult(intent, 0);
}
});
}
the strings.xml with the english paragraph is at the original res/values folder
and the strings.xml with the greek language is in res/values-el
here is 3 screenshots from the test app
here is some screenshots:
the one with the non letters things is the greek paragraph
the problem is that your phone doesnot supports unicode characters for greek,
there are two solutions,
1)It means that fonts your device uses do not support greek characters (hence unknown unicode). You need to find better font and use it in your application (see http://developer.android.com/reference/android/graphics/Typeface.html docs)
2)get online unicode converter or google translator for english to greek and copy the words to your strings.xml

Some strings are not translated when changing system language without restarting app

I'm having a problem where if I open my android app and then go into the system settings to change the language and then open the app again, some strings in the app won't be translated unless the app is force quit and relaunched. Any idea why this happens? I don't have android:configChanges set to "locale" anywhere in my AndroidManifest, so doesn't this mean all activities should be restarted on their own?
I also had this issue.I used the code below which was posted in some StackOverflow answer and then it changed the language without refreshing the activity
public void setLocale(String lang) {
myLocale = new Locale(lang);
Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = myLocale;
res.updateConfiguration(conf, dm);
onConfigurationChanged(conf);
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
// refresh your views here
lblLang.setText(R.string.langselection);
super.onConfigurationChanged(newConfig);
}
I hope it would help you.......
Finally figured out the problem. The strings that weren't being translated were being populated within a static class. So because this class was only being instantiated once, the strings weren't getting re-populated again with the proper translations.

Categories

Resources