Multilingual Not working For Date Picker Android - android

We have some issue with Date picker when change the locale of Android App. any one can help me out.
When we change the language of Device Francia(Canada) then app is working fine with this code
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Locale.setDefault(resources.configuration.locales.get(0))
}else{
Locale.setDefault(resources.configuration.locale)
}
but if we change the locale of app without change the device language from setting the getting incorrect result.
var locale=Locale(SharedPreferencesManager.getStringPreference(Constants.LOCALE, ""))
Locale.setDefault(locale)
val resources = resources
val configuration = resources.configuration
val displayMetrics = resources.displayMetrics
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
configuration.setLocale(locale)
createConfigurationContext(configuration)
} else {
configuration.locale=locale
resources.updateConfiguration(configuration, displayMetrics)
}
Correct View
Incorrect View(Cancel and OK button not change as per app locale )

According to the http://code.google.com/p/android/issues/detail?id=25107 bug report on Android framework, the date picker is not using the String array of the shortened month of the Context of the app. Instead, it uses the System Locale.
You can use AppLocaleDatePickerDialog.java written by Gilbertwat.

Related

In Android , Why Locale.getDefault().displayLanguage and resources.configuration.locale.displayLanguage return different languages?

I am suffering from a problem
I am setting Apps language using :
#TargetApi(Build.VERSION_CODES.N)
private static Context updateResources(Context context, String language) {
Locale locale = new Locale(language);
Locale.setDefault(locale);
Configuration configuration = context.getResources().getConfiguration();
configuration.setLocale(locale);
configuration.setLayoutDirection(locale);
Timber.d("LangCheck updateResources " + locale.getLanguage());
Timber.d("LangCheck updateResources " + locale.getDisplayLanguage());
return context.createConfigurationContext(configuration);
}
private static Context updateResourcesLegacy(Context context, String language) {
Locale locale = new Locale(language);
Locale.setDefault(locale);
Resources resources = context.getResources();
Configuration configuration = resources.getConfiguration();
configuration.locale = locale;
configuration.setLayoutDirection(locale);
resources.updateConfiguration(configuration, resources.getDisplayMetrics());
Timber.d("LangCheck updateResources " + locale.getLanguage());
Timber.d("LangCheck updateResources " + locale.getDisplayLanguage());
return context;
}
After changing , I am able to use in selected language
But after restarting App , I am getting only English. i.e , the resources.getString(..) gave English only even though the above functions run at every startup in base activity
I tried to find the language of the App using : Locale.getDefault().displayLanguage . It's returning 'português' . It's correct
But , if I use resources.configuration.locale.displayLanguage , I am getting 'inglês' !!!
Why this conflict ? That is ,
Locale.getDefault().displayLanguage => português
resources.configuration.locale.displayLanguage =>. inglês
I think since context language is still in English , I am getting getString () -> English Strigs only
Why this conflict ?
pls help me ?
The difference you described comes from the two different ways you used to set the Locale: Locale.setDefault(locale) and configuration.setLocale(locale).
The first one changes the locale and stores it in a static field, which means it is updated for the whole process, and will not be changed until the app process is killed, or if Locale.setDefault is called again.
configuration.setLocale(locale) updates the locale only for the context you are returning. A context you used is probably an Activity context, which means the configuration locale is only changed for that one activity. When you "restart" the app, a new Activity is created, but the app process may not be killed (possible, but doesn't always happen), and that's why you see the difference between the two locale fields.
The Configuration's locale is what Android uses when using Context.getString(), so that's why after restarting you see the strings in English.
For much more details on how to change the locale at runtime, and common pitfalls with doing so, here's a decent article about it: https://proandroiddev.com/change-language-programmatically-at-runtime-on-android-5e6bc15c758

English to reginol language in android works but regional language to english not works

I am developing an application where 2 languages there English,Hindi.
if i change from english to hindi the date picker changed to hindi , but if i change from hindi to english still date picker shows hindi onli instead of English
i am using below
Locale locale = new Locale(language);
Locale.setDefault(locale);
Resources resources = context.getResources();
Configuration configuration = resources.getConfiguration();
configuration.locale = locale;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
configuration.setLayoutDirection(locale);
}
resources.updateConfiguration(configuration, resources.getDisplayMetrics());
Even though i set the locale whenever i changed the language some how it got
missed updating on date picker only , so before calling date picker , i just
setting the locale again before initialing datepicker with saved language from shared preferences
lang = Sharedpreferences.getStringValue(context, AppConstants.MS_PREF_SELECTED_LANG_KEY, MSAppConstants.LANGUAGE_ENGLISH_CODE);
LocaleHelper.setLocale(context.getApplicationContext(), lang);

