javascript internationalization API

javascript internationalization API

EcmascriptのInternationalization APIについて

Transcript

  1. 7.

    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. 8.

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

    // CollatorΛ࢖͏ͱɺsensitivityͷࢦఆͰൺֱͷৄࡉ ౓Λઃఆͯ͜͠ͷΑ͏ͳෳࡶͳൺֱ͕Ͱ͖Δ
  3. 11.

    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. 12.

    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. 15.

    var pr = new Intl.PluralRules('en-US'); pr.select(0); // → ‘other’ ෳ਺ܗ

    pr.select(1); // → ‘one' ୯਺ܗ pr.select(2); // → ‘other' ෳ਺ܗ
  6. 16.

    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. 17.

    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. 20.

    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. 23.

    new Intl.DateTimeFormat('ja-JP', { year: 'numeric', month: 'long', day: 'numeric', hour:

    'numeric', minute: 'numeric', second: 'numeric', hour12: false} ).formatToParts(new Date())
  10. 24.

    [ { "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" } ]