Last active
January 11, 2023 10:46
-
-
Save dwaard/21bcaa0e668d5e41c9ec4bf72e5c17af to your computer and use it in GitHub Desktop.
Experiments with the Locale class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Experiments with the browser locale | |
console.log(Locale.getAvailableBrowserLocales()); | |
// Experiments with instantiating locales | |
let language = Locale.getCurrentBrowserLocale(); | |
language = 'sv'; | |
// language = 'hu-HU'; | |
// language = 'nl'; | |
const locale = new Locale(language); | |
// Experiments with basic translations | |
console.log(locale.t('Hello World')); | |
console.log(locale.t('There is no translation for this')); | |
// Experiments with paramaters | |
console.log(locale.t('Hello :name!', { name: 'Eddie' })); | |
// Adding a wrong parameter, what happens? | |
console.log(locale.t('Hello :name!', { naame: 'Eddie' })); | |
// Making it a bit more complex here | |
console.log(locale.t( | |
'My name is :name, I am :age years old. My brother name is also :name.', | |
{ name: 'Eddie', age: 18 }, | |
)); | |
// Experiments with transChoice | |
const str = '{0} There are no apples|{1} There is one apple|[2,*] There are :count apples'; | |
// Try it with different amounts | |
let amount = 0; | |
console.log(locale.transChoice(str, amount)); | |
amount = 1; | |
console.log(locale.transChoice(str, amount)); | |
amount = 10; | |
console.log(locale.transChoice(str, amount)); | |
// Experiments with formatting numbers and dates | |
console.log(locale.formatNumber(31415.927)); | |
console.log(locale.formatDate(new Date())); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"Hello World": "Helló Világ", | |
"Hello :name!": "Helló :name!", | |
"My name is :name, I am :age years old. My brother name is also :name.": | |
"A nevem :name, :age éves vagyok. A bátyám neve is :name.", | |
"{0} There are no apples|{1} There is one apple|[2,*] There are :count apples" : | |
"{0} Nincs alma|{1} Egy alma van|[2,*] Vannak :count alma" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"Hello World": "Hallo Wereld", | |
"Hello :name!": "Hallo :name!", | |
"My name is :name, I am :age years old. My brother name is also :name.": | |
"Mijn naam is :name en ik ben :age jaar. Mijn broer heet ook :name.", | |
"{0} There are no apples|{1} There is one apple|[2,*] There are :count apples" : | |
"{0} Er zijn geen appels|{1} Er is één appel|[2,*] Er zijn :count appels" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"Hello World": "Hej världen", | |
"Hello :name!": "Hej :name", | |
"My name is :name, I am :age years old. My brother name is also :name.": | |
"Jag heter :name, jag är :age år gammal. Min bror heter också :name.", | |
"{0} There are no apples|{1} There is one apple|[2,*] There are :count apples" : | |
"{0} Det finns inga äpplen|{1} Det finns ett äpple|[2,*] Det finns :count äpplen" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment