|
| 1 | ++++ |
| 2 | +title = "i18n in Java" |
| 3 | +outputs = ["Reveal"] |
| 4 | ++++ |
| 5 | + |
| 6 | +## i18n in Java |
| 7 | + |
| 8 | +--- |
| 9 | + |
| 10 | +### G11N/i18N/L10N |
| 11 | + |
| 12 | + |
| 13 | + |
| 14 | +> Internationalization is the process of designing an application so that it can be adapted to various **languages** and **regions** <ins>without engineering changes</ins>. |
| 15 | +
|
| 16 | +--- |
| 17 | + |
| 18 | +An internationalized program has the following characteristics: |
| 19 | + |
| 20 | +- With the <ins>addition of localized data</ins>, the <ins>same executable</ins> can run worldwide. |
| 21 | +- Textual elements, such as status messages and the GUI component labels, are <ins>not hardcoded</ins> in the program. Instead they are stored outside the source code and <ins>retrieved dynamically</ins>. |
| 22 | +- Support for <ins>new languages</ins> does not require recompilation. |
| 23 | +- Culturally-dependent data, such as <ins>dates and currencies</ins>, appear in formats that conform to the end user's region and language. |
| 24 | +- It can be <ins>localized quickly</ins>. |
| 25 | + |
| 26 | +--- |
| 27 | + |
| 28 | +### Java i18n Demo |
| 29 | + |
| 30 | +```java |
| 31 | +import java.util.Locale; |
| 32 | +import java.util.ResourceBundle; |
| 33 | + |
| 34 | +public class Hello { |
| 35 | + |
| 36 | + public static void main(String[] args) { |
| 37 | + String language = "en"; |
| 38 | + String country = "US"; |
| 39 | + |
| 40 | + if (args.length == 1) { |
| 41 | + language = args[0]; |
| 42 | + } else if (args.length == 2) { |
| 43 | + language = args[0]; |
| 44 | + country = args[1]; |
| 45 | + } |
| 46 | + |
| 47 | + var locale = new Locale(language, country); |
| 48 | + var messages = ResourceBundle.getBundle("messages", locale); |
| 49 | + |
| 50 | + System.out.print(messages.getString("hello") + " "); |
| 51 | + System.out.println(messages.getString("world")); |
| 52 | + } |
| 53 | +} |
| 54 | +``` |
| 55 | + |
| 56 | +--- |
| 57 | + |
| 58 | +messages_en.properties |
| 59 | +``` |
| 60 | +hello=Hello(en) |
| 61 | +world=World |
| 62 | +``` |
| 63 | + |
| 64 | +messages_en_US.properties |
| 65 | +``` |
| 66 | +world=World(en_US) |
| 67 | +``` |
| 68 | + |
| 69 | +messages_es.properties |
| 70 | +``` |
| 71 | +hello=Hola |
| 72 | +world=Mundo |
| 73 | +``` |
| 74 | + |
| 75 | +execute java |
| 76 | +``` |
| 77 | +java Hello.java |
| 78 | +java Hello.java es |
| 79 | +``` |
| 80 | + |
| 81 | +{{% fragment %}} |
| 82 | + |
| 83 | +``` |
| 84 | +Hello(en) World(en_US) |
| 85 | +Hola Mundo |
| 86 | +``` |
| 87 | + |
| 88 | +{{% /fragment %}} |
| 89 | + |
| 90 | +--- |
| 91 | + |
| 92 | +### Java i18N workflow |
| 93 | + |
| 94 | +1. Create the Properties Files |
| 95 | +``` |
| 96 | +greetings = Hello |
| 97 | +farewell = Goodbye |
| 98 | +inquiry = How are you? |
| 99 | +``` |
| 100 | +2. Define the **Locale** |
| 101 | +``` |
| 102 | +aLocale = new Locale("en","US"); |
| 103 | +``` |
| 104 | +3. Create a **ResourceBundle** |
| 105 | +``` |
| 106 | +messages = ResourceBundle.getBundle("MessagesBundle", currentLocale); |
| 107 | +``` |
| 108 | +``` |
| 109 | +MessagesBundle_en_US.properties |
| 110 | +MessagesBundle_fr_FR.properties |
| 111 | +MessagesBundle_de_DE.properties |
| 112 | +``` |
| 113 | +4. Fetch the Text from the ResourceBundle |
| 114 | +``` |
| 115 | +String msg1 = messages.getString("greetings"); |
| 116 | +``` |
| 117 | + |
| 118 | +--- |
| 119 | + |
| 120 | +How does an internationalized program identify the appropriate language and region of its end users? |
| 121 | + |
| 122 | +{{% fragment %}} |
| 123 | + |
| 124 | +It references a <ins>Locale</ins> object. |
| 125 | + |
| 126 | +{{% /fragment %}} |
| 127 | + |
| 128 | +--- |
| 129 | + |
| 130 | +{{% section %}} |
| 131 | + |
| 132 | +### Locale in i18N |
| 133 | + |
| 134 | +> Locale is the user-specific location and cultural information managed by a computer. [(RFC6365)](https://datatracker.ietf.org/doc/html/rfc6365) |
| 135 | +
|
| 136 | +> A concept or identifier used by programmers to represent a particular collection of cultural, regional, or linguistic preferences. |
| 137 | +
|
| 138 | +--- |
| 139 | + |
| 140 | +### Tags for Identifying Languages(BCP 47) |
| 141 | + |
| 142 | + |
| 143 | + |
| 144 | +``` |
| 145 | +langtag = language["-" script]["-" region]*("-" variant)*("-" extension)["-" privateuse] |
| 146 | +``` |
| 147 | + |
| 148 | +--- |
| 149 | + |
| 150 | +### i18N IETF Standard |
| 151 | + |
| 152 | +- [Terminology Used in Internationalization in the IETF](https://datatracker.ietf.org/doc/html/rfc6365) |
| 153 | +- [IETF BCP<sub>Best Current Practice</sub> 47<sup>Language Tag Registry Update (LTRU)</sup>](https://www.rfc-editor.org/info/bcp47) |
| 154 | +- [Making Sense of Language Tags](https://www.slideserve.com/shantell/making-sense-of-language-tags) |
| 155 | + |
| 156 | +--- |
| 157 | + |
| 158 | +### Locale in Java |
| 159 | + |
| 160 | +[java.util.Locale](https://github1s.com/openjdk/jdk/blob/master/src/java.base/share/classes/java/util/Locale.java) |
| 161 | + |
| 162 | +- Implements IETF [BCP 47](https://www.rfc-editor.org/info/bcp47) |
| 163 | + - [RFC 4647](https://www.rfc-editor.org/rfc/rfc4647.txt): Matching of Language Tags |
| 164 | + - <em>Filtering</em> is used to get all matching locales |
| 165 | + - <em>lookup</em> is to choose the best matching locale |
| 166 | + - [RFC 5646](https://www.rfc-editor.org/rfc/rfc5646.txt): Tags for Identifying Languages |
| 167 | +- Refers to IETF [RFC 2616](https://www.rfc-editor.org/rfc/rfc2616.txt) |
| 168 | + - [Quality Values](https://datatracker.ietf.org/doc/html/rfc2616#section-3.9) |
| 169 | +- Refers to ISO |
| 170 | + - [ISO 639](https://www.iso.org/iso-639-language-codes.html): Language codes |
| 171 | + - [ISO 3166](https://www.iso.org/iso-3166-country-codes.html): Country codes |
| 172 | + - [ISO 15924](https://unicode.org/iso15924/iso15924-codes.html): Script code |
| 173 | + |
| 174 | +{{% /section %}} |
| 175 | + |
| 176 | +--- |
| 177 | + |
| 178 | +How does Java get messages by locale identify? |
| 179 | + |
| 180 | +{{% fragment %}} |
| 181 | + |
| 182 | +It's <em>ResourceBundle</em>! |
| 183 | + |
| 184 | +{{% /fragment %}} |
| 185 | + |
| 186 | +--- |
| 187 | + |
| 188 | +### ResourceBundle in Java |
| 189 | + |
| 190 | +[java.util.ResourceBundle](https://github1s.com/openjdk/jdk/blob/master/src/java.base/share/classes/java/util/ResourceBundle.java) |
| 191 | + |
| 192 | +<a href="http://www.gdzwk.com/#/blog/i18n"> <img src="https://img.bmpi.dev/b542cf39-3946-24ed-a492-27f1f044d515.png" width="45%" height="45%" /></a> |
| 193 | + |
| 194 | +--- |
| 195 | + |
| 196 | +### Others for i18N |
| 197 | + |
| 198 | +- [Common Language Data Repository(CLDR)](http://cldr.unicode.org/)<sup>Incorporated into JDK 8</sup> |
| 199 | + - <ins>Locale-specific patterns for formatting and parsing</ins>: dates, times, timezones, numbers and currency values, measurement units,… |
| 200 | + - <ins>Translations of names</ins>: languages, scripts, countries and regions, currencies, eras, months, weekdays, day periods, time zones, cities, and time units, and sequences (and search keywords),… |
| 201 | + - <ins>Language & script information</ins>: characters used; plural cases; gender of lists; capitalization; rules for sorting & searching; writing direction; transliteration rules;… |
| 202 | + - <ins>Country information</ins>: language usage, currency information, calendar preference, week conventions,… |
| 203 | + - <ins>Validity</ins>: Definitions, aliases, and validity information for Unicode locales, languages, scripts, regions, and extensions,… |
| 204 | +- [UNICODE LOCALE DATA MARKUP LANGUAGE (LDML)](http://www.unicode.org/reports/tr35/) |
| 205 | + |
| 206 | +--- |
| 207 | + |
| 208 | +### Outside of Java |
| 209 | + |
| 210 | +- [GUN gettext](https://www.gnu.org/software/gettext/) |
| 211 | + - C |
| 212 | + - C++ |
| 213 | + - Python |
| 214 | + - PHP |
| 215 | + - Elixir |
| 216 | +- [ICU](http://site.icu-project.org/) |
| 217 | + - Code Page Conversion |
| 218 | + - Collation |
| 219 | + - Formatting(CLDR) |
| 220 | + - Time Calculations |
| 221 | + - Unicode Support |
| 222 | + - Regular Expression |
| 223 | + - Bidi |
| 224 | + - Text Boundaries |
| 225 | + |
| 226 | +--- |
| 227 | + |
| 228 | +### Further reading list |
| 229 | + |
| 230 | +- [Building a minimal i18n library](https://janmonschke.com/building-a-minimal-i18n-library) |
| 231 | +- [Clojure uses standard Java ResourceBundle](https://github.com/feldi/clojure-i18n/blob/master/src/i18n/core.clj) |
| 232 | +- [awesome-i18n](https://github.com/jpomykala/awesome-i18n) |
| 233 | +- [awesome-i18n](https://github.com/mrhota/awesome-i18n) |
| 234 | +- [国际化与本地化](https://www.bmpi.dev/dev/i18n-l10n/) |
| 235 | +- [国际化分析与处理](http://www.gdzwk.com/#/blog/i18n) |
0 commit comments