【问题标题】:How to load string.xml for indian languages in android?如何在android中为印度语言加载string.xml?
【发布时间】:2017-03-29 05:50:18
【问题描述】:

我正在尝试为以下印度语言加载 strings.xml 文件:

  1. 奥里亚语(值或)
  2. 孟加拉语 (values-bn)
  3. 旁遮普语(价值观-pa)
  4. 古吉拉特语(价值观-gu)
  5. 马拉地语(价值观-先生)
  6. 马拉雅拉姆语(值-ml)
  7. 泰卢固语(价值观-te)
  8. 泰米尔语(值-ta)
  9. 卡纳达语(值-kn)

并尝试使用values-or-rIN,..etc。并使用以下代码加载语言环境:

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();
        }
    }

以上代码适用于所有国际语言。但不适用于上述语言。我看到了很多答案,根据他们我们需要加载相应语言的字体 ttf 文件。但我不想加载字体(TypeFace)。我想加载strings.xml。

谁能建议如何加载印度语言的strings.xml?是否可以?还是我应该为上述语言选择字体 ttf??

【问题讨论】:

  • 只需使用 values-gu ... 创建文件夹并在代码中为古吉拉特语输入 gu
  • 我提到我已经这样做了,但只得到英文文本。仅适用于印地语和英语。

标签: android localization


【解决方案1】:

像这样的古吉拉特语:

        Locale myLocale = new Locale("gu");
        Resources res =  getResources();
        DisplayMetrics dm = res.getDisplayMetrics();
        Configuration conf = res.getConfiguration();
        conf.locale = myLocale;
        res.updateConfiguration(conf, dm);

将“gu”换成其他语言名称 我在我的应用程序中使用古吉拉特语。这完美地工作

【讨论】:

  • 很好,工作正常。但是我无法理解我们的代码的一个问题几乎相同,只是我从 getBaseContext().getResource() 调用 getResource(),仅此而已。否则我所做的一切都是一样的。
  • 我认为 getbaseContext 在整个应用程序中不起作用,请尝试使用 getApplicationContext
  • 不适合我我正在使用Eureka 设备和kitkat
【解决方案2】:

我是通过以下方式完成的:

    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);

值文件夹名称应该是:

值-ml-rIN

价值观-gu-rIN

如果您的手机不支持特定字符,那么只有您需要字体,否则不需要自定义字体,我制作的应用程序支持 12 种印度本地语言,没有任何自定义字体。

使用以下方法检查您的手机是否支持特定语言

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;
    }  

希望对你有帮助!

【讨论】:

  • 什么是text in isSupported() 这个你传递的方法。
  • 我使用文件夹名称为values-gu 并且不适合我我正在使用Eureka 设备和kitkat
  • @Sagar 抱歉回复晚了,这里的文本表示特定语言的字符串,例如。要检查“印地语”语言是否受支持,所以它应该像这样调用 isSupported(context," हिंदी")。
  • @Sagar 你有没有找到任何修复程序来支持 Kitkat 中的语言。
  • @Srinivasan 我们的目标设备高于kitkat,仅显示支持的语言
猜你喜欢
  • 2014-04-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多