Reputation: 495
Locale.getAvaialableLocales()
gives you all the locales available in device.
but device installs fonts supporting only some of those available locales
I used Resources.getSystem().getAssets().getLocales()
It returns list languages available in "settings-> Language & Inputs -> Language" option.
But the device is capable of support more Languages which are not existed in "settings-> Language & Inputs -> Language" also.
For example In my Karbon device Resources.getSystem().getAssets().getLocales() returns only "Hindi" and "Punjabi" as supporting languages. But this device works fine when select "Telugu" language by using "Hike" application.
So Is there any way that we can check a particular language is supported by the device.
Upvotes: 1
Views: 2360
Reputation: 471
Use this function to check if your device support specific locales:
boolean isLocaleAvailable(Locale locale){
Locale defaultLocale = Locale.getDefault();
//update default locale with mLocale
Locale.setDefault(locale);
String str = (String) DateUtils.getRelativeTimeSpanString(
Calendar.getInstance().getTimeInMillis() - 1000,
Calendar.getInstance().getTimeInMillis(), 0);
//revert locale back to default
Locale.setDefault(defaultLocale);
return !str.startsWith("-");
}
Upvotes: 0
Reputation: 499
I have done following way:
Call method like this:
isSupported(context,"English") //here "English" is the hardcoded string in specific language like Hindi,Urdu,panjabi .....etc.
Is supported method will return true if device has capability to draw specific language font other wise return false.
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: 1
Reputation: 809
Good Question Siva.
So far so many have been discussing this same thing concluding with no answer.
google groups discussion is here
Neither Locale.getAvailableLocales()
nor Resources.getSystem().getAssets().getLocales()
gives you right set of locales on which you can depend upon supporting app language.
Reason is,
Both of them gives you list of all locales supported by that OS. There might be more than 100 languages supported by OS, but the device manufacturer might not place all those language fonts(ttf files) in /system/fonts/(or any system font dir) just to save ROM memory. What they do is, since they make region specific roms, they only place fonts(locales) that are related to that perticular region. That is the reason you don't find India regional languages in America ROM.
The best way to solve it is, include all ttf files in your app assets for whatever languages you want to support, just like we give support for different language strings.
But take care of font licensing and all.
Hope this helps.
Upvotes: 4