Java的國際化支援(I18N問題)

來源:互聯網
上載者:User

Java的國際化支援(I18N問題)

一,讀取PC的locale資訊

Locale資訊是由語言跟國家程式碼群組成的,在Java中,提供了Locale類對應語言資訊。

 

 

/* * //擷取local資訊 Locale defaultLocale = Locale.getDefault(); * System.out.println("country:" + defaultLocale.getCountry()); * System.out.println("language:" + defaultLocale.getLanguage()); */
二,如何支援國際化

1,加入國際化資源檔

國際化資源檔由baseName+Locale構成,例如:MessagesBundle_zh_CN.properties。預設的國際化資源檔名稱為baseName.properties。中文的國際化資源檔必須將檔案中value的值轉換為unicode編碼。例如:k1=\u4F60\u597D,{0}。

2,讀取國際化資源檔

// 定義locale類// Locale currentLocale = new Locale("zh", "CN");Locale defaultLocale = Locale.getDefault();ResourceBundle rb = ResourceBundle.getBundle("MessagesBundle",defaultLocale);String k1 = rb.getString("k1");String k2 = rb.getString("k2");System.out.println(k1);System.out.println(k2);MessageFormat mf = new MessageFormat(rb.getString("k1"));System.out.println(mf.format(new Object[] { "張三" }));

對於預設的預留位置,可以動態使用MessageFormat賦值。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.