Skip to content

Commit

Permalink
feat(localization): 馃幐 locale, translated
Browse files Browse the repository at this point in the history
Refers: #5
  • Loading branch information
rcmoutinho committed Sep 11, 2019
1 parent d636166 commit c321845
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 72 deletions.
112 changes: 55 additions & 57 deletions book/03-localization/sections/01-locale.asc
Original file line number Diff line number Diff line change
@@ -1,44 +1,42 @@
:java-package: src/org/j6toj8/localization
:section-java-package: ../../../{java-package}

=== Localiza莽茫o (Locale)
=== Locale

.Objetivo
.Objective
--------------------------------------------------
Describe the advantages of localizing an application and developing code that defines, reads, and sets the locale with a Locale object.
-
Descrever as vantagens de localizar uma aplica莽茫o e desenvolver c贸digo que defina, leia e aplique a localidade em um objeto Locale.
--------------------------------------------------

脡 esperado que o candidato saiba compreender e analisar aspectos de Internacionaliza莽茫o e Localiza莽茫o, incluindo o uso da da classe `Locale`.
The candidate is expected to be able to understand and analyze aspects of Internationalization and Localization, including the use of the `Locale` class.

Alguns aspectos de uma aplica莽茫o podem depender do pa铆s e da linguagem. Por exemplo:
Some aspects of an application may depend on country and language. For example:

* Formatos de escrita de data
** O dia 6 de Agosto de 2019 seria representado no Brasil por `06/08/2019`, por茅m nos EUA seria `08/06/2019`.
* Formatos de escrita de valores monet谩rios
** Dez Reais no Brasil seriam representados por `R$ 10`, enquanto na Fran莽a Dez Euros seriam `10 鈧琡.
* Formatos de separa莽茫o de casas decimais ou milhares
** No Brasil utiliza-se v铆rgula para separar decimais e pontos para milhares: `100.245,03`. Nos EUA, utiliza-se o inverso: `100,245.03`.
* Date writing formats
** August 6, 2019, would be represented in Brazil by `06/08/2019`, but in the USA would be `08/06/2019`.
* Currency value writing formats
** Ten Reais in Brazil would be represented by `R$ 10`, while in France Ten Euros would be `10 鈧琡.
* Decimal or Thousand Separation Formats
** In Brazil comma is used to separate decimals and points to thousands: `100.245,03`. In the USA, it is the opposite: `100,245.03`.

Por isso, para que sua aplica莽茫o lide corretamente com esses cen谩rios, 茅 necess谩rio compreender dois aspectos: *Internacionaliza莽茫o* (_Internationalization_) e *Localiza莽茫o* (_Localization_).
Therefore, in order for your application to handle these scenarios correctly, you need to understand two aspects: *Internationalization* and *Localization*.

A *Internacionaliza莽茫o*, tamb茅m chamada de *_i18n_*, 茅 o ato de projetar sua aplica莽茫o para que seja poss铆vel facilmente adapt谩-la para utilizar novos formatos e idiomas.
*Internationalization*, also called *_i18n_*, is the act of designing your application so that you can easily adapt it to use new formats and languages.

A *Localiza莽茫o*, tamb茅m chamada de *_l10n_*, 茅 o ato de adaptar sua aplica莽茫o para de fato utilizar um novo formato espec铆fico.
*Localization*, also called *_l10n_*, is the act of tailoring your application to actually use a new specific format.

Antes de continuar, entenda a execu莽茫o do m茅todo `main` no exemplo a seguir e o que 茅 apresentado no console ap贸s sua execu莽茫o.
Before proceeding, understand the execution of the `main` method in the following example and what is presented on the console after its execution.

[source,java,indent=0]
.{java-package}/locale/Locale_Complete.java
----
include::{section-java-package}/locale/Locale_Complete.java[tag=code]
----

.Sa铆da no console
.console output
[source,console]
----
- Constantes -
- Constants -
en_CA
en_GB
- Construtor -
Expand All @@ -57,76 +55,77 @@ az_AZ_#Latn
bs_BA_POSIX_#Latn_c-cc
----

No Java, a classe `Locale` no pacote `java.util` nos ajuda a lidar com esses aspectos.
In Java, the `Locale` class in the `java.util` package helps us deal with these aspects.

. Geralmente, um `Locale` 茅 representado por um idioma e um pa铆s.
* `pt_BR` - Portugu锚s do Brasil
* `en_US` - Ingl锚s dos EUA
* `it_CH` - Italiano da Su铆莽a
* `fr_BE` - Franc锚s da B茅lgica
. Generally, a `Locale` is represented by a language and a country.
* `pt_BR` - Brazilian Portuguese
* `en_US` - US English
* `it_CH` - Italian from Switzerland
* `fr_BE` - French from Belgium
+
[source,java,indent=0]
.{java-package}/locale/Locale_LocaleLanguageCountry.java
----
include::{section-java-package}/locale/Locale_LocaleLanguageCountry.java[tag=code]
----

