Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

locale / i18n #158

Open
magicdawn opened this issue Mar 30, 2023 · 3 comments
Open

locale / i18n #158

magicdawn opened this issue Mar 30, 2023 · 3 comments

Comments

@magicdawn
Copy link
Owner

magicdawn commented Mar 30, 2023

规范

https://unicode-org.github.io/icu/userguide/locale/#the-locale-concept

ISO 639: language code

细分

  • ISO 639-1, 两个字母, 例如中文 zh
  • ISO 639-2, 三个字母, 例如中文 zho 或者 chi

ISO 3166: country / region code

https://www.iso.org/obp/ui/#search

locale

A locale identifies a specific user community - a group of users who have similar culture and language expectations for human-computer interaction (and the kinds of data they process).

Intl API

DOC https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Intl#locales_%E5%8F%82%E6%95%B0

BCP-47
https://datatracker.ietf.org/doc/html/rfc5646#appendix-A

language-tag = 
  language
  ["-" script]
  ["-" region]

大小写不敏感, 只是习惯将 Script / REGION

  • zh-Hans: zh 是 language-code, Hans 是 script (书写形式)
  • zh-Hans-CN: CN 是 country-region-code, 在中国大陆地区使用简体书写的中文
  • zh-CN: 在中国大陆地区使用的中文
  • zh-Hans-TW: 这个有没有呢~

humanize-duration

https://www.npmjs.com/package/humanize-duration#supported-languages

这是个简单的库, 写死了一些 language 的翻译, 需要传一个 ISO 639-1 的 language code, 但是中文特殊 zh_CN / zh_TW 中文这两个不是 language code, 更像 locale

@magicdawn
Copy link
Owner Author

magicdawn commented Mar 30, 2023

node humanize duration

TLDR

  • dayjs.duration 不准确, 秒级的都是返回 a few seconds
  • luxon 麻烦, 需要先 rescale, toHumanIntl.NumberFormat / Intl.ListFormat 控制, 毫秒位不能删除, 需要自己转成秒再传给 Duration.fromObject({seconds: Math.round(constms / 1000))
  • 还是 humanize-duration 方便
const locale = new Intl.DateTimeFormat().resolvedOptions().locale
const lang = locale.startsWith('zh')
  ? locale.replace(/-/, '_')
  : locale.split('-')[0]

// use humanize
import humanize from 'humanize-duration'
export function getDurationDisplay(costms: number) {
  return humanize(costms, {
    language: lang,
    fallbacks: ['en'],
    round: true,
  })
}

// or use humanizer
// 在 type:module 项目中用上面 `humanize` , cjs dep in esm project
import {humanizer} from 'humanize-duration'
export const getDurationDisplay = humanizer({     
  language: lang,
  fallbacks: ['en'],
  round: true, 
})

with humanize-duration

// locale: en-US / zh-CN / zh-TW
// lang: en / zh_CN / zh_TW
import { humanizer } from 'humanize-duration'
import { sync as osLocaleSync } from 'os-locale'
const locale = osLocaleSync()
const lang = locale.startsWith('zh') ? locale.replace(/-/, '_') : locale.split('-')[0]
const getDurationDisplay = humanizer({ language: lang, fallbacks: ['en'], round: true })

with luxon

import {Duration} from 'luxon'

// 太长了, 连毫秒都带出来了
const str = Duration.fromObject({ seconds: Math.round(10000121111 / 1000) })
  .rescale()
  .toHuman({ unitDisplay: 'short' })
// e.g 1小时、1分钟、10秒钟

@magicdawn
Copy link
Owner Author

获取 locale

node os-locale

https://npm.im/os-locale

Intl Api

https://github.com/moment/luxon/blob/3.3.0/src/impl/locale.js#L59

const locale = Intl.DateTimeFormat().resolvedOptions().locale

@magicdawn
Copy link
Owner Author

magicdawn commented Apr 7, 2023

Intl.ListFormat

image

好像中间的 separator 没地方修改, zh-CN , en-US ,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant