Last active
November 2, 2018 19:00
-
-
Save cayasso/7ddb3a88dae30ab828a80c18a05b290d to your computer and use it in GitHub Desktop.
Revisions
-
cayasso revised this gist
Nov 2, 2018 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 = locale } i18n.db = {} -
cayasso revised this gist
Nov 2, 2018 . 1 changed file with 4 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 = locale => { if (!locale) return i18n.db = i18n.db || {} if (!i18n.db[locale]) { i18n.db[locale] = i18n.locales[locale] } i18n.locale = lang } -
cayasso renamed this gist
Nov 2, 2018 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
cayasso revised this gist
Nov 2, 2018 . 2 changed files with 17 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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> ) This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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" } -
cayasso created this gist
Nov 2, 2018 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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