Keep languages constants for faster I18n init

This commit is contained in:
Mathieu Acthernoene 2017-05-19 14:57:55 +02:00
parent 0ca088ec8f
commit de7bcfef94
4 changed files with 52 additions and 21 deletions

View File

@ -10,7 +10,9 @@ import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.WritableArray;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
public class RNI18nModule extends ReactContextBaseJavaModule {
@ -39,20 +41,32 @@ public class RNI18nModule extends ReactContextBaseJavaModule {
return builder.toString();
}
private WritableArray getLocaleList() {
LocaleList locales = Build.VERSION.SDK_INT >= Build.VERSION_CODES.N
? getReactApplicationContext().getResources().getConfiguration().getLocales()
: new LocaleList(getReactApplicationContext().getResources().getConfiguration().locale);
WritableArray array = Arguments.createArray();
for (int i = 0; i < locales.size(); i++) {
array.pushString(this.toLanguageTag(locales.get(i)));
}
return array;
}
@Override
public Map<String, Object> getConstants() {
HashMap<String, Object> constants = new HashMap<String,Object>();
constants.put("languages", this.getLocaleList());
return constants;
}
@ReactMethod
public void getLanguages(Promise promise) {
try {
LocaleList locales = Build.VERSION.SDK_INT >= Build.VERSION_CODES.N
? getReactApplicationContext().getResources().getConfiguration().getLocales()
: new LocaleList(getReactApplicationContext().getResources().getConfiguration().locale);
WritableArray languages = Arguments.createArray();
for (int i = 0; i < locales.size(); i++) {
languages.pushString(this.toLanguageTag(locales.get(i)));
}
promise.resolve(languages);
promise.resolve(this.getLocaleList());
} catch (Exception e) {
promise.reject(e);
}

View File

@ -1,7 +1,9 @@
// @ flow
import React, { Component } from 'react';
import { getLanguages } from 'react-native-i18n';
// import { languages, getLanguages } from 'react-native-i18n';
import { NativeModules } from 'react-native';
const { getLanguages, languages } = NativeModules.RNI18n;
import {
Platform,
@ -23,7 +25,14 @@ export default class extends Component {
render() {
return (
<ScrollView style={styles.container}>
<Text style={styles.title}>Methods</Text>
<Text style={styles.title}>Internal constants</Text>
<View style={styles.line}>
<Text style={styles.label}>languages: </Text>
<Text>{JSON.stringify(languages)}</Text>
</View>
<Text style={styles.title}>Internal methods</Text>
<View style={styles.line}>
<Text style={styles.label}>getLanguages (Promise): </Text>
@ -48,6 +57,7 @@ const styles = StyleSheet.create({
},
line: {
flexDirection: 'row',
marginBottom: 16,
},
label: {
fontWeight: '700',

View File

@ -6,6 +6,7 @@ const { RNI18n } = NativeModules;
// I18n.locale = RNI18n.language;
export const getLanguages = RNI18n.getLanguages;
// export const getLanguages = RNI18n.getLanguages;
// export const languages = RNI18n.languages;
export default I18n;

View File

@ -15,15 +15,21 @@ RCT_EXPORT_MODULE();
return languageTags;
}
RCT_EXPORT_METHOD(getLanguages:(RCTPromiseResolveBlock)resolve
rejecter:(__unused RCTPromiseRejectBlock)reject) {
- (NSArray *)getPreferredLanguages {
NSArray *preferredLanguages = [NSLocale preferredLanguages];
resolve(
[[[UIDevice currentDevice] systemVersion] floatValue] >= 9
? preferredLanguages
: [self toLanguageTags:preferredLanguages]
);
return [[[UIDevice currentDevice] systemVersion] floatValue] >= 9
? preferredLanguages
: [self toLanguageTags:preferredLanguages];
}
- (NSDictionary *)constantsToExport {
return @{ @"languages": [self getPreferredLanguages] };
}
RCT_EXPORT_METHOD(getLanguages:(RCTPromiseResolveBlock)resolve
rejecter:(__unused RCTPromiseRejectBlock)reject) {
resolve([self getPreferredLanguages]);
}
@end