在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):stefalda/ReactNativeLocalization开源软件地址(OpenSource Url):https://github.com/stefalda/ReactNativeLocalization开源编程语言(OpenSource Language):JavaScript 27.2%开源软件介绍(OpenSource Introduction):ReactNativeLocalizationClass to localize the ReactNative interface. Use react-localization if you want to share code with a React project or the localized-strings for a generic javascript solution. Note about version 1.xThis library has been refactored to use the newly created localized-strings package, now added as a dependency, so to unify the code and make it easier to mantain All the basic code is now in the localized-strings project and in the react-localization version that adds support for embedding JSX code in the formatted strings, by overriding the formatString method. This version adds a custom version of the To simplify Android versions' configuration, versions 2.0 and up are compatible only with ReactNative >= 0.56.0 What it doesI just needed a dead simple way to internationalize my first React Native app. At the beginning I thought I'd expose the native iOS internationalization API (NSLocalizedString macro) to React Native, but then I've opted for a solution that seems, at least to me, more in the spirit of React (and I hope better performance wise). In this implementation we can keep the localized strings in the same file of the React View in a similar way of how Styles are implemented (I don't deny that this approach could lead to some duplications in the translated strings, but it could be feasible to create a CommonJS module to use as common source of the strings, requiring it in the different views). Beware Expo created apps need to be ejected before integrating native plugins like this one. So if you've used the Create React Native app shortcut you should eject the app as detailed here here. How it worksThe Javascript library uses a native library (ReactLocalization) to get the current interface language, then it loads and displays the strings matching the current interface locale or the default language (the first one if a match is not found) if a specific localization can't be found. It's possible to force a language different from the interface one. InstallationThe easiest way to install is to type just 2 commands inside your react-native project folder and you are ready to go:
Don´t forget to restart the app / node server or you will see an error. If you're installing for Android and still experiencing problems check if step 4 of "Manual installation Android" has been automatically executed by the linker. Check this article about the new linking behaviour in react-native since version 0.60. Windows platform doesn't support automatic installation by linker. Only manual installation is supported. Manual installation iOS
Manual installation Android
(Thanks to @rebeccahughes for showing by example how to create an android module for React Native) Manual installation windowsFull process is documented in official React Native plugin for Universal Windows repo: https://github.com/Microsoft/react-native-windows/blob/master/docs/LinkingLibrariesWindows.md
UsageIn the React class that you want to localize require the library and define the strings object passing to the constructor a simple object containing a language key (i.e. en, it, fr..) and then a list of key-value pairs with the needed localized strings. // ES6 module syntax
import LocalizedStrings from 'react-native-localization';
// CommonJS syntax
// let LocalizedStrings = require ('react-native-localization');
let strings = new LocalizedStrings({
"en-US":{
how:"How do you want your egg today?",
boiledEgg:"Boiled egg",
softBoiledEgg:"Soft-boiled egg",
choice:"How to choose the egg"
},
en:{
how:"How do you want your egg today?",
boiledEgg:"Boiled egg",
softBoiledEgg:"Soft-boiled egg",
choice:"How to choose the egg"
},
it: {
how:"Come vuoi il tuo uovo oggi?",
boiledEgg:"Uovo sodo",
softBoiledEgg:"Uovo alla coque",
choice:"Come scegliere l'uovo"
}
}); Then use the <Text style={styles.title}>
{strings.how}
</Text> The first language is considered the default one, so if a translation is missing for the selected language, the default one is shown and a line is written to the log as a reminder. Update / Overwrite LocaleYou might have default localized in the build but then download the latest localization strings from a server. Use setContent to overwrite the whole object. NOTE that this will remove all other localizations if used. strings.setContent({
en:{
how:"How do you want your egg todajsie?",
boiledEgg:"Boiled eggsie",
softBoiledEgg:"Soft-boiled egg",
choice:"How to choose the egg"
}
}) You can also only overwrite a specific language using strings.setContent(Object.assign({},strings.getContent(),
{
en:{
how:"How do you want your egg todajsie?",
boiledEgg:"Boiled eggsie",
softBoiledEgg:"Soft-boiled egg",
choice:"How to choose the egg"
}
})); TypescriptFor TypeScript, your {
"compilerOptions": {
"target": "es2015",
"module": "es2015",
"jsx": "react-native",
"moduleResolution": "node",
"allowSyntheticDefaultImports": true
}
} Where Import should be done like this: import LocalizedString from "react-native-localization"; API
en:{
bread:"bread",
butter:"butter",
question:"I'd like {0} and {1}, or just {0}"
}
...
strings.formatString(strings.question, strings.bread, strings.butter) Beware: do not define a string key as
ExamplesTo force a particular language use something like this: _onSetLanguageToItalian() {
strings.setLanguage('it');
this.setState({});
} It's also possible to set the language directly in your Xcode project using the following code snippet: [[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"de", nil] forKey:@"AppleLanguages"]; Replace Check out the WIKI page for additional informations. Questions or suggestions?Feel free to contact me on Twitter or open an issue. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论