internationalization localization is the process of adapting anlux.dmcs.pl/budowanie_java/apj/5 -...

16
1

Upload: others

Post on 30-Sep-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: internationalization Localization is the process of adapting anlux.dmcs.pl/budowanie_java/apj/5 - Internacjonalizacja.pdfIn the Java 2 platform, java.util.Locale represents a specific

1

Page 2: internationalization Localization is the process of adapting anlux.dmcs.pl/budowanie_java/apj/5 - Internacjonalizacja.pdfIn the Java 2 platform, java.util.Locale represents a specific

The process of preparing an application to support more than one language and data format is called internationalization. Localization is the process of adapting an internationalized application to support a specific region or locale. Examples of locale-dependent information include messages and user interface labels, character sets and encoding, and date and currency formats. Although all client user interfaces should be internationalized and localized, it is particularly important for web applications because of the global nature of the web.

2

Page 3: internationalization Localization is the process of adapting anlux.dmcs.pl/budowanie_java/apj/5 - Internacjonalizacja.pdfIn the Java 2 platform, java.util.Locale represents a specific

3

Page 4: internationalization Localization is the process of adapting anlux.dmcs.pl/budowanie_java/apj/5 - Internacjonalizacja.pdfIn the Java 2 platform, java.util.Locale represents a specific

In the Java 2 platform, java.util.Locale represents a specific geographical, political, or cultural region. The string representation of a locale consists of the international standard two-character abbreviation for language and country and an optional variant, all separated by underscore (_) characters. Examples of locale strings include fr (French), de_CH (Swiss German), and en_US_POSIX (English on a POSIX-compliant platform).

Locale-sensitive data is stored in a java.util.ResourceBundle. A resource bundle contains key-value pairs, where the keys uniquely identify a locale-specific object in the bundle. A resource bundle can be backed by a text file (properties resource bundle) or a class (list resource bundle) containing the pairs. You construct resource bundle instance by appending a locale string representation to a base name.

4

Page 5: internationalization Localization is the process of adapting anlux.dmcs.pl/budowanie_java/apj/5 - Internacjonalizacja.pdfIn the Java 2 platform, java.util.Locale represents a specific

5

Page 6: internationalization Localization is the process of adapting anlux.dmcs.pl/budowanie_java/apj/5 - Internacjonalizacja.pdfIn the Java 2 platform, java.util.Locale represents a specific

Messages and labels should be tailored according to the conventions of a user’s language and region. There are two approaches to providing localized messages and labels in a web application:

Provide a version of the JSP page in each of the target locales and have a controller servlet dispatch the request to the appropriate page depending on the requested locale. This approach is useful if large amounts of data on a page or an entire web application need to be internationalized.

Isolate any locale-sensitive data on a page into resource bundles, and access the data so that the corresponding translated message is fetched automatically and inserted into the page. Thus, instead of creating strings directly in your code, you create a resource bundle that contains translations and read the translations from that bundle using the corresponding key.

6

Page 7: internationalization Localization is the process of adapting anlux.dmcs.pl/budowanie_java/apj/5 - Internacjonalizacja.pdfIn the Java 2 platform, java.util.Locale represents a specific

To get the correct strings for a given user, a web application either retrieves the locale (set by a browser language preference) from the request using the getLocale method, or allows the user to explicitly select the locale.

After the locale is set, the controller of a web application typically retrieves the resource bundle for that locale and saves it as a session attribute (see Associating Objects with a Session) for use by other components:

When a session is initiated, the resource bundle for the user’s locale is stored in the localization context. It is also possible to override the resource bundle at runtime for a given scope using the fmt:setBundle tag and for a tag body using the fmt:bundletag.

7

Page 8: internationalization Localization is the process of adapting anlux.dmcs.pl/budowanie_java/apj/5 - Internacjonalizacja.pdfIn the Java 2 platform, java.util.Locale represents a specific

