Skip to content

Instantly share code, notes, and snippets.

@cayasso
Last active November 2, 2018 19:00
Show Gist options
  • Select an option

  • Save cayasso/7ddb3a88dae30ab828a80c18a05b290d to your computer and use it in GitHub Desktop.

Select an option

Save cayasso/7ddb3a88dae30ab828a80c18a05b290d to your computer and use it in GitHub Desktop.

Revisions

  1. cayasso revised this gist Nov 2, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion i18n.js
    Original file line number Diff line number Diff line change
    @@ -42,7 +42,7 @@ i18n.set = locale => {
    if (!i18n.db[locale]) {
    i18n.db[locale] = i18n.locales[locale]
    }
    i18n.locale = lang
    i18n.locale = locale
    }

    i18n.db = {}
  2. cayasso revised this gist Nov 2, 2018. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions i18n.js
    Original file line number Diff line number Diff line change
    @@ -35,12 +35,12 @@ const i18n = (tpl, ...values) => {
    return str.replace(/\${(\d)}/g, (_, i) => values[Number(i)])
    }

    i18n.set = lang => {
    if (!lang) return
    i18n.set = locale => {
    if (!locale) return
    i18n.db = i18n.db || {}

    if (!i18n.db[lang]) {
    i18n.db[lang] = i18n.locales[lang]
    if (!i18n.db[locale]) {
    i18n.db[locale] = i18n.locales[locale]
    }
    i18n.locale = lang
    }
  3. cayasso renamed this gist Nov 2, 2018. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. cayasso revised this gist Nov 2, 2018. 2 changed files with 17 additions and 0 deletions.
    13 changes: 13 additions & 0 deletions component.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    import React from 'react'
    import { Text, View } from 'react-native'
    import i18n from 'lib/i18n
    const name = 'Norman Ramirez'

    export default () => (
    <View>
    <Text>{i18n`Hi how are you ${name}`}</Text>
    <Text>{i18n`I am fine`}</Text>
    <Text>Your locale is {i18n.locale}</Text>
    </View>
    )
    4 changes: 4 additions & 0 deletions es.json
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    {
    "Hi how are you ${0}": "Hola como estas ${0}",
    "I am fine": "Estoy bien"
    }
  5. cayasso created this gist Nov 2, 2018.
    57 changes: 57 additions & 0 deletions i18n.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,57 @@
    import { Platform, NativeModules } from 'react-native'
    import es from '../translations/es.json'

    const init = () => {
    i18n.set(getLocale())
    }

    const getLocale = () => {
    const locale = Platform.selectt({
    android: NativeModules.I18nManager.localeIdentifier,
    ios: NativeModules.SettingsManager.settings.AppleLocale
    })
    return locale.substring(0, 2) || i18n.locale
    }

    const getKey = tpl => {
    const key = (acc, str, i) => `${str}\${${i}}${acc}`
    return tpl
    .slice(0, -1)
    .reduceRight(key, tpl[tpl.length - 1])
    .replace(/\r\n/g, '\n')
    }

    const i18n = (tpl, ...values) => {
    const key = getKey(tpl)
    const db = (i18n.db || {})[i18n.locale] || {}
    const str = db[key]
    if (!str) {
    const out = [tpl[0]]
    for (let i = 0, l = values.length; i < l; ++i) {
    out.push(values[i], tpl[i + 1])
    }
    return out.join('')
    }
    return str.replace(/\${(\d)}/g, (_, i) => values[Number(i)])
    }

    i18n.set = lang => {
    if (!lang) return
    i18n.db = i18n.db || {}

    if (!i18n.db[lang]) {
    i18n.db[lang] = i18n.locales[lang]
    }
    i18n.locale = lang
    }

    i18n.db = {}
    i18n.locale = 'en'
    i18n.locales = {
    en: {},
    es: es.default
    }

    init()

    export default i18n