Browse Source

Translation

ErnestoOrtiz2 3 years ago
parent
commit
55745f615f
4 changed files with 35 additions and 1 deletions
  1. 3
    1
      App.js
  2. 3
    0
      locales/en.json
  3. 3
    0
      locales/es.json
  4. 26
    0
      locales/index.js

+ 3
- 1
App.js View File

@@ -2,10 +2,12 @@ import { StatusBar } from 'expo-status-bar';
2 2
 import React from 'react';
3 3
 import { StyleSheet, Text, View } from 'react-native';
4 4
 
5
+import { t } from './locales'
6
+
5 7
 export default function App() {
6 8
   return (
7 9
     <View style={styles.container}>
8
-      <Text>Open up App.js to start working on your app!</Text>
10
+      <Text>{t{name}}</Text>
9 11
       <StatusBar style="auto" />
10 12
     </View>
11 13
   );

+ 3
- 0
locales/en.json View File

@@ -0,0 +1,3 @@
1
+{
2
+    "Hola": "Hello"
3
+}

+ 3
- 0
locales/es.json View File

@@ -0,0 +1,3 @@
1
+{
2
+    "Hello": "Hola"
3
+}

+ 26
- 0
locales/index.js View File

@@ -0,0 +1,26 @@
1
+import I18n from "ex-react-native-i18n";
2
+import { Localization } from 'expo-localization'
3
+import { I18nManager } from "react-native";
4
+
5
+import en from './en.json'
6
+import es from './es.json'
7
+
8
+I18n.translations = {
9
+    en,
10
+    es
11
+}
12
+
13
+const getLanguage = async() => {
14
+ try {   const choice = await Localization.locale
15
+    I18n.locale = choice.substr(0, 2)
16
+    I18n.initAsync()
17
+ }catch(error){
18
+    console.log('Unable to get locale')
19
+ }
20
+}
21
+
22
+getLanguage()
23
+
24
+export function t(name){
25
+    return I18n.t(name)
26
+}