


Detailed explanation of React-Native global multi-language switching tool library react-native-i18n
This article mainly introduces the detailed explanation of the React-Native global language switching tool library react-native-i18n. The editor thinks it is quite good, so I will share it with you now and give it as a reference. Let’s follow the editor to take a look, I hope it can help everyone.
Attribute | Explanation |
---|---|
All versions | |
iOS+Android | |
Yes | |
Yes | |
No |
Usage:
1.install (omitted, it’s all written in git, just npm things) 2. Used in the project Because they are some static attribute references, you can use redux for storage and replacement, or you can drink them directly (this article takes en and zh as examples). First, build the English version of the configuration file, en/index.jsexport default { home: { greeting: 'Greeting in en', tab_home: 'Home', tab_donate: 'Donate', tab_demo: 'Demo', language: 'language', live_demo: 'Live Demo', buy_me_coffee: 'Buy me a coffee', gitee: 'Gitee', star_me: 'Star me', donate: 'donate', exit: 'exit?', }, donate: { donate: 'donate us~~~', donate_desc: '© 2017 Pactera Technology International Limited. All rights reserved.', }, demo: { dialog: 'dialog', button: 'button', switch: 'switch', action_sheet: 'Action Sheet', } };
export default { home: { greeting: 'Greeting in zh', tab_home: '首页', tab_donate: '捐赠', tab_demo: '例子', language: '语言', live_demo: '例子', buy_me_coffee: '请我一杯coffee', gitee: 'Gitee', star_me: '关注我', donate: '贡献', exit: '是否退出?', }, donate: { donate: '支持我们~~', donate_desc: '© 2017 Pactera Technology International Limited. All rights reserved.', }, demo: { dialog: '提示框', button: '按钮', switch: '开关', action_sheet: '', } };
2.1 Default language environment
We have written 2 language configurations above, so which one is used for initialization? Before calling the business layer, we can make presets firsti18n/index.jsimport i18n from 'react-native-i18n'; import en from './en'; import zh from './zh'; i18n.defaultLocale = 'en'; i18n.fallbacks = true; i18n.translations = { en, zh, }; export {i18n};
ToastAndroid.show(i18n.t('home.exit'),ToastAndroid.SHORT);
Source code analysis
The implementation of this library is divided into two parts, one part is the Native version judgment and other functions and the core implementation of the js part fnando/i18n-jsi18n-js is a lightweight js translation library that supports conversion of various formats and content and switching of language content. The address is as follows: https://github.com/fnando/i18n-jsSo the translation and conversion is done by I18n.js, so what has Native done? Let’s find out (take Android as an example, Apple can’t understand it, sorry)public class RNI18nModule extends ReactContextBaseJavaModule { public RNI18nModule(ReactApplicationContext reactContext) { super(reactContext); } //RN调用的控件名 @Override public String getName() { return "RNI18n"; } //对取出的Locale列表进行格式化的方法 private String toLanguageTag(Locale locale) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { return locale.toLanguageTag(); } StringBuilder builder = new StringBuilder(); builder.append(locale.getLanguage()); if (locale.getCountry() != null) { builder.append("-"); builder.append(locale.getCountry()); } return builder.toString(); } private WritableArray getLocaleList() { WritableArray array = Arguments.createArray(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { //获取区域设置列表。这是获取区域的首选方法。 LocaleList locales = getReactApplicationContext() .getResources().getConfiguration().getLocales(); for (int i = 0; i < locales.size(); i++) { array.pushString(this.toLanguageTag(locales.get(i))); } } else { array.pushString(this.toLanguageTag(getReactApplicationContext() .getResources().getConfiguration().locale)); } return array; } //js端可获取属性的列表 @Override public Map<String, Object> getConstants() { HashMap<String, Object> constants = new HashMap<String,Object>(); constants.put("languages", this.getLocaleList()); return constants; } //提供给js端调用的方法,用来获取默认的语言环境,回调方式用的是promise @ReactMethod public void getLanguages(Promise promise) { try { promise.resolve(this.getLocaleList()); } catch (Exception e) { promise.reject(e); } } }
Copy Code The code is as follows:
Toast.makeText(getReactApplicationContext(),"locales.get(i) "+locales.get(i),Toast.LENGTH_LONG).show();
Summary:
First, Native obtains the LocaleList of this phone, then formats it and takes the first element, which is processed by I18n.js, and then I18n.js selects a set of effective language rules based on the key, and then the process is the same as the order of use.The entire library is less difficult to integrate and easier to use. There are no big pitfalls when using it. It is more delicious when combined with redux.
React-intl implementation of multi-language examples detailed explanation
PHP - implementation of multi-language automatic switching
PHP - Automatic multi-language switching
The above is the detailed content of Detailed explanation of React-Native global multi-language switching tool library react-native-i18n. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



