开发者

Custom Multi Language support

I am aware of Creating a new values directory for the language with the suffix of the language code. For german: values-de or french: values-fr then copy our string.xml into that and translate each entry. And this works based on the Phone Localization settings

I wanted to know if we can bypass the phone setting and and make the user select his required language inside the app?

My requirement is, i want to give a language selection option inside my app, and make the user select the language he wants for the app.. how to dynamically switch between the string.x开发者_如何学Cml (for different languages) ???

thanks in advance


Create method which sets your basic Locale.Lets say

public static void setDefaultLocale(Context context,String locale) {
        Locale locJa = new Locale(locale);
        Locale.setDefault(locJa);

        Configuration config = new Configuration();
        config.locale = locJa;

        context.getResources().updateConfiguration(config, context.getResources()
                .getDisplayMetrics());

        locJa = null;
        config = null;
    }

Now check when user selected Locale.(Here basically I have used menu for language selection).

Configuration config = new Configuration();
String newLocale = config.locale.getLanguage().substring(0, 2)
    .toLowerCase();
if ("ja".equalsIgnoreCase(newLocale)) {
// Call above method with context & newLocale
} 
// Sequentially you check for Locale & change that.


Check out this post... It is the same thing basically.

Changing Locale within the app itself

Locale appLoc = new Locale("en");
Locale.setDefault(appLoc);
Configuration appConfig = new Configuration();
appConfig.locale = appLoc;
getBaseContext().getResources().updateConfiguration(appConfig,
    getBaseContext().getResources().getDisplayMetrics());


If you want to get images according their respective languages,You should create layout folder below like this.First i take the example for custom localization.

Locale appLoc = new Locale("xx");
Locale.setDefault(appLoc);
Configuration appConfig = new Configuration();
appConfig.locale = appLoc;
getBaseContext().getResources().updateConfiguration(appConfig,
    getBaseContext().getResources().getDisplayMetrics()); 

Your layout folder should be layout-xx and your drawable folder also should be drawable-xx.But one thing that when you change the language,you have to refresh the layout.I used in my app, take a button and set the background image.But sometimes images are not changed so i have done like this .

btn.setBackgroundDrawable(null);
btn.setBackgroundResource(R.drwable.yourimage);


It is very easy just follow this link

 languageToLoad = "hi"; // your language
        locale = new Locale(languageToLoad);
        Locale.setDefault(locale);
        config = new Configuration();
        config.locale = locale;
        getBaseContext().getResources().updateConfiguration(config,
                getBaseContext().getResources().getDisplayMetrics());
        this.setContentView(R.layout.activity_main);

- See more at: http://www.theappguruz.com/blog/multi-language-support-to-android-app#sthash.eGmzq57K.dpuf

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