Heim  >  Artikel  >  Java  >  Internationalisierung in Java

Internationalisierung in Java

WBOY
WBOYOriginal
2024-08-30 15:41:371030Durchsuche

Der folgende Artikel bietet einen Überblick über die Internationalisierung in Java. Bei der Internationalisierung werden Webanwendungen so erstellt, dass sie automatisch mehrere Länder, Sprachen und Währungen unterstützen, ohne dass Änderungen an der Anwendung erforderlich sind. Es wird auch I18N genannt, da zwischen den Buchstaben I und N 18 Zeichen liegen. Der weltweite Markt ist heute ein wichtiger Faktor bei der Gestaltung von Software oder Websites. Unternehmen müssen Produkte entwickeln, die Benutzer in ihren lokalen Regionen und Sprachen ansprechen, während die Expansion von Softwareanwendungen für globale Märkte weiter voranschreitet.

Starten Sie Ihren kostenlosen Softwareentwicklungskurs

Webentwicklung, Programmiersprachen, Softwaretests und andere

Entwickler, die an Software für internationale Märkte arbeiten, sollten sich der Gepflogenheiten und Unterschiede in jeder Kultur bewusst sein. Beispiele für Unterschiede sind Sprache, Zeichensetzung, Währung, Datum, Uhrzeit, Ziffern und Zeitzonen. Die Lokalisierung wird auch als I10N abgekürzt, da zwischen dem Anfangsbuchstaben „L“ und dem letzten Buchstaben „N“ zehn Zeichen liegen. Bei der Lokalisierung werden einer Anwendung gebietsschemaspezifische Texte und Komponenten hinzugefügt, damit sie auf eine bestimmte Sprache und einen bestimmten Ort zugeschnitten werden kann.

Syntax der Internationalisierung in Java

Die folgenden Klassen können zur Umsetzung der Internationalisierung verwendet werden:

  • Gebietsschema
  • Zahlenformat
  • Datumsformat

1. Gebietsschema

Ein Locale-Objekt kann zur Darstellung eines geografischen Standorts oder einer Sprache verwendet werden. Das java.util-Paket enthält eine Locale-Klasse.

Konstrukteure der Locale-Klasse:

Locale l = new Locale(String language);
Locale l = new Locale(String language, String country);

Konstanten der Locale-Klasse:

Einige Locale-Konstanten sind bereits in der Locale-Klasse deklariert.

Diese Konstanten können direkt verwendet werden, einige Konstanten sind unten aufgeführt:

  • Gebietsschema. Großbritannien
  • Locale.ITALY
  • Locale.US

Funktionen der Locale-Klasse:

  • pubic static Locale getDefault(): Diese Methode wird verwendet, um die Instanz des aktuellen Gebietsschemas abzurufen.
  • public static Locale[] getAvailableLocales(): Diese Methode wird verwendet, um ein Array vorhandener Gebietsschemas abzurufen.
  • public String getDisplayLanguage(Locale l): Diese Methode wird verwendet, um den Sprachnamen des übergebenen Locale-Objekts abzurufen.
  • public String getDisplayVariant(Locale l): Diese Methode wird verwendet, um den Variantencode für das übergebene Locale-Objekt abzurufen.
  • public String getDisplayCountry(Locale l): Diese Methode wird verwendet, um den Ländernamen des übergebenen Locale-Objekts abzurufen.
  • public static getISO3Language(): Diese Methode wird verwendet, um die dreibuchstabige Abkürzung des aktuellen Sprachcodes des Gebietsschemas abzurufen.
  • public static getISO3Country(): Diese Methode wird verwendet, um die dreibuchstabige Abkürzung des aktuellen Landes des Gebietsschemas abzurufen.

2. Zahlenformat

Mit der NumberFormat-Klasse können wir eine Zahl entsprechend einem bestimmten Gebietsschema formatieren. Die NumberFormat-Klasse ist im Paket java.Text vorhanden und eine abstrakte Klasse, weshalb wir kein Objekt mithilfe ihres Konstruktors erstellen können.

Funktionen der Locale-Klasse:

  • public static NumberFormat getInstance(): Diese Methode wird verwendet, um das Objekt des Standardgebietsschemas abzurufen.
  • public static NumberFormat getInstance(Locale l): Diese Methode wird verwendet, um ihr Objekt für das übergebene Locale-Objekt abzurufen.
  • public static NumberFormat getCurrencyInstance(): Diese Methode wird verwendet, um das Objekt des Standardgebietsschemas abzurufen, um es in einer bestimmten Währung anzuzeigen.
  • öffentliches statisches Format (langes l):Diese Methode wird verwendet, um die übergebene Zahl in ein Gebietsschemaobjekt umzuwandeln.

3. Datumsformat

Wir internationalisieren das Datumsformat, da das Datumsformat von Standort zu Standort unterschiedlich ist. Wir können die DateFromat-Klasse verwenden, um das Datum gemäß einem bestimmten Gebietsschema zu formatieren. DateFormat ist eine abstrakte Klasse im java.text-Paket.

Konstanten der Locale-Klasse:

Einige DateFormat-Konstanten sind bereits in der DateFormat-Klasse deklariert.

Diese Konstanten können direkt verwendet werden, einige Konstanten sind unten aufgeführt:

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

Funktionen der DateFormat-Klasse:

  • 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:

Internationalisierung in 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:

Internationalisierung in 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:

Internationalisierung in 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.

Das obige ist der detaillierte Inhalt vonInternationalisierung in Java. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel:Native Methoden in JavaNächster Artikel:Native Methoden in Java