I'm having issues with default android spell checker: it doesn't want to switch to a different language. Instead it uses default system language for spell check.
Spell Checker works with default language, but when I change Locale to a different language it does not give me corrected words, but when I change language of the system Spell Checking works fine.
here is my code:
TextServicesManager tsm = (TextServicesManager) context.getSystemService(
Context.TEXT_SERVICES_MANAGER_SERVICE);
Locale locale = new Locale("fr"); //whatever language
Locale.setDefault(locale);
Configuration config = new Configuration();
config.setLocale(locale);
config.locale = locale;
Resources res = ctx.getResources();
res.updateConfiguration(config, res.getDisplayMetrics());
spellChecherSession = tsm.newSpellCheckerSession(null, locale, this, true);
I read documentation but I can't understand what am I doing wrong.
I've tried modifying locale and config, from multiple answers here, but it didn't work for me.
Please help to solve this problem.
Thanks.
Related
I use this snippet to allow the user to set his favorite locale in the appplication:
Locale locale = new Locale(newLan);
Locale.setDefault(locale);
Configuration config = getBaseContext().getResources().getConfiguration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,getBaseContext().getResources().getDisplayMetrics());
Problem is that I would like to write a setting that would allow the user to get back to the default langage of the phone.
How is it possible?
Because after using the snippet above and imagine user chose French, I cannot get back the phone locale (which might be english for instance)
I just tried this, my phone locale is US, the toast is shown in french but in the log I still see US, maybe if you don't set the new locale as default it works anyway?
Locale locale = new Locale("fr");
//Locale.setDefault(locale);
Configuration config = getBaseContext().getResources().getConfiguration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,getBaseContext().getResources().getDisplayMetrics());
Toast.makeText(this, android.R.string.cancel, Toast.LENGTH_LONG).show();
Log.d("LOCALE", Locale.getDefault().getCountry());
I've seen using Locale.setDefault() in other questions and answers, now I'm wondering, why would you be required to set the default Locale manually? If that was necessary, wouldn't it be done in updateConfiguration() anyway? this answer is also interesting
How about:
Saving current locale to shared preferences
Force to whatever you want
Use the shared preferences value to move back to original locale
Firstly this bit of code config.locale = locale; is deprecated, you should use config.setLocale(locale);
Have you tried getting the current device locale with Locale.getDefault().getDisplayLanguage();, and set it once the user chooses the default locale to be the selected language of your application with your code snippet?
In https://stackoverflow.com/a/34675427/519334 I solved the issue "go back to device-languge" by remembering the device-language in a static variable before any app-changes to locale have been taken place:
public class Global {
public static final Locale systemLocale = Locale.getDefault();
}
I wonder is every android phone has the en_US locale preinstalled? I have to pre-set the Locale in order to prevent comma separator issue.
Currently, I'm using the method below to force change, but I worried exceptions might happen on some devices.
Locale locale = new Locale("en_US");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
I wonder is every android phone has en_US locale preinstalled?
99.99% will have it.
They most likely have it.
In case you would like to check regardless, you could do something like this:
Locale locale = new Locale("en_US");
List availableLocales = Arrays.asList(Locale.getAvailableLocales());
if(!availableLocales.contains(locale)) {
// en_US locale not available, do your stuff here accordingly
}
I try to change my locale in an android application.
If I use only language all is ok, but today I added portuguese-br translation to my app.
Code:
Locale locale;
if(language.contains("-")) // In this case, the locale specify also the country
{
String[] country_locale = language.split("-");
locale = new Locale(country_locale[0], country_locale[1]);
}
else
locale = new Locale(language);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getApplicationContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
All is ok untill last line.
I know because in a previous piece of my program i could get some string with the correct pt-br locale with this code:
Resources resources = new Resources(ctx.getAssets(), ctx.getResources().getDisplayMetrics(), new_config);
updateConfiguration set my locale to English if Locale was defined with a country code.
String.xml is in values-pt-rBR
While debugging Locale value is set to pt-BR.
EDIT: After further tests, this works on my Android phone, but doesn't work on my tablet (both Sambung with android 4.4.2).
What could be the reason?
EDIT 2: Also over the emulator work if I use a phone, not work if I use a tablet.
On your current Activity, try call the follow after your code:
finish();
startActivity(getIntent());
This will recreate your Activity and, then, reload the string resources.
I use this code to change the language of my app manually:
Locale locale = new Locale(OneLanguageCode);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
I set in OneLanguageCode the international code. Everything works find for every code I have tried like "en", "es", "fr" and so on... But I am driving myself crazy with simplified Chinese.
In that case I have the folder called values-zh-rCN where the strings are. It works well if I set the device language to Simplified Chinese. But there is no way to set it manually with the code above.
I have tried using "zh-CN" as the OneLanguageCode value, but no success either. It is displayed in English.
As I said all this work with other languages. I can have people with Dutch as their device default language but have French in my app. Why am I not able to have Dutch as the device default language but have traditional Chinese in my app?
Thanks for your time.
Locale locale = new Locale("en_CA");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
context.getApplicationContext().getResources().updateConfiguration(config, null);
Thanks for the help of user2968888 this is how I solved:
Locale locale;
if(OneLanguageCode.equals("schinese")) //any tag here to know it is Simplified Chinese
locale = Locale.SIMPLIFIED_CHINESE;
else
locale = new Locale(OneLanguageCode);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getApplicationContext().getResources().updateConfiguration(config, null);
In my app users have the ability to switch their app locale to one of the other app supported languages. I'm having issues getting Portuguese-Brazil and Simplified Chinese to work. All other translations work properly when the user changes the locale settings.
These translations work properly if the device locale is pt-rBR or zh-rCN so the only thing that could be wrong is the locale code I use. However, anything I've tried fails. Anybody know the proper Android locale codes for these so users can properly switch if they desire?
Use
new Locale("pt","BR");
instead of
new Locale("pt_BR");
Use following code its working for me for traditional and simplified chinese.
if(selectedLanguage.equals("zh_CN"))
locale = Locale.SIMPLIFIED_CHINESE;
else if(selectedLanguage.equals("zh_TW"))
locale = Locale.TRADITIONAL_CHINESE;
else
locale = new Locale(selectedLanguage);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
baseContext.getResources().updateConfiguration(config, baseContext.getResources().getDisplayMetrics());