. Um `Locale` pode ter ainda uma Variante, um Script e Extens玫es.
. A `Locale` can also have a Variant, a Script and Extensions.
+
[source,java,indent=0]
.{java-package}/locale/Locale_VarScriptExtension.java
----
include::{section-java-package}/locale/Locale_VarScriptExtension.java[tag=code]
----
+
TIP: No exame da certifica莽茫o, a Oracle geralmente s贸 utiliza idioma e pa铆s.
TIP: In the certification exam, Oracle usually only uses language and country.

. 脡 poss铆vel construir um Locale com o `Builder`, com construtores, ou por uma _Language Tag_.
. It is possible to build a Locale with `Builder`, with constructors, or by a _Language Tag_.
+
[source,java,indent=0]
.{java-package}/locale/Locale_LocaleInstantiation.java
----
include::{section-java-package}/locale/Locale_LocaleInstantiation.java[tag=code]
----
+
A diferen莽a entre eles 茅:
The difference between them is:
+
* Com os construtores 茅 poss铆vel passar apenas o idioma, a regi茫o (pa铆s) e uma variante.
* Com language tags 茅 poss铆vel passar uma `String` no padr茫o https://en.wikipedia.org/wiki/IETF_language_tag[IETF BCP 47].
* Com o builder 茅 poss铆vel criar da forma mais espec铆fica poss铆vel: idioma, regi茫o, variante, script e extens玫es.
* With constructors it is possible to pass only the language, the region (country) and a variant.
* With language tags you can pass a `String` in the pattern https://en.wikipedia.org/wiki/IETF_language_tag[IETF BCP 47].
* With the builder you can create as specific as possible: language, region, variant, script and extensions.

. O `Locale` aceita letras mai煤sculas e min煤sculas escritas de forma incorreta.
. `Locale` accepts incorrectly typed uppercase and lowercase letters.
+
[source,java,indent=0]
.{java-package}/locale/Locale_LocaleCase.java
----
include::{section-java-package}/locale/Locale_LocaleCase.java[tag=code]
----
+
.Sa铆da no console
.console output
[source,console]
----
pt_BR
pt_BR
pt_BR
----
+
Nesse exemplo, escrevemos de forma *incorreta*:
In this example, we wrote *incorrectly*:
+
* O idioma deveria ser min煤sculo (pt), mas est谩 mai煤sculo (PT).
* A regi茫o deveria estar mai煤scula (BR), est谩 min煤scula (br).
* Language should be lowercase (pt), but it is in uppercase (PT).
* Region should be uppercase (BR), but it is in lowercase (br).

+
Mesmo assim, o `Locale` 茅 criado corretamente. Veja que isso 茅 um c贸digo *ruim*. O ideal 茅 sempre escrever respeitando mai煤sculas e min煤sculas.
Even so, `Locale` is created correctly. Note that this is *bad* code. The ideal is always to write respecting the case.

. Existem algumas constantes na classe `Locale` para as localiza莽玫es mais populares.
. There are some constants in the `Locale` class for the most popular locations.
+
[source,java,indent=0]
.{java-package}/locale/Locale_LocaleCommons.java
----
include::{section-java-package}/locale/Locale_LocaleCommons.java[tag=code]
----
+
.Sa铆da no console
.console output
[source,console]
----
en_CA
Expand All @@ -139,53 +138,52 @@ zh_CN
zh_TW
----

. 脡 poss铆vel recuperar o `Locale` padr茫o ou alter谩-lo programaticamente.
. You can retrieve the default `Locale` or change it programmatically.
+
[source,java,indent=0]
.{java-package}/locale/Locale_LocaleDefault.java
----
include::{section-java-package}/locale/Locale_LocaleDefault.java[tag=code]
----
+
.Sa铆da no console
.console output
[source,console]
----
pt_BR
en_BR
ko_KR
----

. 脡 poss铆vel verificar os `Locale` dispon铆veis, pois eles variam de acordo com a JVM sendo executada.
. You can check the available `Locale` as they vary according to the JVM running.
+
[source,java,indent=0]
.{java-package}/locale/Locale_LocaleAvailable.java
----
include::{section-java-package}/locale/Locale_LocaleAvailable.java[tag=code]
----
+
.Sa铆da no console
.console output
[source,console]
----
nn
ar_AE
ar_JO
ar_SY
hr_HR
fr_BE
es_PA
mt_MT
es_VE
bg
kea
nds
zu
am_ET
fr_DZ
ti_ET
----

