亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

支持多國語言

支持多國語言

慕斯王 2023-05-10 15:16:04
我正在嘗試在我的應用程序中實現多語言(英語和希伯來語)。我已經創建了 2 個字符串文件并實現了應該支持多語言功能的所有方法。但是,當我運行該應用程序并在對話框中選擇“希伯來語”時,布局從左到右變為從右到左(希伯來語應該如此),但語言沒有改變。誰能幫我解決這個問題?    import android.app.Activity;    import android.content.Context;    import android.content.DialogInterface;    import android.content.Intent;    import android.content.SharedPreferences;    import android.content.res.Configuration;    import android.os.Bundle;    import android.os.Vibrator;    import android.support.v7.app.AlertDialog;    import android.support.v7.app.AppCompatActivity;    import android.view.View;    import android.widget.Button;    import java.util.Locale;    public class MainActivity extends AppCompatActivity {        private Button btn_chooseBoard;        private Button btn_store;        private Button btn_language;        @Override        protected void onCreate(Bundle savedInstanceState) {            super.onCreate(savedInstanceState);            loadLocale();            setContentView(R.layout.lay_main);            Constants instance = Constants.getInstance();            instance.getAllPlayers();            instance.getAllBoards();            btn_chooseBoard = (Button) findViewById(R.id.btn_startGame);            btn_store = (Button) findViewById(R.id.btn_popup_store);            btn_language = (Button) findViewById(R.id.btn_language);            btn_chooseBoard.setOnClickListener(new View.OnClickListener() {                @Override                public void onClick(View view) {                    Intent intent = new Intent(MainActivity.this, ChooseBoard.class);                    intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);                    startActivity(intent);                    ((Vibrator) getSystemService(Context.VIBRATOR_SERVICE)).vibrate(20);                }            });
查看完整描述

3 回答

?
梵蒂岡之花

TA貢獻1900條經驗 獲得超5個贊

Android 有自己的 API 來處理不同的語言環境。用戶在 Android 系統設置中設置他的語言環境。您不需要向用戶詢問他的語言環境。

只需將英文字符串放入 res/values/strings.xml,將希伯來語字符串放入 res/values-iw/strings.xml。也可以通過使用開始/結束而不是左/右來處理布局 xml 中的 ltr/rtl。

編輯:我不知道是否可以像在 setLocale() 中那樣更改應用程序的語言環境。

您應該關閉您的應用程序并將其從應用程序堆棧中刪除。然后在 Android 設置中將語言更改為希伯來語并重新打開您的應用程序。

如果您現在擁有在布局 .xml 中定義的 rtl 布局,但沒有希伯來語字符串,那么您的 strings.xml 可能有問題。

您的 strings.xml 可能沒有正確的路徑 (res/values-iw/strings.xml)?;蛘?strings.xml 的內容可能是錯誤的??赡芘c您的英文 strings.xml 中的字符串 ID 不同。

您還可以打印出您當前的語言環境,看看它是否設置為“iw”


查看完整回答
反對 回復 2023-05-10
?
白衣非少年

TA貢獻1155條經驗 獲得超0個贊

本地化步驟:

1)從您擁有的每個文本中創建一個字符串??旖莘绞绞寝D到文本,單擊文本,左側會出現一個黃色燈泡。單擊它并單擊Extract Resource,它將為您的文本生成一個名稱以添加到 Strings.xml。對每個文本都這樣做。

2)轉到 Res > Values 文件夾中的 Strings.xml 文件。右上角應該有藍色文字,你可以點擊Open Editor點擊它。

3)單擊左上角的小世界圖標。找到希伯來語。選擇它。點擊確定。現在您已經成功地為希伯來語語言創建了一個單獨的 Strings.xml。

4)返回到您的英文 Strings.xml 文件,Open Editor再次單擊,您將看到所有已存儲為字符串的文本。單擊一個字符串,在底部你會看到“Key”,它是字符串的 id 名稱,Default Value 是英文版本,以及 Translation,你必須手動輸入要翻譯的英文單詞的希伯來語翻譯。為您想要翻譯的所有內容執行此操作。請注意,無論您在翻譯字段中輸入什么,都會自動添加到 strings.xml(iw) 文件中。

5)第 4 步的替代方法是轉到希伯來文 Strings.xml 并在那里手動輸入信息,但您必須確?!懊荑€”(字符串的 ID 名稱)與英文相同。因此,根據我的經驗,第 4 步使它變得更容易。

6)您可以打開 Hebrew Strings.XML 手動編輯您放入其中的翻譯,以防您想要更改已經放入的較長文本。

所以它看起來像這樣:

字符串.xml

<resources>
    <string name="app_name">KotlinKennyGame</string>
    <string name="test">This is a test.</string>
</resources>

activity_main.xml

<TextView android:layout_width="wrap_content" 
          android:layout_height="wrap_content"
          android:text="@string/test"
            />

字符串.xml(iw)

<resources>
    <string name="app_name">KotlinKennyGame</string>
    <string name="test">?? ????</string>
</resources>


查看完整回答
反對 回復 2023-05-10
?
繁花如伊

TA貢獻2012條經驗 獲得超12個贊

嘗試改變

Configuration configuration = new Configuration();

Configuration configuration = getBaseContext().getResources().getConfiguration();


查看完整回答
反對 回復 2023-05-10
  • 3 回答
  • 0 關注
  • 187 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號