Search code examples
androidlocalemultilingual

Android mutilanguage support


I have seen that to have an app support a language such as Hindi then you need to do the following :- 1. In the res folder, create folders with language qualifiers that will contain the language specific strings file. 2. In your layout file, do not hardcode text, but instead use variables such as @string/hello_info. 3. Create a fonts folder under assets, and add the appropriate .ttf file into it.

However, in some places I have also seen the following being additionaly used.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    TextView tv = (TextView) findViewById(R.id.textView);
    Typeface hindi = Typeface.createFromAsset(getAssets(), "fonts/mangle.ttf");
    tv.setTypeface(hindi);
}

Is this now required, and if so under what circumstances ? I only need to display the multilanguage text via the layout.

Also, wrt the device's locale, ie presume that eg in India, the locale would be automaticcally set to the appropriate value for that area ? How am I able to test this either via the device or emulator ? My device just shows English(UK), English (US), France, and Spain. Thanks in advance


Solution

  • This code is only useful for set a custom font with that .ttf file which you've added on the assets:

    TextView tv = (TextView) findViewById(R.id.textView);
        Typeface hindi = Typeface.createFromAsset(getAssets(), "fonts/mangle.ttf");
        tv.setTypeface(hindi);
    

    So the answer is yes.

    the locale would be automaticcally set to the appropriate value for that area?

    If the device language is that language, yes.

    Languages Docs: http://developer.android.com/training/basics/supporting-devices/languages.html

    Localization-checklist: http://developer.android.com/distribute/tools/localization-checklist.html

    and you can set the language with:

    Locale locale = new Locale("en_US");
        Locale.setDefault(locale);
        Configuration config = new Configuration();
        config.locale = locale;
        SettingActivity.this.getApplicationContext().getResources().updateConfiguration(config, null);
        langd.dismiss();
        Intent i = getBaseContext().getPackageManager()
                .getLaunchIntentForPackage(getBaseContext().getPackageName());
        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(i);
    

    And after set, it will clear the screen and you can see the result.