Upgrade to Pro — share decks privately, control downloads, hide ads and more …

javascript internationalization API

javascript internationalization API

EcmascriptのInternationalization APIについて

More Decks by Taketoshi Aono(青野健利 a.k.a brn)

Other Decks in Programming

Transcript

  1. console.log(new Intl.Collator().compare('a', ‘c')); // → a negative value console.log(new Intl.Collator().compare('c',

    'a')); // → a positive value console.log(new Intl.Collator().compare('a', 'a')); // → 0
  2. new Intl.Collator('gr',{sensitivity: ‘base'}) .compare(‘ς', 'σ') // → 0 // ΪϦγϟޠͷγάϚ͸จ຤ͱจதͰจࣈίʔυ͕ҟͳΔ

    // CollatorΛ࢖͏ͱɺsensitivityͷࢦఆͰൺֱͷৄࡉ ౓Λઃఆͯ͜͠ͷΑ͏ͳෳࡶͳൺֱ͕Ͱ͖Δ
  3. var number = 123456.789; // υΠπͰ͸ΧϯϚΛখ਺ɺϐϦΦυΛઍ୯Ґͷ۠੾Γʹ༻͍Δ console.log(new Intl.NumberFormat('de-DE').format(number)); // →

    123.456,789 // ΠϯυͰ͸thousands/lakh/crore۠੾Γ͕༻͍Δ console.log(new Intl.NumberFormat('en-IN').format(number)); // → 1,23,456.789 // nu֦ுΩʔʹΑΓ׽਺ࣈͳͲͷ൪߸ํ͕ࣜ࢖͑Δ console.log(new Intl.NumberFormat(‘zh-Hans-CN-u-nu-hanidec') .format(number)); // → Ұೋࡾ,࢛ޒ࿡.ࣣീ۝
  4. var number = 123456.789; // ௨՟ϑΥʔϚοτΛ༻͍Δ console.log(new Intl.NumberFormat(‘de-DE', { style:

    'currency', currency: 'EUR' } ).format(number)); // → 123.456,79 € console.log(new Intl.NumberFormat(‘ja-JP', { style: 'currency', currency: 'JPY' } ).format(number)); // → ˇ123,457
  5. var pr = new Intl.PluralRules('en-US'); pr.select(0); // → ‘other’ ෳ਺ܗ

    pr.select(1); // → ‘one' ୯਺ܗ pr.select(2); // → ‘other' ෳ਺ܗ
  6. const type = pr.select(count); const wordIndices = {'other': 'dogs', 'one':

    'dog'}; const verbIndices = {'other': 'are', 'one': 'is'}; console.log( `Your ${indices[type]} ${verbIndices[type]} cute.` ) // count === 1 Your dog is cute. // count === 0 || count > 1 Your dogs are cute.
  7. const ordIndices = { 'one': 'st', 'two': 'nd', 'few': 'rd',

    'other': 'th' } const pr = new Intl.PluralRules('en-US', {type: ‘ordinal'} ); const ord = pr.select(3); // → few console.log(`3${ordIndices[ord]}`); // → 3rd
  8. new Intl.DateTimeFormat('ja-JP', { year: 'numeric', month: 'long', day: 'numeric', hour:

    'numeric', minute: 'numeric', second: 'numeric', hour12: false} ).format(new Date()) // → 2019೥8݄27೔ 16:47:50
  9. new Intl.DateTimeFormat('ja-JP', { year: 'numeric', month: 'long', day: 'numeric', hour:

    'numeric', minute: 'numeric', second: 'numeric', hour12: false} ).formatToParts(new Date())
  10. [ { "type": "year", "value": "2019" }, { "type": "literal",

    "value": "೥" }, { "type": "month", "value": "8" }, { "type": "literal", "value": "݄" }, { "type": "day", "value": "27" }, { "type": "literal", "value": "೔ " }, { "type": "hour", "value": "16" }, { "type": "literal", "value": ":" }, { "type": "minute", "value": "57" }, { "type": "literal", "value": ":" }, { "type": "second", "value": "38" } ]