-
Notifications
You must be signed in to change notification settings - Fork 0
/
native.js
68 lines (49 loc) · 1.46 KB
/
native.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
'use strict';
import { NativeModules } from 'react-native'
const { RNLanguages } = NativeModules;
var locale = RNLanguages?RNLanguages.language:`en_US`
if ( !locale.includes(`_`) || !locale.includes(`-`) )
locale = `en_US`
if ( locale.includes(`-`) )
locale = locale.replace(`-`, `_`)
var directory = `locales`
var file = `main`
var maps = []
const setLocale = ( l ) => locale = l
const setDirectory = ( d ) => directory = d
const setFile = ( f ) => file = f
const getDirectory = () => directory
const getFile = () => file
const getLocale = () => locale
const setMaps = ( main_locale, locales = [] ) => maps.push({[main_locale]:[ ...locales]})
const map = ( l ) =>
{
for ( let [okey, ovalue] of Object.entries(maps))
for ( let [lkey, lvalue] of Object.entries(ovalue))
if ( lvalue.indexOf(l) !== -1 )
l = lkey
return l
}
const load = ( my_locale ) =>
{
let _locale = my_locale || getLocale()
_locale = map(_locale)
try
{
const translations = require('../../locales/index')
return translations[`${_locale}_${getFile()}`] || {}
}
catch (e)
{
return {}
}
}
const t = ( text, arr = {}, locale = null ) =>
{
const translator = load(locale)
text = translator[text] || text
for ( let key in arr )
text = text.replace(`{${key}}`, arr[key])
return text
}
module.exports = { t, setLocale, setDirectory, setFile, getDirectory, getFile, getLocale, setMaps }