Java の国際化

WBOY
リリース: 2024-08-30 15:41:37
オリジナル
990 人が閲覧しました

次の記事では、Java における国際化の概要を説明します。国際化とは、アプリケーションに変更を加えることなく、複数の国、言語、通貨を自動的にサポートするような Web アプリケーションを作成するプロセスです。文字 I と N の間に 18 文字があるため、I18N とも呼ばれます。今日、ソフトウェアや Web サイトを設計する場合、世界的な市場は重要な要素です。世界市場向けのソフトウェア アプリケーションの拡大が続く中、企業は現地の地域や言語のユーザーを魅了する製品を作成する必要があります。

無料ソフトウェア開発コースを始めましょう

Web 開発、プログラミング言語、ソフトウェア テスト、その他

国際市場向けのソフトウェアに取り組んでいる開発者は、それぞれの文化の習慣と違いを認識する必要があります。言語、句読点、通貨、日付、時刻、数字、タイムゾーンはすべて違いの例です。最初の文字「L」と最後の文字「N」の間に 10 文字があるため、ローカリゼーションは I10N とも短縮されます。ローカリゼーションとは、アプリケーションを特定の言語と場所に合わせて調整できるように、ロケール固有のテキストとコンポーネントをアプリケーションに追加するプロセスです。

Java における国際化の構文

次のクラスを使用して国際化を実装できます:

  • ロケール
  • 数値形式
  • 日付形式

1.ロケール

Locale オブジェクトは、地理的位置または言語を表すために使用できます。 java.util パッケージには Locale クラスが含まれています。

Locale クラスのコンストラクター:

Locale l = new Locale(String language);
ログイン後にコピー
Locale l = new Locale(String language, String country);
ログイン後にコピー

ロケールクラスの定数:

一部の Locale 定数は Locale クラスですでに宣言されています。

これらの定数は直接使用できます。いくつかの定数を以下に示します。

  • ロケール。イギリス
  • ロケール.ITALY
  • ロケール.US

ロケールクラスの関数:

  • pubic static Locale getDefault(): このメソッドは、現在のロケールのインスタンスを取得するために使用されます。
  • public static Locale[] getAvailableLocales(): このメソッドは、現在のロケールの配列を取得するために使用されます。
  • public String getDisplayLanguage(Locale l): このメソッドは、渡されたロケール オブジェクトの言語名を取得するために使用されます。
  • public String getDisplayVariant(Locale l): このメソッドは、渡されたロケール オブジェクトのバリアント コードを取得するために使用されます。
  • public String getDisplay Country(Locale l): このメソッドは、渡されたロケール オブジェクトの国名を取得するために使用されます。
  • public static getISO3Language(): このメソッドは、ロケールの現在の言語コードの 3 文字の省略形を取得するために使用されます。
  • public static getISO3 Country(): このメソッドは、ロケールの現在の国の 3 文字の略称を取得するために使用されます。

2.数値形式

NumberFormat クラスを使用すると、特定のロケールに従って数値を書式設定できます。 NumberFormat クラスは java.Text パッケージに存在し、抽象クラスであるため、そのコンストラクターを使用してオブジェクトを作成できません。

ロケールクラスの関数:

  • public static NumberFormat getInstance(): このメソッドは、デフォルトの Locale のオブジェクトを取得するために使用されます。
  • public static NumberFormat getInstance(Locale l): このメソッドは、渡された Locale オブジェクトのオブジェクトを取得するために使用されます。
  • public static NumberFormat getCurrencyInstance(): このメソッドは、特定の通貨で表示するデフォルト ロケールのオブジェクトを取得するために使用されます。
  • public static format(long l):このメソッドは、渡された数値をロケール オブジェクトに変換するために使用されます。

3.日付形式

日付の形式は場所によって異なるため、日付形式を国際化しています。 DateFromat クラスを使用すると、指定されたロケールに従って日付をフォーマットできます。 DateFormat は java.text パッケージの抽象クラスです。

ロケールクラスの定数:

一部の DateFormat 定数は、DateFormat クラスですでに宣言されています。

これらの定数は直接使用できます。いくつかの定数を以下に示します。

  • DateFormat.LONG
  • DateFormat.MINUTE_FIELD
  • DateFormat.MINUTE_FIELD

