简体   繁体   中英

How to load string.xml for indian languages in android?

I am trying to load strings.xml files for following Indian languages:

  1. Oriya (values-or)
  2. Bengali (values-bn)
  3. Punjabi (values-pa)
  4. Gujarati (values-gu)
  5. Marathi (values-mr)
  6. Malayalam (values-ml)
  7. Telugu (values-te)
  8. Tamil (values-ta)
  9. Kannada (values-kn)

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??

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

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 !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM