Android 修改app的语言(主要讲繁体与简体的转换)


参考网址:

(1)、http://blog.youkuaiyun.com/liyuchong2537631/article/details/48292385

(2)、http://www.jb51.net/article/90326.htm


1、多语言的实现:是指同一个应用界面能支持多种语言的显示,供不同语言的人使用。也就是我们应用内显示的字符串都要有不同语言的多个备份。

首先最重要一点是不能把要显示的字符串写死在代码里,而是应该写到对应的配置文件中,也就是res目录下的strings.xml中。


2、根据需求为每种要支持的语言创建一份对应的strings.xml。

(1)新建文件夹



(2)选择locale,并选择要支持的语言和地区。(我选择的是繁体字,所以选择台湾)



(3)形成一份新的values-zh-rTW       strings.xml







这样,应用中显示的每一个字符串在上述方法得到的各个strings.xml文件中都需要有一个备份(当然,语言的内容要更改为对应语言)。


3、代码的实现:

(1)设置语言界面:(Setting.class)



先判断手机系统用的是什么语言

  if (sharedPreferencesUtil.getLanguage().equals("CN")) {
      
            tv_simple_language.setTextColor(getResources().getColor(R.color.e50011));//简体选中
            tv_tw_language.setTextColor(getResources().getColor(R.color.A333333));//繁体未选中
            
        } else if (sharedPreferencesUtil.getLanguage().equals("TW")) {
            
            tv_simple_language.setTextColor(getResources().getColor(R.color.A333333));//简体未选中
            tv_tw_language.setTextColor(getResources().getColor(R.color.e50011));//繁体选中
            
        } else if (sharedPreferencesUtil.getLanguage().equals("0")) {//sharedPreferencesUtil.getLanguage()初始化为 0 (获取系统的语言)
            if (getResources().getConfiguration().locale.getCountry().equals("CN")) {//判断当前手机系统的语言
            
                tv_simple_language.setTextColor(getResources().getColor(R.color.e50011));//简体选中
                tv_tw_language.setTextColor(getResources().getColor(R.color.A333333));//繁体未选中
                sharedPreferencesUtil.setLanguage("CN");//简体
                
            } else if (getResources().getConfiguration().locale.getCountry().equals("TW")) {
                
                tv_simple_language.setTextColor(getResources().getColor(R.color.A333333));//简体未选中
                tv_tw_language.setTextColor(getResources().getColor(R.color.e50011));//繁体选中
                sharedPreferencesUtil.setLanguage("TW");//繁体
                
            }
        }
    Resources resources = getResources();
    Configuration configuration = resources.getConfiguration(); // 获取资源配置
    DisplayMetrics metrics = resources.getDisplayMetrics();//获得屏幕参数:主要是分辨率,像素等。
    tv_simple_language.setTextColor(getResources().getColor(R.color.e50011));
    tv_tw_language.setTextColor(getResources().getColor(R.color.A333333));
    sharedPreferencesUtil.setLanguage("CN");//简体


    configuration.locale = Locale.CHINA; // 设置当前语言配置为简体
    resources.updateConfiguration(configuration, metrics); // 更新配置文件
    sendBroadcast(new Intent("language")); // 发送广播,广播接受后重新开启此Activtiy以重新初始化界面语言.
    finish(); Resources resources = getResources();
    Configuration configuration = resources.getConfiguration(); // 获取资源配置
    DisplayMetrics metrics = resources.getDisplayMetrics();
    tv_simple_language.setTextColor(getResources().getColor(R.color.A333333));
    tv_tw_language.setTextColor(getResources().getColor(R.color.e50011));
    sharedPreferencesUtil.setLanguage("TW");//繁体

    configuration.locale = Locale.TAIWAN; // 设置当前语言配置为繁体
    resources.updateConfiguration(configuration, metrics); // 更新配置文件
    sendBroadcast(new Intent("language")); // 发送广播,广播接受后重新开启此Activtiy以重新初始化界面语言.

(2)ChangeReceiver广播类(为了实现点击按钮后整个app的语言都相应的变化)
/**
 * Created by hong on 2016/10/26.
 *
 * 自定义广播类 语言改变后重启Activity
 *
 */
public class ChangeReceiver extends BroadcastReceiver {
    private Intent mIntent;

    @Override
    public void onReceive(Context context, Intent intent) {
        mIntent = new Intent(context, MainActivity.class);
        mIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);//关闭所有的activity,返回首页
        mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(mIntent);
    }
}

