npm i --save tns-i18n-deep
- 版本:0.0.13
- GitHub: https://github.com/kodaren/tns-i18n-deep
- NPM: https://npmjs.net.cn/package/tns-i18n-deep
- 下载
- 昨天: 0
- 上周: 0
- 上个月: 0
tns-i18n-deep
这是一个实现国际化的 Nativescript 插件,它支持深度嵌套的本地化字符串。
它将 _L
函数添加到全局变量和 XML 文件中,因此您可以在应用的任何地方使用它。
它基于 tns-i18n 模块,唯一的区别是此模块支持深度嵌套字符串,因此您更容易组织您的本地化。
模块会检测首选用户语言,如果应用可以处理该语言,则显示该语言中的文本;否则,将加载您选择的默认语言中的文本。
安装
tns plugin add tns-i18n-deep
用法
导入
在其他任何内容之前,在 app.js 中导入该模块。下面是示例:
import Vue from 'nativescript-vue';
import HelloWorld from './components/HelloWorld';
//> You have to mention the default language code
const i18n = require('tns-i18n-deep')('en');
// Uncommment the following to see NativeScript-Vue output logs
//Vue.config.silent = false;
new Vue({
i18n,
render: h => h('frame', [h(HelloWorld)])
}).$start();
```javascript
## Creating locales
Now, create a **i18n** folder in the **app** folder.
app |___ i18n |___ en.js |___ sv.js . . . . . .
### Must export in files
The files containing the strings, must export an object of the strings.
Eg: **en.js**
```javascript
module.exports = {
greetings: 'Hello %s',
bye: 'Nice to meet you %s, see you in %s days',
weekdays: {
monday: "Monday",
tuesday: "Tuesday",
tuesday: "Wednesday",
}
};
加载字符串
XML
XML 文件中
<Label text="{{_L('greetings')}}"></Label>
JS
_L("greetings")
带有深度字符串的 JS
_L("weekdays.monday")
替换
该模块支持 %s
的替换。
例如
<Label text="{{_L('greetings', 'My friend')}}"></Label>
<!-- Will give: Hello My friend -->
多重替换
对于多重替换,您可以使用数组、单独的参数,甚至两者都使用。
例如
<Label text="{{_L('bye', 'My friend', '5')}}"></Label>
<!-- Will give: Nice to meet you My friend, see you in 5 days -->
或者
<Label text="{{_L('bye', ['My friend', '5'])}}"></Label>
<!-- Will give: Nice to meet you My friend, see you in 5 days -->