Reputation: 43
I am trying to load strings.xml files for following Indian languages:
and tried with values-or-rIN,..etc
as well. and using following code to load locale:
public void changeLang(String lang) {
Configuration config = getBaseContext().getResources().getConfiguration();
if (!"".equals(lang) && !config.locale.getLanguage().equals(lang)) {
SharedPreferences.Editor ed = PreferenceManager.getDefaultSharedPreferences(this).edit();
ed.putString(getString(R.string.locale_lang), lang);
ed.commit();
locale = new Locale(lang);
Locale.setDefault(locale);
Configuration conf = new Configuration(config);
conf.locale = locale;
getBaseContext().getResources().updateConfiguration(conf, getBaseContext().getResources().getDisplayMetrics());
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = prefs.edit();
editor.putString(this.getString(R.string.locale_lang), lang);
editor.commit();
}
}
Above code is working for all international languages. but not for above-mentioned languages. I saw many answers, as per them we need to load fonts ttf files of the corresponding language. But I don't want to load fonts (TypeFace). I want to load strings.xml.
Could anyone suggest how to load strings.xml of Indian languages? Is it possible? or Should I go for fonts ttf for above-mentioned languages??
Upvotes: 4
Views: 4876
Reputation: 499
I had done this following way:
String lang = "ml";//for malyalam language
if(lang.equalsIgnoreCase(getResources().getString(R.string.text_item_ml))) {
lang = Constants.LANGUAGE_CODE_MALAYALAM;
country = Constants.COUNTRY_CODE_INDIA;
}
Locale locale;
if (country == null) {
locale = new Locale(lang);
} else {
locale = new Locale(lang, country);
}
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,getBaseContext().getResources().getDisplayMetrics());
onConfigurationChanged(config);
And values folder name should be:
values-ml-rIN
values-gu-rIN
If your phone not supporting particular character then only you need font other wise no need custom font, I made application which is supporting 12 indian local language without any custom font.
Check that your phone supported particular lang or not using below method
public static boolean isSupported(Context context, String text) {
final int WIDTH_PX = 200;
final int HEIGHT_PX = 80;
int w = WIDTH_PX, h = HEIGHT_PX;
Resources resources = context.getResources();
float scale = resources.getDisplayMetrics().density;
Bitmap.Config conf = Bitmap.Config.ARGB_8888;
Bitmap bitmap = Bitmap.createBitmap(w, h, conf); // this creates a MUTABLE bitmap
Bitmap orig = bitmap.copy(conf, false);
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.rgb(0, 0, 0));
paint.setTextSize((int) (14 * scale));
// draw text to the Canvas center
Rect bounds = new Rect();
paint.getTextBounds(text, 0, text.length(), bounds);
int x = (bitmap.getWidth() - bounds.width()) / 2;
int y = (bitmap.getHeight() + bounds.height()) / 2;
canvas.drawText(text, x, y, paint);
boolean res = !orig.sameAs(bitmap);
orig.recycle();
bitmap.recycle();
return res;
}
Hope It will help you !
Upvotes: 2
Reputation: 2576
like this for Gujarati language:
Locale myLocale = new Locale("gu");
Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = myLocale;
res.updateConfiguration(conf, dm);
Change "gu" with other language name I am using Gujarati in my app. This working perfectly
Upvotes: 3