简体   繁体   English

如何为 Android 应用设置默认语言?

[英]How To Set Default Language for Android app?

I've created an app in two languages.我用两种语言创建了一个应用程序。 The second one (english), is used when user's default system language is english.第二个(英语),当用户的默认系统语言是英语时使用。 If it's not, then the first one is used.如果不是,则使用第一个。

I want to set the second language (that's english) as a DEFAULT language, which means that when user opens my app and his system language is not the first one, nor English, the English language will appear as a default one.我想将第二种语言(即英语)设置为默认语言,这意味着当用户打开我的应用程序并且他的系统语言不是第一种语言,也不是英语时,英语将显示为默认语言。

I tried:我试过:

    Locale locale = new Locale("en_US");
    Locale.setDefault(locale);
    Configuration config = new Configuration();
    config.locale = locale;
    context.getApplicationContext.getResources().updateConfiguration(config, null);

But got "context cannot be resolved" error everytime.. Is this piece of code right or..?但是每次都出现“上下文无法解决”错误..这段代码是对的还是..?

Okay,好的,

to make everything clear, I realized res/values is a DEFAULT directory and the others are just "in case of language".为了让一切都清楚,我意识到res/values是一个默认目录,而其他目录只是“在语言的情况下”。 So everything I had to do was to switch the english to /res/values and the other language goes to res/values-es所以我所要做的就是将英语切换到 /res/values 而另一种语言切换到res/values-es

You should define all languages you support using res folders, ie res/values , res/values-en , res/values-fr .您应该使用 res 文件夹定义您支持的所有语言,即res/valuesres/values-enres/values-fr The system will take care of everything else, you don't need any code.系统会处理其他一切,您不需要任何代码。

If you are in an activity you can do:如果您正在参加一项活动,您可以:

this.getApplicationContext().getResources().updateConfiguration(config, null);

...to fix your error. ...修复你的错误。 Otherwise you need to pass in the context.否则,您需要传入上下文。

Make sure you add the parenthesis at the end of getApplicationContext() .确保在getApplicationContext()的末尾添加括号。 You didn't do so in your code.您没有在代码中这样做。

I've been in the same situation, my app was first created in portuguese (BR) so we went global and I had as second language En-Us, so my solution was creating a new language ( clicking in translation Editor + Brazil )... so I have my default language (Portuguese) second (English) third (Portuguese)我遇到过同样的情况,我的应用程序最初是用葡萄牙语 (BR) 创建的,所以我们走向全球,我将 En-Us 作为第二语言,所以我的解决方案是创建一种新语言(单击翻译编辑器 + 巴西)。 ..所以我有我的默认语言(葡萄牙语)第二(英语)第三(葡萄牙语)

then I replaced the resource/ values to the english strings setting as default...然后我将资源/值替换为默认的英文字符串设置...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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