In Debian systems, readdir system calls are used to read directory contents. If its performance is not good, try the following optimization strategy: Simplify the number of directory files: Split large directories into multiple small directories as much as possible, reducing the number of items processed per readdir call. Enable directory content caching: build a cache mechanism, update the cache regularly or when directory content changes, and reduce frequent calls to readdir. Memory caches (such as Memcached or Redis) or local caches (such as files or databases) can be considered. Adopt efficient data structure: If you implement directory traversal by yourself, select more efficient data structures (such as hash tables instead of linear search) to store and access directory information

The readdir function in the Debian system is a system call used to read directory contents and is often used in C programming. This article will explain how to integrate readdir with other tools to enhance its functionality. Method 1: Combining C language program and pipeline First, write a C program to call the readdir function and output the result: #include#include#include#includeintmain(intargc,char*argv[]){DIR*dir;structdirent*entry;if(argc!=2){

Website performance optimization is inseparable from in-depth analysis of access logs. Nginx log records the detailed information of users visiting the website. Cleverly using this data can effectively improve the speed of the website. This article will introduce several website performance optimization methods based on Nginx logs. 1. User behavior analysis and optimization. By analyzing the Nginx log, we can gain a deep understanding of user behavior and make targeted optimization based on this: High-frequency access IP identification: Find the IP address with the highest access frequency, and optimize the server resource configuration for these IP addresses, such as increasing bandwidth or improving the response speed of specific content. Status code analysis: analyze the frequency of different HTTP status codes (such as 404 errors), find out problems in website navigation or content management, and proceed

This article discusses how to improve Hadoop data processing efficiency on Debian systems. Optimization strategies cover hardware upgrades, operating system parameter adjustments, Hadoop configuration modifications, and the use of efficient algorithms and tools. 1. Hardware resource strengthening ensures that all nodes have consistent hardware configurations, especially paying attention to CPU, memory and network equipment performance. Choosing high-performance hardware components is essential to improve overall processing speed. 2. Operating system tunes file descriptors and network connections: Modify the /etc/security/limits.conf file to increase the upper limit of file descriptors and network connections allowed to be opened at the same time by the system. JVM parameter adjustment: Adjust in hadoop-env.sh file

This guide will guide you to learn how to use Syslog in Debian systems. Syslog is a key service in Linux systems for logging system and application log messages. It helps administrators monitor and analyze system activity to quickly identify and resolve problems. 1. Basic knowledge of Syslog The core functions of Syslog include: centrally collecting and managing log messages; supporting multiple log output formats and target locations (such as files or networks); providing real-time log viewing and filtering functions. 2. Install and configure Syslog (using Rsyslog) The Debian system uses Rsyslog by default. You can install it with the following command: sudoaptupdatesud

This article describes how to clean useless software packages and free up disk space in the Debian system. Step 1: Update the package list Make sure your package list is up to date: sudoaptupdate Step 2: View installed packages Use the following command to view all installed packages: dpkg--get-selections|grep-vdeinstall Step 3: Identify redundant packages Use the aptitude tool to find packages that are no longer needed. aptitude will provide suggestions to help you safely delete packages: sudoaptitudesearch '~pimportant' This command lists the tags

In Debian systems, OpenSSL is an important library for encryption, decryption and certificate management. To prevent a man-in-the-middle attack (MITM), the following measures can be taken: Use HTTPS: Ensure that all network requests use the HTTPS protocol instead of HTTP. HTTPS uses TLS (Transport Layer Security Protocol) to encrypt communication data to ensure that the data is not stolen or tampered during transmission. Verify server certificate: Manually verify the server certificate on the client to ensure it is trustworthy. The server can be manually verified through the delegate method of URLSession

The steps to install an SSL certificate on the Debian mail server are as follows: 1. Install the OpenSSL toolkit First, make sure that the OpenSSL toolkit is already installed on your system. If not installed, you can use the following command to install: sudoapt-getupdatesudoapt-getinstallopenssl2. Generate private key and certificate request Next, use OpenSSL to generate a 2048-bit RSA private key and a certificate request (CSR): openss