(3)在AndroidManifest.xml 里注册广播
 
   
       
       
        
        
        
             
         
         
        
        
        
    
       
       

(4)启动页界面(SplashActivity)
在启动页要判断之前是否有设置过语言
 
        Configuration config = getResources().getConfiguration();
        DisplayMetrics dm = getResources().getDisplayMetrics();
        if (sharedPreferencesUtil.getLanguage().equals("CN")) {
            config.locale = Locale.CHINA; // 设置当前语言配置为简体
            getResources().updateConfiguration(config, dm); // 更新配置文件
        } else if (sharedPreferencesUtil.getLanguage().equals("TW")) {
            config.locale = Locale.TAIWAN; // 设置当前语言配置为繁体
            getResources().updateConfiguration(config, dm); // 更新配置文件
        }

注:因为在MainActivity中有从服务端获取图片放在viewpager上,作为广告栏,存放图片是用
com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(bannerInfo.getThumb(), bannerImageView, displayImageOptions);存放的。bannerInfo.getThumb():图片.jpg     bannerImageView:ImageView  
设置语言的时候,用 DisplayMetrics metrics = new DisplayMetrics();广告栏的图片加载不出来,后面改成 DisplayMetrics dm =getResources().getDisplayMetrics();图片就可以加载出来了。(这只是我遇到的问题)













有问题请多指教,谢谢~






### 实现Android应用中的多语言支持 在Android应用程序中实现中文和英文的多语言支持,并将默认语言设置为英文,涉及创建不同的字符串资源文件以及调整应用程序逻辑以处理这些资源。 #### 创建语言资源文件 对于每种所需的语言,在`res/values/`目录下建立对应的子目录用于存储特定语言的字符串资源文件。例如: - `res/values/strings.xml`: 存储默认(即英文)的字符串。 ```xml <resources> <string name="app_name">My Application</string> <!-- 更多字符串 --> </resources> ``` - `res/values-zh-rCN/strings.xml`: 存储简体中文版的字符串。 ```xml <resources> <string name="app_name">我的应用</string> <!-- 对应更多翻译后的字符串 --> </resources> ``` 上述操作确保了当设备处于中国大陆地区并选择了中文环境时会自动加载此文件内的文本[^1]。 #### 应用启动时设定默认语言为英语 为了让应用首次运行或无特别指定情况下显示英文界面,可以在Application类或者MainActivity的onCreate方法里加入如下代码片段来强制更改Locale至English: ```java import java.util.Locale; import android.content.res.Configuration; public class MyApplication extends Application { @Override public void onCreate() { super.onCreate(); Locale locale = new Locale("en"); Locale.setDefault(locale); Configuration config = getBaseContext().getResources().getConfiguration(); config.locale = locale; getBaseContext().getResources().updateConfiguration(config, null); // 如果API级别大于等于24,则还需要调用以下两行代码 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){ createConfigurationContext(config); } } } ``` 这段Java代码的作用是在程序初始化阶段就指定了全局使用的区域信息为美国英语,从而使得整个应用程序默认呈现英文内容[^2]。 #### 提供用户切换语言的功能 如果希望允许用户手动选择他们喜欢的语言版本而不仅仅是依赖系统的当前设置,可以考虑添加一个选项菜单让用户能够自由地在这两者之间转换。这可以通过编程方式动态更新UI组件所关联的文字描述来完成。这里给出一个简单的例子说明如何做到这一点: 假设有一个按钮控件Button btnChangeLang;点击它之后弹出对话框询问用户想要更改为哪种语言,接着依据用户的输入执行相应的动作: ```java btnChangeLang.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { final String[] items={"English","Chinese"}; AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this); builder.setTitle("Choose Language").setItems(items, new DialogInterface.OnClickListener(){ @Override public void onClick(DialogInterface dialogInterface,int i){ switch(i){ case 0:// English setAppLanguage("en", MainActivity.this); break; case 1:// Chinese setAppLanguage("zh", MainActivity.this); break; } recreate(); // 更新Activity UI } }).show(); } }); private static void setAppLanguage(String langCode, Context context){ Resources resources=context.getResources(); DisplayMetrics dm=resources.getDisplayMetrics(); Configuration config=resources.getConfiguration(); Locale locale=new Locale(langCode); Locale.setDefault(locale); if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.JELLY_BEAN_MR1){ config.setLocale(locale); }else{ config.locale=locale; } resources.updateConfiguration(config,dm); } ``` 以上实现了基本的选择器功能,其中`recreate()`函数用来刷新当前活动页面以便立即反映出新的语言变化[^5]。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值