DateFormat クラスの関数:

  • final String format(Date date): This method converts and returns the specified Date object into a string.
  • static final DateFormat getInstance(): This method is used to gets a date-time formatter with a short formatting style for date and time.
  • static Locale[] getAvailableLocales(): This method is used to gets an array of present locales.
  • static final DateFormat getTimeInstance(): This method is used to gets time formatter with default formatting style for the default locale.
  • static final DateFormat getTimeInstance(int style): This method is used to gets time formatter with the specified formatting style for the default locale.
  • static final DateFormat getTimeInstance(int style, Locale locale): This method is used to gets time formatter with the specified formatting style for the given locale.
  • TimeZone getTimeZone(): This method is used to gets an object of TimeZone for this DateFormat instance.
  • static final DateFormat getDateInstance(): This method is used to gets date formatter with default formatting style for the default locale.
  • static final DateFormat getDateInstance(int style): This method is used to gets date formatter with the specified formatting style for the default locale.
  • static final DateFormat getDateInstance(int style, Locale locale): This method is used to gets date formatter with the specified formatting style for the given locale.
  • static final DateFormat getDateTimeInstance(): This method is used to gets date or time formatter with default formatting style for the default locale.
  • NumberFormat getNumberFormat(): This method is used to gets an object of NumberFormat for this DateFormat instance.
  • Calendar getCalendar(): This method is used to gets an object of the Calendar for this DateFormat instance.

Examples of Internationalization in Java

Given below are the examples mentioned:

Example #1

Example for the internationalization in Java to create different country locale.

Code:

// The program can be tested in Eclipse IDE, JAVA 11
package jex;
import java.util.Locale;
public class ex
{
public static void main(String[] args) {
Locale[] locales = { new Locale("en", "US"), new Locale("it", "IT"), new Locale("es", "ES") };
for (int l=0; l< locales.length; l++)
{
String Language = locales[l].getDisplayLanguage(locales[l]);
System.out.println(locales[l].toString() + ": " + Language);
}
}
}
ログイン後にコピー

Output:

Java の国際化

As in the above program, the Locale class objects are created and store in the array. Next, used the for loop to iterate each locale object and display its name and its language, as we can see in the above output.

Example #2

Example for the internationalization in Java to show the number in different formats for the different countries.

Code:

// The program can be tested in Eclipse IDE, JAVA 11
package jex;
import java.util.*;
import java.text.*;
public class ex
{
public static void main (String[]args)
{
double n = 45273.8956;
NumberFormat f1 = NumberFormat.getInstance (Locale.US);
NumberFormat f2 = NumberFormat.getInstance (Locale.ITALY);
NumberFormat f3 = NumberFormat.getInstance (Locale.CHINA);
System.out.println ("The number format in US is :" + f1.format (n));
System.out.println ("The number format in ITALY is:" + f2.format (n));
System.out.println ("The number format in CHINA is :" + f3.format (n));
}
}
ログイン後にコピー

Output:

Java の国際化

As in the above program, three different NumberFormat class objects are created using the Locale class. Next, using the format() method of the NumberFormat class, the given number is printing in the specific format of the country, as we can see in the above output.

Example #3

Example for the internationalization in Java to show the date in different formats for the different countries.

Code:

// The program can be tested in Eclipse IDE, JAVA 11
package jex;
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
public class ex
{
public static void main (String[]args)
{
DateFormat d1 = DateFormat.getDateInstance (0, Locale.US);
DateFormat d2 = DateFormat.getDateInstance (0, Locale.ITALY);
DateFormat d3 = DateFormat.getDateInstance (0, Locale.CHINA);
System.out.println ("The date format in US is :" + d1.format (new Date ()));
System.out.println ("The date format in ITALY is : " + d2.format (new Date ()));
System.out.println ("The date format in CHINA is : " + d3.format (new Date ()));
}
}
ログイン後にコピー

Output:

Java の国際化

As in the above program, three different DateFormat class objects are created using the Locale class. Next, using the format() method of the DateFormat class, the return date of the Date() method is printing in the specific format of the country, as we can see in the above output.

Conclusion

Internationalization is also called I18N because there are 18 characters between the letters I and N. It is the process of creating web applications in such a way that they automatically support several countries, languages, and currencies without requiring any changes to the application.

以上がJava の国際化の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
ソース:php
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!