EcmascriptのInternationalization APIについて
Javascript Internationalization API
View Slide
Name!CSO 5BLFUPTIJ"POP੨݈རOccupation'SPOUFOE%FWFMPQFS1SPEVDU0XOFSCompany$ZCFSBHFOU"EUFDI4UVEJP"*.FTTFOHFSOSS$POUSJCVUPSPG7AboutIUUQJOGPCODI
&DNB&DNB͕&DNBͱͯ͠*OUFSOBUJPOBMJ[BUJPO"1*ͷ༷ࡦఆΛߦ͍ͬͯΔ&DNBͰ͋Δ&DNBTDSJQU +BWBTDSJQU͕&DNBΛแ͓ͯ͠Γ&DNBTDSJQUଆʹJOͷ࣮͕ఏڙ͞ΕΔͪͳΈʹ+40/&DNBͰఆٛ͞Ε͓ͯΓɺ&DNBʹแ͞Ε͍ͯΔ
*OUM*OUFSOBMJ[BUJPO"1**OUMͱ͍͏άϩʔόϧΦϒδΣΫτΛެ։͓ͯ͠ΓͦͷΦϒδΣΫτܦ༝Ͱ֤छ"1*ʹΞΫηε͢Δ͜ͱ͕Ͱ͖Δࠓͦͷத͔Βදతͳͭͷ"1*Λհ
*OUM$PMMBUPS
*OUM$PMMBUPS*OUM$PMMBUPSओʹจࣈྻൺֱΛߦ͏ͨΊͷ"1*֤MPDBMFΛؾʹ͠ͳ͕Β6OJDPEFʹجͮ͘ෳࡶͳจࣈൺֱΛߦͬͯ͘ΕΔ
console.log(new Intl.Collator().compare('a', ‘c'));// → a negative valueconsole.log(new Intl.Collator().compare('c', 'a'));// → a positive valueconsole.log(new Intl.Collator().compare('a', 'a'));// → 0
new Intl.Collator('gr',{sensitivity: ‘base'}).compare(‘ς', 'σ')// → 0// ΪϦγϟޠͷγάϚจͱจதͰจࣈίʔυ͕ҟͳΔ// CollatorΛ͏ͱɺsensitivityͷࢦఆͰൺֱͷৄࡉΛઃఆͯ͜͠ͷΑ͏ͳෳࡶͳൺֱ͕Ͱ͖Δ
*OUM/VNCFS'PSNBU
*OUM/VNCFS'PSNBU*OUM/VNCFS'PSNBUMPDBMFʹԠͨ͡ͷॲཧΛߦ͏"1*EFDJNBMͱDVSSFODZʢ͓ۚʣͷͭʹରԠ
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));// → Ұೋࡾ,࢛ޒ.ࣣീ
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
*OUM1MVSBM3VMFT
*OUM/VNCFS'PSNBU୯ޠͷෳܗͷॲཧΛߦ͏"1*
var pr = new Intl.PluralRules('en-US');pr.select(0);// → ‘other’ ෳܗpr.select(1);// → ‘one' ୯ܗpr.select(2);// → ‘other' ෳܗ
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.
const ordIndices = {'one': 'st','two': 'nd','few': 'rd','other': 'th'}const pr = new Intl.PluralRules('en-US',{type: ‘ordinal'});const ord = pr.select(3);// → fewconsole.log(`3${ordIndices[ord]}`);// → 3rd
*OUM%BUFUJNF'PSNBU
*OUM%BUFUJNF'PSNBUMPDBMFʹԠͨ͡ͱ࣌ࠁͷॲཧΛߦ͏"1*
new Intl.DateTimeFormat('ja-JP', {year: 'numeric',month: 'long',day: 'numeric',hour: 'numeric',minute: 'numeric',second: 'numeric',hour12: false}).format(new Date())// → 20198݄27 16:47:50
new Intl.DateTimeFormat('ja-JP', {month: 'long',day: 'numeric',hour: 'numeric',hour12: false}).format(new Date())// → 8݄27 16࣌
*OUM%BUFUJNF'PSNBUGPSNBU5P1BSUTͱ͍͏ϝιουΛ͏͜ͱͰಠࣗͷϑΥʔϚοτ࣮ݱͰ͖Δ
new Intl.DateTimeFormat('ja-JP', {year: 'numeric',month: 'long',day: 'numeric',hour: 'numeric',minute: 'numeric',second: 'numeric',hour12: false}).formatToParts(new Date())
[{"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"}]
·ͱΊ*&͔Β͑ΔͷͰੋඇ͓ࢼ͠Λ