Internationalization và Localization vô Java tiếp tục nhắc đến Internationalization và Localization.
Bạn đang xem: locale là gì
Internationalization cũng rất có thể được viết lách tắt là i18n vì thế với 18 ký tự động ở đằm thắm I và N. Internationalization (quốc tế hoá) là một trong những nghệ thuật được cho phép tất cả chúng ta đưa đến những phần mềm nhưng mà rất có thể thích nghi với rất nhiều ngữ điệu và nhiều chống không giống nhau.
Localization cũng rất có thể được viết lách tắt là l10n vì thế 10 ký tự động ở đằm thắm L và N. Localization (nội địa hoá) là nghệ thuật canh ty đưa đến một phần mềm rất có thể thích nghi với cùng 1 ngữ điệu và vùng miền ví dụ.
Như vậy, một phần mềm tương hỗ nhiều ngữ điệu thì gọi là Internationalization (quốc tế hoá) và khi người tiêu dùng lựa chọn 1 ngữ điệu ví dụ nhằm hiển thị thì gọi là Localization (nội địa hoá)
Internationalization và localization vô Java – Các bộ phận vô phần mềm rất cần phải quốc tế hóa



Internationalization và localization vô Java – Quốc tế hoá những thông điệp (Messages)
Chúng tao tiếp tục dùng lớp ResourceBundle để xử lý quốc tế hoá cho những message. Các message vô phần mềm sẽ tiến hành khai báo vô tập dượt tin properties chứ không viết lách vô code (hardcoded).
Tên của tập dượt tin properties đặt theo dõi qui tắc filename_languagecode_countrycode với filename là tên gọi tập dượt tin tưởng, languagecode là mã ngữ điệu, countrycode là mã vương quốc. Mé bên dưới là bảng tế bào mô tả language code và country code theo dõi từng vương quốc.
Country | Language Code | Country Code |
English | en | EN |
Germany | de | DE |
China | zh | CN |
Czech Republic | cs | CZ |
Netherlands | nl | AN |
France | fr | FR |
Italy | it | IT |
Japan | ja | JP |
Korea | ko | KR |
Russia | ru | RU |
Spain | es | ES |
Bulgaria | bg | BG |
Canada | ca | CA |
Croatia | hr | HR |
Denmark | da | DK |
Finland | fi | FI |
Greece | el | GR |
Hungary | hu | HU |
Indonesia | in | ID |
Latvia | lv | LV |
Lithuania | lt | LT |
Norway | nb | NO |
Portugal | pt | PT |
Romania | ro | RO |
Serbia | sr | RS |
Slovakia | sk | SK |
Slovenia | sl | SI |
Sweden | sv | SE |
Thailand | th | TH |
Turkey | tr | TR |
Ukraine | uk | UA |
Vietnam | vi | VN |
Ví dụ tạo ra công tác hiển thị chuỗi “Hello World” và “Xin xin chào thế giới!” tuỳ nằm trong vô lựa lựa chọn của những người dùng là giờ đồng hồ Anh hoặc giờ đồng hồ Việt. Các bước triển khai như sau
Bước 1: Tạo 2 tập dượt tin tưởng properties ứng 2 ngữ điệu ham muốn hiển thị
Tạo tập dượt tin MessageBundle_en_US bằng phương pháp loài chuột cần vô project hoặc package -> lựa chọn New -> lựa chọn Properties File -> nhập thương hiệu tập dượt tin tưởng tại File Name -> lựa chọn Finish
Nhập nội dung mang lại tập dượt tin MessageBundle_en_US
#key = value greeting = Hello World!
Tạo tập dượt tin tưởng MessageBundle_vi_VN và nhập ngoại dung sau
greeting = Xin xin chào thế giới!
Lưu ý key trong các tập dượt tin tưởng cần giống như nhau, những tập dượt tin tưởng này chỉ không giống nhau về value. Mỗi value ứng cho 1 ngữ điệu.
Hình bên dưới cho thấy thêm nhị tập dượt tin tưởng properties được lưu vô packge tên internationalization.message
Xem thêm: tính diện tích tam giác biết 3 cạnh

Bước 2: Tạo lớp I18NMessages và thiết lập xử lý
package internationalization.message;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.Scanner;
/**
*
* @author indonesia-hanoi.org.vn
*/
public class I18NMessages {
public static void main(String[] args) {
int lang;
Scanner s = new Scanner(System.in);
ResourceBundle bundle;
bởi {
System.out.println("1. English");
System.out.println("2. Vietnamese");
System.out.println("3. Exit program");
System.out.print("Please choose your language: ");
thầy thuốc = s.nextInt();
switch (lang) {
case 1:
//internationalization.message is package name
//MessageBundle is properties tệp tin name
bundle = ResourceBundle.getBundle("internationalization.message.MessageBundle", Locale.US);
System.out.println("Message in " + Locale.US
+ ": " + bundle.getString("greeting"));
break;
case 2:
//changing the mặc định locale đồ sộ Vietnamese
Locale.setDefault(new Locale("vi", "VN"));
bundle = ResourceBundle.getBundle("internationalization.message.MessageBundle");
System.out.println("Message in " + Locale.getDefault()
+ ": " + bundle.getString("greeting"));
break;
}
} while (lang != 3);
}
}
Khi chạy công tác bên trên, tất cả chúng ta tiếp tục cảm nhận được sản phẩm sau

