Skip to main content

Internationalization

  • XLIFF: XML Localization Interchange File Format.
  • ICU: International Components for Unicode.
  • BCP 47: IETF BCP 47 language tag.

i18n Implementation

// locale/zh-CN.js

export default {
hello: '你好,{name}',
}
// locale/en-GB.js

export default {
hello: 'Hello,{name}',
}
import IntlMessageFormat from 'intl-messageformat'
import en from '../locale/en'
import zh from '../locale/zh'

const MESSAGES = { en, zh }
const LOCALE = 'en' // 这里写上决定语言的方法,例如可以从 cookie 判断语言

class Intl {
get(key, defaultMessage, options) {
let msg = MESSAGES[LOCALE][key]

if (msg == null) {
if (defaultMessage != null)
return defaultMessage

return key
}

if (options) {
msg = new IntlMessageFormat(msg, LOCALE)
return msg.format(options)
}

return msg
}
}

export default Intl

i18n Library

i18n Solution