The JSP versions of the Duke’s Bookstore application uses the fmt:message tag to provide localized strings for messages, HTML link text, button labels, and error messages:

Messages that are not queued on a component and are therefore not loaded automatically are referenced using a value expression. You can reference a localized message from almost any JavaServer Faces tag attribute.

The value expression that references a message has the same notation whether you loaded the resource bundle with the loadBundle tag or registered it with the resource-bundle element in the configuration file.

The value expression notation is var.message, in which var matches the var attribute of the loadBundle tag or the var element defined in the resource-bundle element of the configuration file, and message matches the key of the message contained in the resource bundle, referred to by the var attribute.

8

Page 9: internationalization Localization is the process of adapting anlux.dmcs.pl/budowanie_java/apj/5 - Internacjonalizacja.pdfIn the Java 2 platform, java.util.Locale represents a specific

9

Page 10: internationalization Localization is the process of adapting anlux.dmcs.pl/budowanie_java/apj/5 - Internacjonalizacja.pdfIn the Java 2 platform, java.util.Locale represents a specific

Java programs use the DateFormat.getDateInstance(int, locale) to parse and format dates in a locale-sensitive manner. Java programs use the NumberFormat.getXXXInstance(locale) method, where XXX can be Currency, Number, or Percent, to parse and format numerical values in a locale-sensitive manner. The servlet version of Duke’s Bookstore uses the currency version of this method to format book prices.

JSTL applications use the fmt:formatDate and fmt:parseDate tags to handle localized dates and use the fmt:formatNumber and fmt:parseNumber tags to handle localized numbers, including currency values. For information on the JSTL formatting tags, see Formatting Tags. The JSTL version of Duke’s bookstore uses the fmt:formatNumbertag to format book prices and the fmt:formatDate tag to format the ship date for an order:

10

Page 11: internationalization Localization is the process of adapting anlux.dmcs.pl/budowanie_java/apj/5 - Internacjonalizacja.pdfIn the Java 2 platform, java.util.Locale represents a specific

The following sections describe character sets and character encodings.

11

Page 12: internationalization Localization is the process of adapting anlux.dmcs.pl/budowanie_java/apj/5 - Internacjonalizacja.pdfIn the Java 2 platform, java.util.Locale represents a specific

A character set is a set of textual and graphic symbols, each of which is mapped to a set of nonnegative integers.

The first character set used in computing was US-ASCII. It is limited in that it can represent only American English. US-ASCII contains uppercase and lowercase Latin alphabets, numerals, punctuation, a set of control codes, and a few miscellaneous symbols.

Unicode defines a standardized, universal character set that can be extended to accommodate additions. When the Java program source file encoding doesn’t support Unicode, you can represent Unicode characters as escape sequences by using the notation \uXXXX, where XXXX is the character’s 16-bit representation in hexadecimal. For example, the Spanish version of the Duke’s Bookstore message file uses Unicode for non-ASCII characters:

A character encoding maps a character set to units of a specific width and defines byte serialization and ordering rules. Many character sets have more than one encoding. For example, Java programs can represent Japanese character sets using the EUC-JP or Shift-JIS encodings, among others. Each encoding has rules for representing and serializing a character set.

The ISO 8859 series defines 13 character encodings that can represent texts in dozens of languages. Each ISO 8859 character encoding can have up to 256 characters. ISO-8859-1 (Latin-1) comprises the ASCII character set, characters with diacritics (accents,diaereses, cedillas, circumflexes, and so on), and additional symbols.

UTF-8 (Unicode Transformation Format, 8-bit form) is a variable-width character encoding that encodes 16-bit Unicode characters as one to four bytes. A byte in UTF-8 is equivalent to 7-bit ASCII if its high-order bit is zero; otherwise, the character comprises a variable number of bytes.

UTF-8 is compatible with the majority of existing web content and provides access to the Unicode character set. Current versions of browsers and email clients support UTF-8. In addition, many new web standards specify UTF-8 as their character encoding. For example, UTF-8 is one of the two required encodings for XML documents (the other is UTF-16).

