fereron / yii2-js-localize
该包的最新版本(dev-main)没有可用的许可证信息。
JsLocalizator 是一个 Yii2 扩展,旨在将原生的 Yii2 翻译模块移植到 JavaScript。
dev-main
2020-10-19 07:00 UTC
Requires
- yiisoft/yii2: ~2.0
This package is auto-updated.
Last update: 2024-09-19 15:19:24 UTC
README
Yii2-js-localize 是一个 Yii2 扩展,旨在将原生的 Yii2 翻译模块移植到 JavaScript。语言文件可以像 Yii2 服务器端一样发布和使用。目前支持占位符和复数形式(包括表达式)。
用法
在 JavaScript 中使用翻译
Yii.t('app','My non-translated text');
确保有可用的翻译以查看结果。
示例
(为演示目的,默认语言为英语,可以是任何语言)
简单翻译
Yii.t('app','Hello world'); // Hello World
使用自定义语言的简单翻译
Yii.t('app','Hello world','','fr'); // Bonjour tout le monde
Yii.t('app','Hello world','','de'); // Hallo Welt
占位符
Yii.t('app','Hello {name}',{name:'Michael'}); // Hello Michael
多个占位符
Yii.t('app','Hello {firstname} {lastname}', {firstname:'Michael', lastname:'Jackson'}); // Hello Michael Jackson
复数形式
Yii.t('app','Apple|Apples',0); // Apples
Yii.t('app','Apple|Apples',1); // Apple
Yii.t('app','Apple|Apples',2); // Apples
带有占位符的复数形式
Yii.t('app','{n} Apple|{n} Apples',0); // 0 Apples
Yii.t('app','{n} Apple|{n} Apples',1); // 1 Apple
Yii.t('app','{n} Apple|{n} Apples',2); // 2 Apples
带有表达式的复数形式
Yii.t('app','0#No comments, be the first!|1#One comment|n>1#{n} comments',0); // No comments, be the first!
Yii.t('app','0#No comments, be the first!|1#One comment|n>1#{n} comments',1); // One comment
Yii.t('app','0#No comments, be the first!|1#One comment|n>1#{n} comments',2); // 2 comments
带有表达式和占位符的复数形式
Yii.t('app','0#{name} has no mail|1#{name} has one mail|n>1#{name} has {n} mails',{n:0, name:'Pete'}); // Pete has no mail
Yii.t('app','0#{name} has no mail|1#{name} has one mail|n>1#{name} has {n} mails',{n:1, name:'Pete'}); // Pete has one mail
Yii.t('app','0#{name} has no mail|1#{name} has one mail|n>1#{name} has {n} mails',{n:2, name:'Pete'}); // Pete has 2 mails