Skip to content

Commit 09b39e6

Browse files
committed
fix: fix i18n defaultLocaleName
1 parent 137bee9 commit 09b39e6

File tree

4 files changed

+161
-154
lines changed

4 files changed

+161
-154
lines changed

docs/apis/ScriptAPI/i18n.md

Lines changed: 79 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,74 @@
11
# 🛫 Internationalization API
22

3-
As our community gets bigger and bigger, language becomes a barrier for users and players from different countries to communicate. To change this, we made `I18nAPI` and encourage plugin creators use it.
3+
As our community gets bigger and bigger, language becomes a barrier for users and players from different countries to
4+
communicate. To change this, we made `I18nAPI` and encourage plugin creators use it.
45

56
## Load translation data
67

78
`i18n.load(path, defaultLocaleName, defaultLangData)`
89

9-
1010
- Params:
11-
- path: `String`
12-
The file[^1]/directory[^2] where the translation data is located
13-
- defaultLocaleName: `String`
14-
**This parameter is deprecated since 0.9.0, it will follows LeviLamina's language**. default locale name, in the form of `zh_CN` or `en` (this parameter will be used if the target language is not provided for the translation of `i18n.tr` or `i18n.get`)
15-
If an empty string is passed in, the default follows the system language
16-
- defaultLangData: `Object`
17-
This parameter will be used to complete or create translation files in the form of
18-
```js
19-
{
20-
localeName: {
21-
"src": "translation"
22-
},
23-
en_CN: {
24-
"a.b.c.d.114514": "1919810",
25-
"src": "source"
11+
- path: `String`
12+
The file[^1]/directory[^2] where the translation data is located
13+
- defaultLocaleName: `String`
14+
default locale name, in the form of `zh_CN` or `en` (this parameter will be used if the target language is not
15+
provided for the translation of `i18n.tr` or `i18n.get`)
16+
If an empty string is passed in, the default follows the system language
17+
- defaultLangData: `Object`
18+
This parameter will be used to complete or create translation files in the form of
19+
```js
20+
{
21+
localeName: {
22+
"src": "translation"
23+
},
24+
en_CN: {
25+
"a.b.c.d.114514": "1919810",
26+
"src": "source"
27+
}
2628
}
27-
}
28-
```
29+
```
2930
- Throw:
30-
- Invalid parameter
31+
- Invalid parameter
3132

3233
[^1]: the file must be in JSON format and the content of the file should be similar to
34+
3335
```json
3436
{
35-
"localeName": {
36-
"src": "translation"
37-
},
38-
"zh_CN": {
39-
"a.b.c.d.114514": "1919810",
40-
"src": "源"
41-
}
37+
"localeName": {
38+
"src": "translation"
39+
},
40+
"zh_CN": {
41+
"a.b.c.d.114514": "1919810",
42+
"src": "源"
4243
}
44+
}
4345
```
4446