Internationalization và localization vô Java – Quốc tế hoá ngày (Date)
Chúng tao rất có thể quốc tế hoá ngày bằng phương pháp dùng cách thức getDateInstance() của lớp DateFormat. Phương thức này nhận vô 2 thông số là style với những loại như DEFAULT, SHORT, LONG và locale.
Ví dụ tại đây tiếp tục hiển thị ngày lúc này theo dõi những vùng miền không giống nhau như JP và VN
package internationalization;
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Scanner;
/**
*
* @author indonesia-hanoi.org.vn
*/
public class I18NDate {
private static void printDate(Locale locale) {
DateFormat formatter = DateFormat.getDateInstance(DateFormat.LONG,
locale);
Date currentDate = new Date();
String date = formatter.format(currentDate);
System.out.println(locale + " : " + date);
}
public static void main(String[] args) {
int region;
Scanner s = new Scanner(System.in);
bởi {
System.out.println("1. Japan");
System.out.println("2. Vietnam");
System.out.println("3. Exit program");
System.out.print("Please choose your region: ");
region = s.nextInt();
switch (region) {
case 1:
printDate(Locale.JAPAN);
break;
case 2:
printDate(new Locale("vi", "VN"));
break;
}
} while (region != 3);
}
}
Khi chạy chương trình

Internationalization và localization vô Java – Quốc tế hoá thời hạn (Time)
Trong ví dụ này, công tác tiếp tục hiển thị thời hạn lúc này dựa trên vùng miền được chỉ định và hướng dẫn.
package internationalization; import java.text.DateFormat; import java.util.Date; import java.util.Locale; import java.util.Scanner; /** * * @author indonesia-hanoi.org.vn */ public class I18NTime { static void printTime(Locale locale) { DateFormat formatter = DateFormat.getTimeInstance(DateFormat.LONG, locale); Date currentDate = new Date(); String time = formatter.format(currentDate); System.out.println(time + " in locale: " + locale); } public static void main(String[] args) { int region; Scanner s = new Scanner(System.in); bởi { System.out.println("1. England"); System.out.println("2. Vietnam"); System.out.println("3. Exit program"); System.out.print("Please choose your region: "); region = s.nextInt(); switch (region) { case 1: printTime(Locale.ENGLISH); break; case 2: printTime(new Locale("vi", "VN")); break; } } while (region != 3); } }
package internationalization;
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Scanner;
/**
*
* @author indonesia-hanoi.org.vn
*/
public class I18NTime {
static void printTime(Locale locale) {
DateFormat formatter = DateFormat.getTimeInstance(DateFormat.LONG, locale);
Date currentDate = new Date();
String time = formatter.format(currentDate);
System.out.println(time + " in locale: " + locale);
}
public static void main(String[] args) {
int region;
Scanner s = new Scanner(System.in);
bởi {
System.out.println("1. England");
System.out.println("2. Vietnam");
System.out.println("3. Exit program");
System.out.print("Please choose your region: ");
region = s.nextInt();
switch (region) {
case 1:
printTime(Locale.ENGLISH);
break;
case 2:
printTime(new Locale("vi", "VN"));
break;
}
} while (region != 3);
}
}
Internationalization và localization vô Java – Quốc tế hoá số (Number)
Cài bịa đặt ví dụ
package internationalization;
import static internationalization.I18NTime.printTime;
import java.text.NumberFormat;
import java.util.Locale;
import java.util.Scanner;
/**
*
* @author indonesia-hanoi.org.vn
*/
public class I18NNumber {
private static void printNumber(Locale locale) {
double n = 10000.12345;
NumberFormat formatter = NumberFormat.getNumberInstance(locale);
String number = formatter.format(n);
System.out.println(number + " for the locale " + locale);
}
public static void main(String[] args) {
int region;
Scanner s = new Scanner(System.in);
bởi {
System.out.println("1. England");
System.out.println("2. Vietnam");
System.out.println("3. Exit program");
System.out.print("Please choose your region: ");
region = s.nextInt();
switch (region) {
case 1:
printNumber(Locale.ENGLISH);
break;
case 2:
printNumber(new Locale("vi", "VN"));
break;
}
} while (region != 3);
}
}
Kết ngược khi chạy chương trình

Internationalization và localization vô Java – Quốc tế hoá chi phí tệ (Currency)
package internationalization;
import java.text.NumberFormat;
import java.util.Locale;
import java.util.Scanner;
/**
*
* @author indonesia-hanoi.org.vn
*/
public class I18NCurrency {
private static void printCurrency(Locale locale) {
float n = 10500.3245F;
NumberFormat formatter = NumberFormat.getCurrencyInstance(locale);
String currency = formatter.format(n);
System.out.println(currency + " for the locale " + locale);
}
public static void main(String[] args) {
int region;
Scanner s = new Scanner(System.in);
bởi {
System.out.println("1. Japan");
System.out.println("2. Vietnam");
System.out.println("3. Exit program");
System.out.print("Please choose your region: ");
region = s.nextInt();
switch (region) {
case 1:
printCurrency(Locale.JAPAN);
break;
case 2:
printCurrency(new Locale("vi", "VN"));
break;
}
} while (region != 3);
}
}
Kết ngược khi chạy chương trình
Xem thêm: bóng cao su thử nước giá rẻ

Tổng kết Internationalization và localization vô Java
- Quốc tế hoà và trong nước hoá
- Quốc tế hoà thông điệp
- Quốc tế hoá ngày, thời hạn, số và chi phí tệ
Bình luận