forked from AlexanderZaytsev/react-native-i18n
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
29 lines (22 loc) · 681 Bytes
/
index.js
File metadata and controls
29 lines (22 loc) · 681 Bytes
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
// @flow
'use strict';
const RNI18n = require('react-native').NativeModules.RNI18n;
const I18nJs = require('i18n-js');
if (typeof RNI18n !== 'undefined') {
I18nJs.locale = RNI18n.languages[0];
} else if (__DEV__) {
console.warn('react-native-i18n module is not correctly linked');
}
export const getLanguages = () => RNI18n.getLanguages();
const replaceWithParams = (text, params) => {
let result = text;
params.forEach((value, index) => {
const reg = new RegExp(`({)${index}(})`, 'g');
result = result.replace(reg, value);
});
return result;
};
I18nJs.tf = (key, params) => {
return replaceWithParams(I18nJs.t(key), params);
};
export default I18nJs;