2011-06-24 106 views
30

可能重複:
Change language programatically in Android我怎樣才能改變我的應用程序的語言

我是新來的Android。在我的應用程序中,用戶可以選擇三種語言的語言。根據用戶選擇的語言,應該更改整個應用程序的語言。我怎樣才能做到這一點?

+0

使用此改變語言編程: 'code' Locale locale = new Locale(「en_US」); Locale.setDefault(locale); Configuration config = new Configuration(); config.locale = locale; context.getApplicationContext()。getResources()。updateConfiguration(config,null); setContentView(R.layout.activity_main); 'code' –

回答

6

您可以設置區域設置。

Resources res = context.getResources(); 
    // Change locale settings in the app. 
    DisplayMetrics dm = res.getDisplayMetrics(); 
    android.content.res.Configuration conf = res.getConfiguration(); 
    conf.locale = new Locale(language_code.toLowerCase()); 
    res.updateConfiguration(conf, dm); 

如果您有語言特定的內容 - 您可以更改該設置的基礎。 更多的細節,你可以看到Locale this also

49

使用此以編程方式更改語言:

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。檢查this link的列表。

,並在res/values-ja一個文件夾日語或res/values-ar阿拉伯語..

並做好string.xml文件,並把您是否想在佈局什麼語言。這將獲取從值文件夾的默認語言,否則如果你想手動,那麼它會從你的外部文件夾values-ar等取

阿拉伯語的res/values-ar一個例子:

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

在哪裏調用你的代碼!? – MBH

+0

在需要的地方撥打它。 – Hulk

+1

會調用它一次足以將其應用於整個應用程序?或每一項活動? – MBH

相關問題