4547
[^2]: Please add the path separator (`/` or `\\\`) at the end of the path.
46-
The contents of the directory should be similar to.
48+
The contents of the directory should be similar to.
49+
4750
```
4851
LOCALE_name.json
4952
[Dir] ┼ en.json
5053
zh_CN.json
5154
```
52-
Each file inside should be similar to:
55+
56+
Each file inside should be similar to:
57+
5358
```json
5459
{
55-
"a.c.b.d": "translation",
56-
"use": {
57-
"nested": {
58-
"src": "translation"
59-
},
60-
"114514": {
61-
"1919810": "heng heng aaaaaaaaaaaaaaaaaaaaaaaaa",
62-
"a": "此处的ID为use.nested.114514.a",
63-
"b": "The ID of this will be use.nested.114514.b"
64-
}
60+
"a.c.b.d": "translation",
61+
"use": {
62+
"nested": {
63+
"src": "translation"
64+
},
65+
"114514": {
66+
"1919810": "heng heng aaaaaaaaaaaaaaaaaaaaaaaaa",
67+
"a": "此处的ID为use.nested.114514.a",
68+
"b": "The ID of this will be use.nested.114514.b"
6569
}
6670
}
71+
}
6772
```
6873
6974
<br/>
@@ -73,13 +78,13 @@ As our community gets bigger and bigger, language becomes a barrier for users an
7378
`i18n.get(key[,localeName])`
7479
7580
- Params:
76-
- key: `String`
77-
Text or ID
78-
- localeName: `String`(Optional)
79-
Target language, default is the `defaultLocaleName` passed in when `i18n.load`.
81+
- key: `String`
82+
Text or ID
83+
- localeName: `String`(Optional)
84+
Target language, default is the `defaultLocaleName` passed in when `i18n.load`.
8085
- Return value: `String` translation (if no translation is found after multiple fallbacks, then `key` is returned)
8186
- Throw:
82-
- Invalid parameter
87+
- Invalid parameter
8388
8489
<br/>
8590
@@ -88,16 +93,16 @@ As our community gets bigger and bigger, language becomes a barrier for users an
8893
`i18n.trl(localeName, key, ...) `
8994
9095
- Parameters:
91-
- localeName: `String`
92-
Target language
93-
- key: `String`
94-
Text or ID
95-
- ... : `Any`
96-
Format arguments
96+
- localeName: `String`
97+
Target language
98+
- key: `String`
99+
Text or ID
100+
- ... : `Any`
101+
Format arguments
97102
- Return value: `String` translated and formatted text
98103
- Note: Formatting should follow [syntax](https://fmt.dev/latest/syntax.html)
99104
- Throw:
100-
- Invalid parameter
105+
- Invalid parameter
101106
102107
<br/>
103108
@@ -106,49 +111,51 @@ As our community gets bigger and bigger, language becomes a barrier for users an
106111
`i18n.tr(key, ...) `
107112
108113
- Parameters:
109-
- key: `String`
110-
Text or ID
111-
- ... : `Any`
112-
Format arguments
114+
- key: `String`
115+
Text or ID
116+
- ... : `Any`
117+
Format arguments
113118
- Return value: `String` translated and formatted text
114119
- Note: Formatting should follow [syntax](https://fmt.dev/latest/syntax.html)
115120
- Throw:
116-
- Invalid parameter
121+
- Invalid parameter
117122
118123
<br/>
119124
120125
## Full sample
121126
122127
```js
123128
i18n.load("plugins/feature-llse-i18n-test/language.json", "" /* "zh_CN" */, {
124-
"zh_CN": {
125-
"{1}, {0}, named_arg(float.2): {named_arg:.2f}": "测试: {1}, {0}, named_arg(float.2): {named_arg:.2f}",
126-
"{} {} {a} {b}": "测试2: {} {} {a} {b}",
127-
"welcome": "Hi, {}! Welcome to the server!"
128-
},
129-
"en": {
130-
"welcome": "Hi, {}! Welcome to the SERVER!"
131-
}
129+
"zh_CN": {
130+
"{1}, {0}, named_arg(float.2): {named_arg:.2f}": "测试: {1}, {0}, named_arg(float.2): {named_arg:.2f}",
131+
"{} {} {a} {b}": "测试2: {} {} {a} {b}",
132+
"welcome": "Hi, {}! Welcome to the server!"
133+
},
134+
"en": {
135+
"welcome": "Hi, {}! Welcome to the SERVER!"
136+
}
132137
});
133138

134139
LLSE_Player.prototype._sendText = LLSE_PLayer.prototype.sendText;
135-
LLSE_Player.prototype.sendText = function() {
136-
let args = Array.from(arguments);
137-
let text = args.shift();
138-
pl._sendText(i18n.trl(this.langCode, text, . . args));
140+
LLSE_Player.prototype.sendText = function () {
141+
let args = Array.from(arguments);
142+
let text = args.shift();
143+
pl._sendText(i18n.trl(this.langCode, text,..args)
144+
)
145+
;
139146
}
140147

141148
logger.info(tr("{1}, {0}, named_arg(float.2): {named_arg:.2f}", "string0", 1, {
142-
named_arg: 114.51419 // Named arguments should be object
149+
named_arg: 114.51419 // Named arguments should be object
143150
}));
144151
// Out: test: 1, string0, named_arg(float.2): 114.51
145152

146153
logger.info(tr("{} {} {a} {b}", "arg0", "arg1", {
147-
a: "114514",
148-
b: "1919810"
154+
a: "114514",
155+
b: "1919810"
149156
}));
150157

151158
mc.listen("onJoin", (pl) => {
152-
pl.sendText("welcome", pl.realName);
159+
pl.sendText("welcome", pl.realName);
153160
});
154161
```

0 commit comments

Comments
 (0)