Web components usually use PrintWriter to produce responses; PrintWriter automatically encodes using ISO-8859-1. Servlets can also output binary data using OutputStream classes, which perform no encoding. An application that uses a character set that cannot use the default encoding must explicitly set a different encoding.

For web components, three encodings must be considered

12

Page 13: internationalization Localization is the process of adapting anlux.dmcs.pl/budowanie_java/apj/5 - Internacjonalizacja.pdfIn the Java 2 platform, java.util.Locale represents a specific

The request encoding is the character encoding in which parameters in an incoming request are interpreted. Currently, many browsers do not send a request encoding qualifier with the Content-Type header. In such cases, a web container will use the default encoding, ISO-8859-1, to parse request data.

If the client hasn’t set character encoding and the request data is encoded with a different encoding from the default, the data won’t be interpreted correctly. To remedy this situation, you can use the ServletRequest.setCharacterEncoding(String enc) method to override the character encoding supplied by the container. To control the request encoding from JSP pages, you can use the JSTL fmt:requestEncoding tag. You must call the method or tag before parsing any request parameters or reading any input from the request. Calling the method or tag once data has been read will not affect the encoding.

13

Page 14: internationalization Localization is the process of adapting anlux.dmcs.pl/budowanie_java/apj/5 - Internacjonalizacja.pdfIn the Java 2 platform, java.util.Locale represents a specific

For JSP pages, the page encoding is the character encoding in which the file is encoded.

For JSP pages in standard syntax, the page encoding is determined from the following sources:

1. The page encoding value of a JSP property group (see Setting Properties for Groups of JSP Pages) whose URL pattern matches the page.

2. The pageEncoding attribute of the page directive of the page. It is a translation-time error to name different encodings in the pageEncoding attribute of the page directive of a JSP page and in a JSP property group.

3. The CHARSET value of the contentType attribute of the page directive.

If none of these is provided, ISO-8859-1 is used as the default page encoding.

For JSP pages in XML syntax (JSP documents), the page encoding is determined as described in section 4.3.3 and appendix F.1 of the XML specification.

The pageEncoding and contentType attributes determine the page character encoding of only the file that physically contains the page directive. A web container raises a translation-time error if an unsupported page encoding is specified.

14

Page 15: internationalization Localization is the process of adapting anlux.dmcs.pl/budowanie_java/apj/5 - Internacjonalizacja.pdfIn the Java 2 platform, java.util.Locale represents a specific

The response encoding is the character encoding of the textual response generated by a web component. The response encoding must be set appropriately so that the characters are rendered correctly for a given locale. A web container sets an initial response encoding for a JSP page from the following sources:

1. The CHARSET value of the contentType attribute of the page directive2. The encoding specified by the pageEncoding attribute of the page directive3. The page encoding value of a JSP property group whose URL pattern matches the

page

If none of these is provided, ISO-8859-1 is used as the default response encoding.

The javax.servlet.ServletResponse.setCharacterEncoding, javax.servlet.ServletResponse.setContentType, and javax.servlet.ServletResponse.setLocale methods can be called repeatedly to change the character encoding. Calls made after the servlet response’s getWriter method has been called or after the response is committed have no effect on the character encoding. Data is sent to the response stream on buffer flushes (for buffered pages) or on encountering the first content on unbuffered pages.

Calls to setContentType set the character encoding only if the given content type string provides a value for the charset attribute. Calls to setLocale set the character encoding only if neither setCharacterEncoding nor setContentType has set the character encoding before. To control the response encoding from JSP pages, you can use the JSTL fmt.setLocale tag.

15

Page 16: internationalization Localization is the process of adapting anlux.dmcs.pl/budowanie_java/apj/5 - Internacjonalizacja.pdfIn the Java 2 platform, java.util.Locale represents a specific

16