2016-06-10 46 views
3

我尝试学习如何在android studio中编程,我目前正在第二篇教程中介绍如何通过本地化来更改语言。 当我尝试在文件夹values_el内创建第二个strings.xml文件时,它说我不能让名称应该是唯一的。 我尝试将原始的strings.xml文件从值文件夹复制到新的values_el文件夹,我翻译了这些消息但没有任何反应。 另外我尝试右键点击原始strings.xml文件,我按翻译选项,我从那里翻译他们,但没有再发生。 当我在手机上运行应用程序时,语言在上述两种方式中都是英语。 我的手机语言是希腊语,但我的程序字母是英语。在Android应用程序中添加新区域并强制区域设置(本地化)

问题2.

首先,为什么语言没有我的电话改变? 第二是有没有办法通过按下按钮来改变我的应用程序的语言,而我打开它?我在谷歌播放的一些游戏可以选择在开始游戏之前选择你的语言。 对不起,我的英语,如果你不明白我说的话请让我知道,所以我尝试用谷歌翻译帮助更好地解释它。 无论如何感谢您的时间。

那是我的代码运行

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:showIn="@layout/activity_my" 
    android:orientation="horizontal"> 

    <EditText 
     android:id="@+id/edit_message" 
     android:layout_weight="1" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:text="@string/edit_message" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/button_send" 
     android:onClick="sendMessage"/> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/test" 
     android:onClick="testActivity" /> 

</LinearLayout> 

回答

3

你为什么不使用最简单的方式通过IDE?

  1. 打开strings.xml文件
  2. 点击Open Editor链接所看到如下

enter image description here

  • 添加您要添加​​翻译的语言环境像这样选择
  • enter image description here

  • 这将创建一个适合你的文件没有你不必担心文件名

  • 你不需要打开每个strings.xml把本地化的字符串。从这个strings.xml编辑器中执行它。

  • 回到第二个问题:本地应用程序将是在设备设置中在设备上选择的区域设置。如果您尚未针对设备区域设置进行本地化,则它将回落到res/values下的主要strings.xml

    要强制区域设置在您的应用程序,而不管设备的语言环境:

    添加以下方法在你的活动类,并调用它在onCreate

    private void setLanguageForApp(String language){ 
    
        String languageToLoad = language; //pass the language code as param 
        Locale locale; 
        if(languageToLoad.equals("not-set")){ 
         locale = Locale.getDefault(); 
        } 
        else { 
         locale = new Locale(languageToLoad); 
        } 
        Locale.setDefault(locale); 
        Configuration config = new Configuration(); 
        config.locale = locale; 
        getBaseContext().getResources().updateConfiguration(config, 
          getBaseContext().getResources().getDisplayMetrics()); 
    } 
    
    +0

    这样也可以用希腊国旗创建文件strings.xml而不做任何事情。谢谢。 – Mixalis

    1

    您的文件夹名称为:values_el这是不正确。 它应该是values-el

    是的,可以更改应用程序的语言。请参考下面的链接。

    Change language programmatically in Android

    +0

    我尝试,但没有任何反应。也许我做了别的错事。无论如何感谢您的反馈。 – Mixalis

    0

    我不知道如果我正好理解你的问题,但你可以通过右键点击资源添加新语言 - >新-Android资源目录 - >区域和选择希腊和地区。该目录将被创建并在那里添加带有翻译的文件。 如果您在这一点上改变手机的语言,它应该显示翻译的应用程序。

    第二个问题,我不确定如果是正确的方法,但我会更改区域设置并重新加载应用程序或活动。像这样

    String languageToLoad = "fr_FR"; 
    Locale locale = new Locale(languageToLoad); 
    Locale.setDefault(locale); 
    Configuration config = new Configuration(); 
    config.locale = locale; 
    context.getResources().updateConfiguration(config,context.getResources().getDisplayMetrics()); 
    
    
    Intent intent = new Intent(XYZ.this, XYZ.class); 
    

    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    startActivity(intent);

    检查这个答案:https://stackoverflow.com/a/15971553/1623224

    +0

    谢谢。我现在要尝试手动语言变更。该代码看起来做我想要的。 – Mixalis