Issue when changing Locale to ARABIC in Android application

My Android application supports two languages: Arabic and English. Arabic is the default language.
Now, to make Arabic as default language, i am changing my app locale to Arabic in Splash Screen. And i have maintained both English and Arabic string files for the locale change. But, when i click on some other random fragments (eg. Navigation menu item), my app static strings changes back to English locale.
I assume this is because my app default locale might have been changed to English. This issue is generated randomly, no specific scenarios are noted.
Can you suggest any solution?
Edit: I am using Shared preferences to save the language.
First step you wand to save the language in your sqlite (ex: language saved in table settings at sqlite):
mDatabase = new SqliteItemDatabase(getApplicationContext());
final List<Setting> allsettings = mDatabase.listSettings();
String the_lang = "";
if(position == 0)
{
the_lang = "en";
}
else if(position == 1)
{
the_lang = "ar";
}
LocaleHelper.setLocale(LAngSelect.this, the_lang);// =>set language
mDatabase.updateSettings(new Setting(allsettings.get(0).getId(),the_lang));
mDatabase.close();
Second step: to set activity right-to-left you want to add in every activity:
mDatabase = new SqliteItemDatabase(this);
final List<Setting> allsettings = mDatabase.listSettings();
String langs = allsettings.get(0).getLang() ;
if(langs.equals("ar")) {
//HERE CHECK CONDITION FOR YOUR LANGUAGE if it is AR then
//change if it is english then don't
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
//Resources res = getResources(); //resource handle
}
}

Set Android DatePicker title language