. Um `Locale` tamb茅m pode ser representado somente pelo idioma.
. A `Locale` can also be represented by language only.
+
[source,java,indent=0]
.{java-package}/locale/Locale_LocaleLanguageOnly.java
----
include::{section-java-package}/locale/Locale_LocaleLanguageOnly.java[tag=code]
----
+
.Sa铆da no console
.console output
[source,console]
----
pt
Expand All @@ -194,12 +192,12 @@ es
fr
----

.Refer锚ncias
.References
****
* Adding Internationalization and Localization
+
Boyarsky, Jeanne; Selikoff, Scott. OCP: Oracle Certified Professional Java SE 8 Programmer II Study Guide (p. 255). Wiley. Edi莽茫o do Kindle.
Boyarsky, Jeanne; Selikoff, Scott. OCP: Oracle Certified Professional Java SE 8 Programmer II Study Guide (p. 255). Wiley. Kindle Edition.
* https://www.baeldung.com/java-8-localization[Internationalization and Localization in Java 8.]
Expand All @@ -209,4 +207,4 @@ Boyarsky, Jeanne; Selikoff, Scott. OCP: Oracle Certified Professional Java SE 8
* http://www.oracle.com/us/technologies/java/locale-140624.html[Internationalization: Understanding Locale in the Java Platform.]
****
****
2 changes: 1 addition & 1 deletion src/org/j6toj8/localization/locale/Locale_Complete.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class Locale_Complete {

// tag::code[]
public static void main(String[] args) throws IOException {
System.out.println(" - Constantes - ");
System.out.println(" - Constants - ");
System.out.println(Locale.CANADA);
System.out.println(Locale.UK);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class Locale_LocaleAvailable {
// tag::code[]
public static void main(String[] args) throws IOException {
Locale[] availableLocales = Locale.getAvailableLocales();
// imprime o 10 primeiros Locales dispon铆veis
// prints the first 10 available locales
for (int i = 0; i < 10; i++) {
System.out.println(availableLocales[i]);
}
Expand Down
4 changes: 2 additions & 2 deletions src/org/j6toj8/localization/locale/Locale_LocaleDefault.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ public class Locale_LocaleDefault {

// tag::code[]
public static void main(String[] args) throws IOException {
System.out.println(Locale.getDefault()); // o padr茫o inicial muda de acordo com seu dispositivo
Locale.setDefault(Locale.KOREA); // altera o Locale default
System.out.println(Locale.getDefault()); // initial default changes according to your device
Locale.setDefault(Locale.KOREA); // change the default Locale
System.out.println(Locale.getDefault()); // ko_KR
}
// end::code[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ public class Locale_LocaleInstantiation {

// tag::code[]
public static void main(String[] args) throws IOException {
new Locale("pt", "BR"); // pt-BR com Construtor
new Locale("pt", "BR"); // pt-BR with constructor

Locale.forLanguageTag("pt-BR"); // pt-BR com LanguageTag
Locale.forLanguageTag("pt-BR"); // pt-BR with LanguageTag

Locale localePtBR = new Locale.Builder() // pt-BR com Builder
Locale localePtBR = new Locale.Builder() // pt-BR with Builder
.setLanguage("pt")
.setRegion("BR")
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ public class Locale_LocaleLanguageCountry {

public static void main(String[] args) throws IOException {
// tag::code[]
new Locale("pt", "BR"); // Portugu锚s do Brasil
new Locale("en", "US"); // Ingl锚s dos EUA
new Locale("it", "CH"); // Italiano da Su铆莽a
new Locale("fr", "BE"); // Franc锚s da B茅lgica
new Locale("pt", "BR"); // Brazilian Portuguese
new Locale("en", "US"); // US English
new Locale("it", "CH"); // Italian from Switzerland
new Locale("fr", "BE"); // French from Belgium
// end::code[]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ public class Locale_LocaleLanguageOnly {

// tag::code[]
public static void main(String[] args) throws IOException {
System.out.println(new Locale("pt")); // portugu锚s
System.out.println(new Locale("en")); // ingl锚s
System.out.println(new Locale("es")); // espanhol
System.out.println(new Locale("fr")); // franc锚s
System.out.println(new Locale("pt")); // portuguese
System.out.println(new Locale("en")); // english
System.out.println(new Locale("es")); // spanish
System.out.println(new Locale("fr")); // french
}
// end::code[]
}

0 comments on commit c321845

Please sign in to comment.