Switch to Promise getLanguages on iOS instead of constants

This commit is contained in:
Mathieu Acthernoene 2017-05-17 12:48:51 +02:00
parent 9111605ed7
commit 958ae660e6
4 changed files with 44 additions and 43 deletions

View File

@ -36,7 +36,7 @@
2DCD954D1E0B4F2C00145EB5 /* RNI18nExampleTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 00E356F21AD99517003FC87E /* RNI18nExampleTests.m */; }; 2DCD954D1E0B4F2C00145EB5 /* RNI18nExampleTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 00E356F21AD99517003FC87E /* RNI18nExampleTests.m */; };
5E9157361DD0AC6A00FF2AA8 /* libRCTAnimation.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 5E9157331DD0AC6500FF2AA8 /* libRCTAnimation.a */; }; 5E9157361DD0AC6A00FF2AA8 /* libRCTAnimation.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 5E9157331DD0AC6500FF2AA8 /* libRCTAnimation.a */; };
832341BD1AAA6AB300B99B32 /* libRCTText.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 832341B51AAA6A8300B99B32 /* libRCTText.a */; }; 832341BD1AAA6AB300B99B32 /* libRCTText.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 832341B51AAA6A8300B99B32 /* libRCTText.a */; };
DE458F75AAAA4EB4BDBF8FD4 /* libRNI18n.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 10E778393CB046649BBFBD2B /* libRNI18n.a */; }; ABFA6573F7664C81811507CA /* libRNI18n.a in Frameworks */ = {isa = PBXBuildFile; fileRef = FE588EF9ECCA427F8B433FD5 /* libRNI18n.a */; };
/* End PBXBuildFile section */ /* End PBXBuildFile section */
/* Begin PBXContainerItemProxy section */ /* Begin PBXContainerItemProxy section */
@ -215,9 +215,9 @@
remoteGlobalIDString = 2D2A28201D9B03D100D4039D; remoteGlobalIDString = 2D2A28201D9B03D100D4039D;
remoteInfo = "RCTAnimation-tvOS"; remoteInfo = "RCTAnimation-tvOS";
}; };
66372B7B1E95BCCF004C6B85 /* PBXContainerItemProxy */ = { 66044F041ECC61DB00C25374 /* PBXContainerItemProxy */ = {
isa = PBXContainerItemProxy; isa = PBXContainerItemProxy;
containerPortal = 556FAA27631847E0A7BD01A8 /* RNI18n.xcodeproj */; containerPortal = A4AC77F56F3944BE96E4C85C /* RNI18n.xcodeproj */;
proxyType = 2; proxyType = 2;
remoteGlobalIDString = 134814201AA4EA6300B7C361; remoteGlobalIDString = 134814201AA4EA6300B7C361;
remoteInfo = RNI18n; remoteInfo = RNI18n;
@ -248,7 +248,6 @@
00E356EE1AD99517003FC87E /* RNI18nExampleTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = RNI18nExampleTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 00E356EE1AD99517003FC87E /* RNI18nExampleTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = RNI18nExampleTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
00E356F11AD99517003FC87E /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; }; 00E356F11AD99517003FC87E /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
00E356F21AD99517003FC87E /* RNI18nExampleTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RNI18nExampleTests.m; sourceTree = "<group>"; }; 00E356F21AD99517003FC87E /* RNI18nExampleTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RNI18nExampleTests.m; sourceTree = "<group>"; };
10E778393CB046649BBFBD2B /* libRNI18n.a */ = {isa = PBXFileReference; explicitFileType = undefined; fileEncoding = 9; includeInIndex = 0; lastKnownFileType = archive.ar; path = libRNI18n.a; sourceTree = "<group>"; };
139105B61AF99BAD00B5F7CC /* RCTSettings.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = RCTSettings.xcodeproj; path = "../node_modules/react-native/Libraries/Settings/RCTSettings.xcodeproj"; sourceTree = "<group>"; }; 139105B61AF99BAD00B5F7CC /* RCTSettings.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = RCTSettings.xcodeproj; path = "../node_modules/react-native/Libraries/Settings/RCTSettings.xcodeproj"; sourceTree = "<group>"; };
139FDEE61B06529A00C62182 /* RCTWebSocket.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = RCTWebSocket.xcodeproj; path = "../node_modules/react-native/Libraries/WebSocket/RCTWebSocket.xcodeproj"; sourceTree = "<group>"; }; 139FDEE61B06529A00C62182 /* RCTWebSocket.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = RCTWebSocket.xcodeproj; path = "../node_modules/react-native/Libraries/WebSocket/RCTWebSocket.xcodeproj"; sourceTree = "<group>"; };
13B07F961A680F5B00A75B9A /* RNI18nExample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = RNI18nExample.app; sourceTree = BUILT_PRODUCTS_DIR; }; 13B07F961A680F5B00A75B9A /* RNI18nExample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = RNI18nExample.app; sourceTree = BUILT_PRODUCTS_DIR; };
@ -261,10 +260,11 @@
146833FF1AC3E56700842450 /* React.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = React.xcodeproj; path = "../node_modules/react-native/React/React.xcodeproj"; sourceTree = "<group>"; }; 146833FF1AC3E56700842450 /* React.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = React.xcodeproj; path = "../node_modules/react-native/React/React.xcodeproj"; sourceTree = "<group>"; };
2D02E47B1E0B4A5D006451C7 /* RNI18nExample-tvOS.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "RNI18nExample-tvOS.app"; sourceTree = BUILT_PRODUCTS_DIR; }; 2D02E47B1E0B4A5D006451C7 /* RNI18nExample-tvOS.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "RNI18nExample-tvOS.app"; sourceTree = BUILT_PRODUCTS_DIR; };
2D02E4901E0B4A5D006451C7 /* RNI18nExample-tvOSTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "RNI18nExample-tvOSTests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; }; 2D02E4901E0B4A5D006451C7 /* RNI18nExample-tvOSTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "RNI18nExample-tvOSTests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
556FAA27631847E0A7BD01A8 /* RNI18n.xcodeproj */ = {isa = PBXFileReference; explicitFileType = undefined; fileEncoding = 9; includeInIndex = 0; lastKnownFileType = "wrapper.pb-project"; name = RNI18n.xcodeproj; path = "../node_modules/react-native-i18n/ios/RNI18n.xcodeproj"; sourceTree = "<group>"; };
5E91572D1DD0AC6500FF2AA8 /* RCTAnimation.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = RCTAnimation.xcodeproj; path = "../node_modules/react-native/Libraries/NativeAnimation/RCTAnimation.xcodeproj"; sourceTree = "<group>"; }; 5E91572D1DD0AC6500FF2AA8 /* RCTAnimation.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = RCTAnimation.xcodeproj; path = "../node_modules/react-native/Libraries/NativeAnimation/RCTAnimation.xcodeproj"; sourceTree = "<group>"; };
78C398B01ACF4ADC00677621 /* RCTLinking.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = RCTLinking.xcodeproj; path = "../node_modules/react-native/Libraries/LinkingIOS/RCTLinking.xcodeproj"; sourceTree = "<group>"; }; 78C398B01ACF4ADC00677621 /* RCTLinking.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = RCTLinking.xcodeproj; path = "../node_modules/react-native/Libraries/LinkingIOS/RCTLinking.xcodeproj"; sourceTree = "<group>"; };
832341B01AAA6A8300B99B32 /* RCTText.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = RCTText.xcodeproj; path = "../node_modules/react-native/Libraries/Text/RCTText.xcodeproj"; sourceTree = "<group>"; }; 832341B01AAA6A8300B99B32 /* RCTText.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = RCTText.xcodeproj; path = "../node_modules/react-native/Libraries/Text/RCTText.xcodeproj"; sourceTree = "<group>"; };
A4AC77F56F3944BE96E4C85C /* RNI18n.xcodeproj */ = {isa = PBXFileReference; explicitFileType = undefined; fileEncoding = 9; includeInIndex = 0; lastKnownFileType = "wrapper.pb-project"; name = RNI18n.xcodeproj; path = "../node_modules/react-native-i18n/ios/RNI18n.xcodeproj"; sourceTree = "<group>"; };
FE588EF9ECCA427F8B433FD5 /* libRNI18n.a */ = {isa = PBXFileReference; explicitFileType = undefined; fileEncoding = 9; includeInIndex = 0; lastKnownFileType = archive.ar; path = libRNI18n.a; sourceTree = "<group>"; };
/* End PBXFileReference section */ /* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */ /* Begin PBXFrameworksBuildPhase section */
@ -291,7 +291,7 @@
832341BD1AAA6AB300B99B32 /* libRCTText.a in Frameworks */, 832341BD1AAA6AB300B99B32 /* libRCTText.a in Frameworks */,
00C302EA1ABCBA2D00DB3ED1 /* libRCTVibration.a in Frameworks */, 00C302EA1ABCBA2D00DB3ED1 /* libRCTVibration.a in Frameworks */,
139FDEF61B0652A700C62182 /* libRCTWebSocket.a in Frameworks */, 139FDEF61B0652A700C62182 /* libRCTWebSocket.a in Frameworks */,
DE458F75AAAA4EB4BDBF8FD4 /* libRNI18n.a in Frameworks */, ABFA6573F7664C81811507CA /* libRNI18n.a in Frameworks */,
); );
runOnlyForDeploymentPostprocessing = 0; runOnlyForDeploymentPostprocessing = 0;
}; };
@ -435,10 +435,10 @@
name = Products; name = Products;
sourceTree = "<group>"; sourceTree = "<group>";
}; };
66372B5F1E95BCCE004C6B85 /* Products */ = { 66044EE81ECC61DB00C25374 /* Products */ = {
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
66372B7C1E95BCCF004C6B85 /* libRNI18n.a */, 66044F051ECC61DB00C25374 /* libRNI18n.a */,
); );
name = Products; name = Products;
sourceTree = "<group>"; sourceTree = "<group>";
@ -466,7 +466,7 @@
832341B01AAA6A8300B99B32 /* RCTText.xcodeproj */, 832341B01AAA6A8300B99B32 /* RCTText.xcodeproj */,
00C302DF1ABCB9EE00DB3ED1 /* RCTVibration.xcodeproj */, 00C302DF1ABCB9EE00DB3ED1 /* RCTVibration.xcodeproj */,
139FDEE61B06529A00C62182 /* RCTWebSocket.xcodeproj */, 139FDEE61B06529A00C62182 /* RCTWebSocket.xcodeproj */,
556FAA27631847E0A7BD01A8 /* RNI18n.xcodeproj */, A4AC77F56F3944BE96E4C85C /* RNI18n.xcodeproj */,
); );
name = Libraries; name = Libraries;
sourceTree = "<group>"; sourceTree = "<group>";
@ -659,8 +659,8 @@
ProjectRef = 146833FF1AC3E56700842450 /* React.xcodeproj */; ProjectRef = 146833FF1AC3E56700842450 /* React.xcodeproj */;
}, },
{ {
ProductGroup = 66372B5F1E95BCCE004C6B85 /* Products */; ProductGroup = 66044EE81ECC61DB00C25374 /* Products */;
ProjectRef = 556FAA27631847E0A7BD01A8 /* RNI18n.xcodeproj */; ProjectRef = A4AC77F56F3944BE96E4C85C /* RNI18n.xcodeproj */;
}, },
); );
projectRoot = ""; projectRoot = "";
@ -835,11 +835,11 @@
remoteRef = 5E9157341DD0AC6500FF2AA8 /* PBXContainerItemProxy */; remoteRef = 5E9157341DD0AC6500FF2AA8 /* PBXContainerItemProxy */;
sourceTree = BUILT_PRODUCTS_DIR; sourceTree = BUILT_PRODUCTS_DIR;
}; };
66372B7C1E95BCCF004C6B85 /* libRNI18n.a */ = { 66044F051ECC61DB00C25374 /* libRNI18n.a */ = {
isa = PBXReferenceProxy; isa = PBXReferenceProxy;
fileType = archive.ar; fileType = archive.ar;
path = libRNI18n.a; path = libRNI18n.a;
remoteRef = 66372B7B1E95BCCF004C6B85 /* PBXContainerItemProxy */; remoteRef = 66044F041ECC61DB00C25374 /* PBXContainerItemProxy */;
sourceTree = BUILT_PRODUCTS_DIR; sourceTree = BUILT_PRODUCTS_DIR;
}; };
78C398B91ACF4ADC00677621 /* libRCTLinking.a */ = { 78C398B91ACF4ADC00677621 /* libRCTLinking.a */ = {

View File

@ -1,7 +1,7 @@
// @ flow // @ flow
import React, { Component } from 'react'; import React, { Component } from 'react';
import { language, languages } from 'react-native-i18n'; import { getLanguages } from 'react-native-i18n';
import { import {
Platform, Platform,
@ -12,19 +12,22 @@ import {
} from 'react-native'; } from 'react-native';
export default class extends Component { export default class extends Component {
state = { languages: [] }
componentWillMount() {
getLanguages().then(languages => {
this.setState({ languages })
})
}
render() { render() {
return ( return (
<ScrollView style={styles.container}> <ScrollView style={styles.container}>
<Text style={styles.title}>Constants</Text> <Text style={styles.title}>Methods</Text>
<View style={styles.line}> <View style={styles.line}>
<Text style={styles.label}>language: </Text> <Text style={styles.label}>getLanguages (Promise): </Text>
<Text>{JSON.stringify(language)}</Text> <Text>{JSON.stringify(this.state.languages)}</Text>
</View>
<View style={styles.line}>
<Text style={styles.label}>languages: </Text>
<Text>{JSON.stringify(languages)}</Text>
</View> </View>
</ScrollView> </ScrollView>
); );

View File

@ -4,9 +4,8 @@ import { NativeModules } from 'react-native';
import I18n from 'i18n-js'; import I18n from 'i18n-js';
const { RNI18n } = NativeModules; const { RNI18n } = NativeModules;
I18n.locale = RNI18n.language; // I18n.locale = RNI18n.language;
export const language = RNI18n.language; export const getLanguages = RNI18n.getLanguages;
export const languages = RNI18n.languages;
export default I18n; export default I18n;

View File

@ -5,26 +5,25 @@
RCT_EXPORT_MODULE(); RCT_EXPORT_MODULE();
- (NSDictionary *)constantsToExport { - (NSMutableArray *)toLanguageTags:(NSArray *)languages {
NSMutableArray *languageTags = [NSMutableArray array];
for (id l in languages) {
[languageTags addObject:[l stringByReplacingOccurrencesOfString:@"_" withString:@"-"]];
}
return languageTags;
}
RCT_EXPORT_METHOD(getLanguages:(RCTPromiseResolveBlock)resolve
rejecter:(__unused RCTPromiseRejectBlock)reject) {
NSArray *preferredLanguages = [NSLocale preferredLanguages]; NSArray *preferredLanguages = [NSLocale preferredLanguages];
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 9) { resolve(
return @{ [[[UIDevice currentDevice] systemVersion] floatValue] >= 9
@"language": [preferredLanguages objectAtIndex:0], ? preferredLanguages
@"languages": preferredLanguages : [self toLanguageTags:preferredLanguages]
}; );
}
NSMutableArray *forcedLanguageTags = [NSMutableArray array];
for (id l in preferredLanguages) {
[forcedLanguageTags addObject:[l stringByReplacingOccurrencesOfString:@"_" withString:@"-"]];
}
return @{
@"language": [forcedLanguageTags objectAtIndex:0],
@"languages": forcedLanguageTags
};
} }
@end @end