lancetw/react-isomorphic-bundle

View on GitHub
src/shared/utils/locale-utils.js

Summary

Maintainability
A
55 mins
Test Coverage
import { includes } from 'lodash'
export const supportedList = ['zh-hant-tw', 'zh-hant-cn', 'en']
import counterpart from 'counterpart'

const msgList = {
  'email must be unique': 'message.email.unique'
}

export function messageI18n (text) {
  if (text in msgList) {
    return counterpart(msgList[text])
  }

  return text
}

export function fallBackLocale (locale) {
  if (!locale) return locale

  const defaultLocale = supportedList[0]

  if (includes(supportedList, locale)) {
    return locale
  }

  return defaultLocale
}

export function fixLocaleName (locale) {
  if (!locale) return locale

  if (locale === 'zh-hant-tw') {
    return 'zh-TW'
  }
  if (locale === 'zh-hant-cn') {
    return 'zh-CN'
  }

  return locale
}

export function originLocaleName (locale) {
  if (!locale) return locale

  if (locale.startsWith('en')) {
    return 'en'
  }

  if (locale === 'zh-TW' || locale === 'zh-tw') {
    return 'zh-hant-tw'
  }

  if (locale === 'zh-CN' || locale === 'zh-cn') {
    return 'zh-hant-cn'
  }

  return fallBackLocale(locale)
}