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

The Future of Frontend i18n : Intl.MessageFormat

Saji
August 24, 2024

The Future of Frontend i18n : Intl.MessageFormat

フロントエンドカンファレンス北海道 2024 : 2023/08/24
- https://fortee.jp/frontend-conf-hokkaido-2024/proposal/31eab828-f20d-4f9d-87ae-e2d92c790dfb
X
- https://twitter.com/sajikix

Saji

August 24, 2024
Tweet

More Decks by Saji

Other Decks in Programming

Transcript

  1. .'ͷߏจෳ਺ύλʔϯϚον .BUDIFS Ϛον͍ͨࣜ͠PSม਺ͷࢀরΛ૿΍͢͜ͱ͕Ͱ͖Δ w ͜ͷ৔߹ύλʔϯ΋ͦͷ਺ʹԠͯ͡૿΍ͤΔ w $PNQMFY .input {$numLikes :integer}

    .input {$numShares :integer} .match {$numLikes} {$numShares} // match৚͕݅2ͭ 0 0 {{Your item has no likes and has not been shared.}} 0 one {{Your item has no likes and has been shared …}} 0 * {{Your item has no likes and has been shared …}} // … ҎԼશύλʔϯྻڍ(͜ͷྫͩͱ3x3ͷ9ύλʔϯ)
  2. *OUMଆͰͷࢦఆؔ਺Λొ࿥ ؔ਺͸.FTTBHF'PSNBUίϯετϥΫλॳظԽ࣌ʹࢦఆ͢Δ const message = "logged in at {$date :myDate

    type=short}"; const mf = new Intl.MessageFormat("en", message, { functions: { myDate: (…) => {…} }, }); const logMes = mf.format({ date: new Date() }); // logged in at 2024/08/23
  3. *OUM.FTTBHF'PSNBUQBSTF3FTPVSDF Ұൠతͳจݴ؅ཧͰ͸จݴϦιʔεΛύʔε͠ɺཉ͍͠จݴΛLFZͰऔಘ͢Δ w Α͋͘ΔHFU.FTTBHF lOPUJ fi DBUJPO.FTTBHFz Έ͍ͨͳ΍ͭ *OUM.FTTBHF'PSNBUͰ΋͜Ε͕Ͱ͖ͯཉ͍͠ͱ͍͏ఏҊ w

    *OUM.FTTBHF'PSNBUຊମͱ͸෼͚ͯఏҊ͞Ε͓ͯΓɺݱࡏ4UBHF ͨͩ͠ɺͦ΋ͦ΋ʮϦιʔεϑΝΠϧʯͷඪ४͕ܾ·͍ͬͯͳ͍ w Ͳ͏΍ͬͯෳ਺ͷ.FTTBHFΛྻڍ͢Δͷ͔  w ίϝϯτ͸ʁηΫγϣϯͰ۠੾Γʁϝλ৘ใ͸ʁFUD
  4. *OUM.FTTBHF'PSNBUQBSTF3FTPVSDF Πϝʔδ ࠓޙશવมΘΔՄೳੑ͋Γ  const source = "…"; // string

    source const resource = Intl.MessageFormat.parseResource(source, [ "en", ]); const greeting = resource .get("greeting") .resolveMessage({ place: "world" }); greeting.toString(); // 'Hello world!'