I have an app that is available in 4 languages, which can be chosen within the app. On Android, the DatePicker has a title. This title, even after setting the locale, always seems to favor the devices set locale. Only the title does this, as the functional part of the DatePicker is in the app-chosen language.
Here is what it looks like when the app is set to Korean.
How do I go about Changing that Thu, Aug 24 to Korean? I've set both Xamarin and Android locale to Korean. Is there an attribute in the Date Picker renderer I can set?
Thanks.
If you are doing something like this to apply a new locale context base configuration:
protected override void AttachBaseContext(Android.Content.Context #base)
{
Locale locale = Locale.Korean;
Locale.SetDefault(Locale.Category.Format, locale);
#base.Resources.Configuration.SetLocale(locale);
var newContext = #base.CreateConfigurationContext(#base.Resources.Configuration);
base.AttachBaseContext(newContext);
}
The Material-design CalendarView does not honor the context's locale correctly from the one that is passed to its .ctor and you end up with the wrong Title localization as shown in your question ;-(
One option is to subclass DatePicker and re-implement DatePickerCalendarDelegate and apply that to a sub-classed AlertDialog, but that is a bit crazy amount of coding to correctly address the issue.
So this is a fix (hack) that I have been using (simplified for SO, so you will need to API the various API level checks, etc..):
Material Design Fix (including Oreo beta):
// globals/cached
bool headerChangeFlag = true;
TextView headerTextView;
string headerDatePatternLocale;
SimpleDateFormat monthDayFormatLocale;
void SetHeaderMonthDay(DatePickerDialog dialog, Locale locale)
{
if (headerTextView == null)
{
// Material Design formatted CalendarView being used, need to do API level check and skip on older APIs
var id = base.Resources.GetIdentifier("date_picker_header_date", "id", "android");
headerTextView = dialog.DatePicker.FindViewById<TextView>(id);
headerDatePatternLocale = Android.Text.Format.DateFormat.GetBestDateTimePattern(locale, "EMMMd");
monthDayFormatLocale = new SimpleDateFormat(headerDatePatternLocale, locale);
headerTextView.SetTextColor(Android.Graphics.Color.Red);
headerTextView.TextChanged += (sender, e) =>
{
headerChangeFlag = !headerChangeFlag;
if (!headerChangeFlag)
return;
SetHeaderMonthDay(dialog, locale);
};
}
var selectedDateLocale = monthDayFormatLocale.Format(new Date((long)dialog.DatePicker.DateTime.ToUniversalTime().Subtract(
new System.DateTime(1970, 1, 1, 0, 0, 0, System.DateTimeKind.Utc)).TotalMilliseconds));
headerTextView.Text = selectedDateLocale;
}
Usage:
var dialog = new DatePickerDialog(this);
dialog.Show();
SetHeaderMonthDay(dialog, Locale.Korean); // Call once, the text change event will update it when user changes date...
Results:

How to change locale to use Latin Serbian (instead of Cyrillic Serbian)

The Serbian language has Latin and Cyrillic alphabets. In Android's Date and Time Picker widgets, the displayed alphabet for Serbian locales seems to be Cyrillic, as seen here.
I wanted to change the locale so that the android widgets are using the Latin Serbian alphabet.
The current language/country code (yielding Cyrillic) are sr and RS respectively. Therefore, my setLocale function is called as
setLocale("sr", "RS");
This is the part im not sure about - according to localeplanet.com, the local code for latin serbian is sr_Latn_RS. However, I tried both
setLocale("sr_Latn", "RS");
//and
setLocale("sr_Latn_RS", "RS");
neither of which work (no change occurs, default to english). According to the Android documentation, it looks like setLocale expects two letter codes.
The language codes are two-letter lowercase ISO language codes (such
as "en") as defined by ISO 639-1. The country codes are two-letter
uppercase ISO country codes (such as "US") as defined by ISO 3166-1.
The variant codes are unspecified.
So how do I specify a Latin serbian locale code? Or does it not exist?
The previous answer works well if you only support Lollipop or above. However, if you're coding in Serbian a lot of your user base probably won't have it. Here's a solution that works for old and new versions.
private static Locale serbianLatinLocale(){
Locale locale = null;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
for (Locale checkLocale : Locale.getAvailableLocales()) {
if (checkLocale.getISO3Language().equals("srp") && checkLocale.getCountry().equals("LATN") && checkLocale.getVariant().equals("")) {
locale = checkLocale;
}
}
} else {
locale = new Locale.Builder().setLanguage("sr").setRegion("RS").setScript("Latn").build();
}
return locale;
}
For getting latin locale I first used code below.
new Locale.Builder().setLanguage("sr").setRegion("RS").setScript("Latn").build();
But this solution didn't work on my Android 5.1.1 device (it was still in cyrillic). So I removed setting of region like this:
new Locale.Builder().setLanguage("sr").setScript("Latn").build();
And you have to put your string for serbian resources in b+sr+Latn folder.
Please search for your query before posting a question. It may be answered in some other related form.
Locale newLocale = new Locale("sr","RS");
Configuration config = new Configuration();
config.setLocale(newLocale);
// using this to reference my Activity
this.getBaseContext().getResources().updateConfiguration(config, this.getBaseContext().getResources().getDisplayMetrics());
i found these two answers suitable to your query
android custom date-picker SO and locale from english to french.
EDIT
Locale[] locales = Locale.getAvailableLocales();
for(Locale locale : locales){
if(locale.getCountry().equalsIgnoreCase("RS")
&& locale.getScript().equalsIgnoreCase("Latn"))
{
Configuration config = new Configuration();
config.setLocale(locale);
// using this to reference my Activity
this.getBaseContext().getResources().updateConfiguration(config, this.getBaseContext().getResources().getDisplayMetrics());
break;
}
}
I know there will be an efficient way to do it, however you may get the direction that you need to get the list of available locales and get the locale you desire. Hope it helps
EDIT-2 (Final)
you can construct the locale using:
Locale locale = new Locale.Builder().setLanguage("sr").setRegion("RS").setScript("Latn").build();
setLocale(locale);
Can you please use below one ?
public class MyApplication extends Application {
#Override
public void onCreate() {
super.onCreate();
Resources res = this.getResources();
Configuration conf = res.getConfiguration();
boolean isLatinAlphabet = PreferenceManager.getDefaultSharedPreferences(this);
if(conf.locale.getLanguage().equals("sr") && isLatinAlphabet) {
conf.locale = new Locale("sr", "YourContryCode");
res.updateConfiguration(conf, res.getDisplayMetrics());
}
}
}
Note: Replace your YourContryCode in conf.locale = new Locale("sr", "YourContryCode"); line.
Manifest.xml:
<application
android:name=".MyApplication"
android:icon="#drawable/ic_launcher"
android:label="#string/application_name"
android:theme="#style/AppTheme">
...
</application>
Hope this will help you.

Categories

Resources