且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何更改应用程序的语言

更新时间:2023-12-06 10:06:58

使用它以编程方式更改语言:

Use this to change the language programmatically:

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

写下该语言的国家代码来代替您想要的任何语言的 "en_US".例如,对于日语,ja_JP;对于阿拉伯语,ar.检查 此链接 获取列表.

Write the country code of the language in place of "en_US" for whatever language you want. For example, for Japanese, ja_JP; for Arabic, ar. Check this link for a list.

res/values-ja 中创建一个文件夹用于日语或 res/values-ar 用于阿拉伯语..

And make a folder in res/values-ja for Japanese or res/values-ar for Arabic..

并制作一个 string.xml 文件,并在布局上放置您想要的任何语言.它将从 values 文件夹中获取默认语言,否则如果您需要手动获取,它将从您的外部文件夹 values-ar 等中获取.

And make a string.xml file, and put whatever languages you want on your layout. It will fetch the default language from values folder otherwise if you want it manually, then it will fetch from your external folder values-ar, etc.

res/values-ar 的例子,用于阿拉伯语:

An example of res/values-ar for Arabic:

<?xml version="1.0" encoding="UTF-8"?>
  <resources>
    <string name="label">حسب</string>
    <string name="name">بحث</string> 
    <string name="search">بحث :</string>
 </resource>