2016-06-10 31 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

    相關問題