ilib-loctool-webos-rust 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +16 -0
- package/LICENSE +201 -0
- package/README.md +87 -2
- package/RustFile.js +434 -0
- package/RustFileType.js +401 -0
- package/package.json +65 -3
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# ilib-loctool-webos-rust
|
|
2
|
+
|
|
3
|
+
## 1.0.0
|
|
4
|
+
|
|
5
|
+
### Major Changes
|
|
6
|
+
|
|
7
|
+
- 6a936b1: Initial release: new plugin to extract and localize strings from Rust (`.rs`) source files for the webOS platform. It extracts the string literals passed to the `get_loc_string()` and `get_loc_string_with_key()` method calls on the resource bundle (e.g. `bundle.get_loc_string("Yes")`). The output path of the JSON resource files is configured with `settings.rust.mappings` in `project.json`, similar to the mappings of the Dart plugin. Only the Rust files that match one of the mappings are extracted.
|
|
8
|
+
|
|
9
|
+
Rust unicode escapes (`\u{XXXX}`) are converted to the `\uXXXX` form used in the xliff files, and line continuations in string literals are removed in the same way as rustc does.
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- Updated dependencies [cf383ea]
|
|
14
|
+
- Updated dependencies [5875507]
|
|
15
|
+
- ilib-loctool-webos-common@1.3.0
|
|
16
|
+
- ilib-loctool-webos-json-resource@1.7.11
|
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,88 @@
|
|
|
1
|
-
#
|
|
1
|
+
# ilib-loctool-webos-rust
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
ilib-loctool-webos-rust is a plugin for loctool that allows it to read and localize Rust files. This plugin is optimized for the webOS platform.
|
|
4
|
+
### Rust FileType
|
|
5
|
+
This plugin extracts the strings passed to the two methods of the resource bundle, as shown in the examples below.
|
|
6
|
+
```rust
|
|
7
|
+
bundle.get_loc_string("Yes");
|
|
8
|
+
bundle.get_loc_string_with_key("PictureMode.Standard", "Standard");
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Note: The following are valid Rust but are not extracted:
|
|
12
|
+
* Strings that are not literals at the call site, such as constants or variables
|
|
13
|
+
(e.g. `bundle.get_loc_string(STANDARD)`).
|
|
14
|
+
* Path calls such as `ResBundle::get_loc_string(&bundle, "Yes")`.
|
|
15
|
+
* Raw string literals such as `r#"..."#`.
|
|
16
|
+
* Calls with an `i18n` comment between the arguments. Put the `i18n` comment after the call instead
|
|
17
|
+
(see below).
|
|
18
|
+
```rust
|
|
19
|
+
bundle.get_loc_string(
|
|
20
|
+
"Power", // i18n Power button
|
|
21
|
+
);
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
#### Comments for translators
|
|
25
|
+
An `i18n` comment on the same line as the call is extracted as the comment of the resource, so that
|
|
26
|
+
translators can see it.
|
|
27
|
+
```rust
|
|
28
|
+
bundle.get_loc_string("OK"); // i18n button
|
|
29
|
+
bundle.get_loc_string("Cancel"); /* i18n cancel button */
|
|
30
|
+
bundle.get_loc_string("Power"); // i18n : Power button
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
### Resource file path
|
|
34
|
+
The location of the generated JSON resource files is configured with `mappings` in the `rust` section of
|
|
35
|
+
the `settings` in your `project.json` file. Each key of `mappings` is a glob pattern that is matched against
|
|
36
|
+
the path of the Rust source file, and the `template` of the first matching pattern decides the output path.
|
|
37
|
+
Only the Rust source files that match one of the patterns are localized. Rust files that do not match any
|
|
38
|
+
pattern are not extracted. In generate mode, there is no source file to match, so the first mapping is used.
|
|
39
|
+
|
|
40
|
+
Note: The JSON resource file type keeps one resource file per locale. If several mappings are configured,
|
|
41
|
+
all strings of a locale are written to the path of the mapping that is used first for that locale.
|
|
42
|
+
|
|
43
|
+
```json
|
|
44
|
+
"settings": {
|
|
45
|
+
"rust": {
|
|
46
|
+
"mappings": {
|
|
47
|
+
"**/*.rs": {
|
|
48
|
+
"template": "[resourceDir]/[localeDir]/rsstrings.json"
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
If no `mappings` are configured, or `mappings` is an empty object, the default above is used,
|
|
56
|
+
which generates the files below:
|
|
57
|
+
```
|
|
58
|
+
resources/rsstrings.json (en-US)
|
|
59
|
+
resources/ko/rsstrings.json (ko-KR)
|
|
60
|
+
resources/en/GB/rsstrings.json (en-GB)
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
The template supports the substitutions of loctool's path templates, such as `[dir]`, `[resourceDir]`,
|
|
64
|
+
`[locale]`, `[language]`, `[region]`, `[localeUnder]`. The `[localeDir]` keyword follows the webOS
|
|
65
|
+
resource directory layout:
|
|
66
|
+
* `en-US` is written at the root (the keyword becomes an empty string).
|
|
67
|
+
* A language default locale uses the language only (e.g. `ko-KR` -> `ko`).
|
|
68
|
+
* Other locales use each part of the locale as a directory (e.g. `en-GB` -> `en/GB`).
|
|
69
|
+
|
|
70
|
+
In the same way, `[locale]` and `[localeUnder]` use the language only for a language default locale
|
|
71
|
+
(e.g. `ko-KR` -> `ko`, `en-US` -> `en`), and the full locale for the others (e.g. `en-GB` -> `en-GB` / `en_GB`).
|
|
72
|
+
|
|
73
|
+
Note: In generate mode, there is no source file, so `[dir]` becomes an empty string. If the template
|
|
74
|
+
uses `[dir]`, the resource files of generate mode are written to a different path than in localize mode.
|
|
75
|
+
|
|
76
|
+
#### Sample
|
|
77
|
+
The simple sample is provided in the [samples-loctool](https://github.com/iLib-js/ilib-mono-webos/tree/main/packages/samples-loctool).
|
|
78
|
+
|
|
79
|
+
## License
|
|
80
|
+
|
|
81
|
+
Copyright (c) 2026, JEDLSoft
|
|
82
|
+
|
|
83
|
+
This plugin is licensed under Apache2. See the [LICENSE](https://github.com/iLib-js/ilib-mono-webos/blob/main/packages/ilib-loctool-webos-rust/LICENSE)
|
|
84
|
+
file for more details.
|
|
85
|
+
|
|
86
|
+
## Release Notes
|
|
87
|
+
|
|
88
|
+
See the [CHANGELOG.md](https://github.com/iLib-js/ilib-mono-webos/blob/main/packages/ilib-loctool-webos-rust/CHANGELOG.md) file.
|
package/RustFile.js
ADDED
|
@@ -0,0 +1,434 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* RustFile.js - plugin to extract resources from a Rust source code file
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2026 JEDLSoft
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
*
|
|
16
|
+
* See the License for the specific language governing permissions and
|
|
17
|
+
* limitations under the License.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
var fs = require("fs");
|
|
21
|
+
var path = require("path");
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Create a new Rust file with the given path name and within
|
|
25
|
+
* the given project.
|
|
26
|
+
*
|
|
27
|
+
* @param {Project} project the project object
|
|
28
|
+
* @param {String} pathName path to the file relative to the root
|
|
29
|
+
* of the project file
|
|
30
|
+
* @param {FileType} type the file type of this instance
|
|
31
|
+
*/
|
|
32
|
+
var RustFile = function(props) {
|
|
33
|
+
this.project = props.project;
|
|
34
|
+
this.pathName = props.pathName;
|
|
35
|
+
this.type = props.type;
|
|
36
|
+
this.API = props.project.getAPI();
|
|
37
|
+
this.logger = this.API.getLogger("loctool.plugin.webOSRustFile");
|
|
38
|
+
this.set = this.API.newTranslationSet(this.project ? this.project.sourceLocale : "zxx-XX");
|
|
39
|
+
this.mapping = this.type && this.type.getMapping(this.pathName);
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Format a UTF-16 code unit as a JS style unicode escape (\uXXXX).
|
|
44
|
+
*
|
|
45
|
+
* @private
|
|
46
|
+
* @param {Number} unit the UTF-16 code unit
|
|
47
|
+
* @returns {String} the escape sequence
|
|
48
|
+
*/
|
|
49
|
+
function toJsEscape(unit) {
|
|
50
|
+
return "\\u" + ("0000" + unit.toString(16).toUpperCase()).slice(-4);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Convert the Rust unicode escapes (\u{X} .. \u{XXXXXX}) in the given string
|
|
55
|
+
* to the JS style escapes (\uXXXX) used by the other webOS plugins and in the
|
|
56
|
+
* existing xliff files, so that the same key is generated for the same string.
|
|
57
|
+
*
|
|
58
|
+
* - A code point in the BMP (up to U+FFFF) keeps the hex digits as written.
|
|
59
|
+
* They are only padded to 4 digits, and the case is kept
|
|
60
|
+
* (\u{28} -> \u0028, \u{201e} -> \u201e), because the keys are compared as
|
|
61
|
+
* text and the existing xliff files mix upper and lower case.
|
|
62
|
+
* - A code point outside the BMP does not fit in 4 hex digits, so it is
|
|
63
|
+
* converted to a different value: a surrogate pair of two UTF-16 code units
|
|
64
|
+
* (\u{1F600} -> \uD83D\uDE00). The hex digits of the pair are computed, not
|
|
65
|
+
* copied, so they are always in upper case, whatever the case of the
|
|
66
|
+
* original digits (\u{1f600} -> \uD83D\uDE00).
|
|
67
|
+
* - Underscores between the hex digits are removed, as rustc does
|
|
68
|
+
* (\u{1F_600} -> \uD83D\uDE00, \u{00_E9} -> \u00E9).
|
|
69
|
+
* - An escaped backslash followed by "u{...}" (\\u{41}) is literal text and
|
|
70
|
+
* is left unchanged.
|
|
71
|
+
* - Invalid escapes (no hex digits, a leading underscore, more than 6 digits,
|
|
72
|
+
* above U+10FFFF) are left unchanged; they do not compile in Rust anyway.
|
|
73
|
+
*
|
|
74
|
+
* @static
|
|
75
|
+
* @param {String} string the string to convert
|
|
76
|
+
* @returns {String} the string with JS style unicode escapes
|
|
77
|
+
*/
|
|
78
|
+
RustFile.convertUnicodeEscapes = function(string) {
|
|
79
|
+
if (!string) return string;
|
|
80
|
+
return string.replace(/(\\*)\\u\{([0-9A-Fa-f][0-9A-Fa-f_]*)\}/g, function(match, backslashes, digits) {
|
|
81
|
+
// an even number of preceding backslashes means this backslash is not escaped
|
|
82
|
+
if (backslashes.length % 2 !== 0) return match;
|
|
83
|
+
// Rust allows underscores between the hex digits (\u{1F_600});
|
|
84
|
+
// they are not counted in the 6 digit limit
|
|
85
|
+
var hex = digits.replace(/_/g, "");
|
|
86
|
+
if (hex.length > 6) return match;
|
|
87
|
+
var codePoint = parseInt(hex, 16);
|
|
88
|
+
if (codePoint > 0x10FFFF) return match;
|
|
89
|
+
if (codePoint <= 0xFFFF) {
|
|
90
|
+
return backslashes + "\\u" + ("0000" + hex).slice(-4);
|
|
91
|
+
}
|
|
92
|
+
codePoint -= 0x10000;
|
|
93
|
+
return backslashes +
|
|
94
|
+
toJsEscape(0xD800 + (codePoint >> 10)) +
|
|
95
|
+
toJsEscape(0xDC00 + (codePoint & 0x3FF));
|
|
96
|
+
});
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Remove the line continuations in the given string. In a Rust string literal,
|
|
101
|
+
* a backslash at the end of a line removes the newline and all the whitespace
|
|
102
|
+
* at the beginning of the next line:
|
|
103
|
+
*
|
|
104
|
+
* "Connect a LAN cable \
|
|
105
|
+
* to the TV." -> "Connect a LAN cable to the TV."
|
|
106
|
+
*
|
|
107
|
+
* An escaped backslash at the end of a line ("\\" + newline) is literal text
|
|
108
|
+
* followed by a real newline, and is left unchanged.
|
|
109
|
+
*
|
|
110
|
+
* @static
|
|
111
|
+
* @param {String} string the string to convert
|
|
112
|
+
* @returns {String} the string without line continuations
|
|
113
|
+
*/
|
|
114
|
+
RustFile.removeLineContinuations = function(string) {
|
|
115
|
+
if (!string) return string;
|
|
116
|
+
return string.replace(/(\\*)\\\r?\n[ \t\r\n]*/g, function(match, backslashes) {
|
|
117
|
+
// an even number of preceding backslashes means this backslash is not escaped
|
|
118
|
+
if (backslashes.length % 2 !== 0) return match;
|
|
119
|
+
return backslashes;
|
|
120
|
+
});
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Make the key of the given string literal text. This does not make the same
|
|
125
|
+
* string that would be in memory in Rust: only some escapes are unescaped.
|
|
126
|
+
*
|
|
127
|
+
* - The quotes and backslashes (\" \' \\) and \n, \t, \v are unescaped.
|
|
128
|
+
* - Line continuations (a backslash at the end of a line) are removed.
|
|
129
|
+
* See {@link RustFile.removeLineContinuations}.
|
|
130
|
+
* - Rust unicode escapes (\u{XXXX}) are not decoded to the characters. They
|
|
131
|
+
* are converted to the JS style escape text (\uXXXX), as in the existing
|
|
132
|
+
* xliff files, so that the keys match them. The key is compared as text, so
|
|
133
|
+
* "\u{AC00}" and the literal character U+AC00 give different keys.
|
|
134
|
+
* See {@link RustFile.convertUnicodeEscapes}.
|
|
135
|
+
*
|
|
136
|
+
* @static
|
|
137
|
+
* @param {String} string the string literal text to make a key of
|
|
138
|
+
* @returns {String} the key
|
|
139
|
+
*/
|
|
140
|
+
RustFile.unescapeString = function(string) {
|
|
141
|
+
// Convert Rust unicode escapes first, before the backslashes are unescaped,
|
|
142
|
+
// so that an escaped backslash ("\\u{...}") can still be told apart from a
|
|
143
|
+
// real escape.
|
|
144
|
+
var unescaped = RustFile.convertUnicodeEscapes(RustFile.removeLineContinuations(string));
|
|
145
|
+
unescaped = unescaped.
|
|
146
|
+
replace(/^\\\\/, "\\"). // unescape backslashes
|
|
147
|
+
replace(/([^\\])\\\\/g, "$1\\").
|
|
148
|
+
replace(/([^\\])\\"/g, '$1"').
|
|
149
|
+
replace(/\\"/g, '"').
|
|
150
|
+
replace(/\\'/g, "'").
|
|
151
|
+
replace(/\\n/g, "\n").
|
|
152
|
+
replace(/\\t/g, "\t").
|
|
153
|
+
replace(/\\v/g, "\v");
|
|
154
|
+
|
|
155
|
+
return unescaped;
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Use a key for the given string as it is. Not manipulating at all.
|
|
160
|
+
*
|
|
161
|
+
* @private
|
|
162
|
+
* @param {String} source the source string to make a resource
|
|
163
|
+
* key for
|
|
164
|
+
* @returns {String} a unique key for this string
|
|
165
|
+
*/
|
|
166
|
+
RustFile.prototype.makeKey = function(source) {
|
|
167
|
+
return RustFile.unescapeString(source);
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Trim the comment markers and the leading colon from the text of an
|
|
172
|
+
* i18n comment, so that only the comment for the translators remains.
|
|
173
|
+
* For example, ": Power button" becomes "Power button".
|
|
174
|
+
*
|
|
175
|
+
* @static
|
|
176
|
+
* @param {String|undefined} commentString the text that follows the
|
|
177
|
+
* i18n keyword in the comment
|
|
178
|
+
* @returns {String|undefined} the trimmed comment, or undefined if no
|
|
179
|
+
* comment string is given or nothing remains after trimming
|
|
180
|
+
*/
|
|
181
|
+
RustFile.trimComment = function(commentString) {
|
|
182
|
+
if (!commentString) return;
|
|
183
|
+
|
|
184
|
+
// Only the markers at the beginning (a colon, slashes or stars) and the
|
|
185
|
+
// end ("*/") are removed, so that a colon, slash or star in the middle of
|
|
186
|
+
// the comment (e.g. "Format: HH:MM", "a/b") is kept.
|
|
187
|
+
var trimComment = commentString.
|
|
188
|
+
replace(/\s*\*+\/\s*$/, "").
|
|
189
|
+
replace(/^[\s\/*]*:?\s*/, "").
|
|
190
|
+
replace(/\s+$/, "");
|
|
191
|
+
return trimComment || undefined;
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Find the end of the block comment that starts at the given index. Block
|
|
196
|
+
* comments can be nested in Rust, so each "/*" inside the comment needs its
|
|
197
|
+
* own "*\/":
|
|
198
|
+
*
|
|
199
|
+
* /* outer /* inner *\/ still in the comment *\/
|
|
200
|
+
*
|
|
201
|
+
* @private
|
|
202
|
+
* @param {String} data the data that is being parsed
|
|
203
|
+
* @param {Number} start the index of the "/*" that opens the comment
|
|
204
|
+
* @returns {Number} the index just after the "*\/" that closes the comment,
|
|
205
|
+
* or the length of the data if the comment is not closed
|
|
206
|
+
*/
|
|
207
|
+
RustFile.findBlockCommentEnd = function(data, start) {
|
|
208
|
+
var depth = 0;
|
|
209
|
+
var i = start;
|
|
210
|
+
while (i < data.length - 1) {
|
|
211
|
+
if (data[i] === "/" && data[i + 1] === "*") {
|
|
212
|
+
depth++;
|
|
213
|
+
i += 2;
|
|
214
|
+
} else if (data[i] === "*" && data[i + 1] === "/") {
|
|
215
|
+
depth--;
|
|
216
|
+
i += 2;
|
|
217
|
+
if (depth === 0) return i;
|
|
218
|
+
} else {
|
|
219
|
+
i++;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
return data.length;
|
|
223
|
+
};
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* Remove single and multi-lines comments except for i18n comment style.
|
|
227
|
+
*
|
|
228
|
+
* @private
|
|
229
|
+
* @param {String} data the string to clean
|
|
230
|
+
* @returns {String} the cleaned string
|
|
231
|
+
*/
|
|
232
|
+
RustFile.removeCommentLines = function(data) {
|
|
233
|
+
if (!data) return;
|
|
234
|
+
// Remove comments while preserving string and character literals.
|
|
235
|
+
// Match char literals, string literals, // line comments and /* */ block
|
|
236
|
+
// comments as whole tokens in one left-to-right scan. The literals and the
|
|
237
|
+
// i18n comments are kept, and the other comments are removed.
|
|
238
|
+
// The i18n comments must be matched as tokens too: otherwise the scan
|
|
239
|
+
// continues inside them, and a double quote in the comment text
|
|
240
|
+
// (e.g. `// i18n: 5" screen`) opens a fake string literal that runs
|
|
241
|
+
// across the following lines and hides the comments there.
|
|
242
|
+
// A line comment ends before the line break ("\n" or "\r\n") or at the end
|
|
243
|
+
// of the data, and the line break itself is kept, so that the lines are not
|
|
244
|
+
// joined. A block comment can be nested, which a regex cannot count, so the
|
|
245
|
+
// regex matches only its "/*" and RustFile.findBlockCommentEnd() finds the
|
|
246
|
+
// matching "*/".
|
|
247
|
+
var reToken = /'(\\.|[^'\\])'|"(\\[\s\S]|[^"\\])*"|\/\/[^\r\n]*|\/\*/g;
|
|
248
|
+
var result = "";
|
|
249
|
+
var last = 0;
|
|
250
|
+
var match, token;
|
|
251
|
+
while ((match = reToken.exec(data)) !== null) {
|
|
252
|
+
token = match[0];
|
|
253
|
+
if (token === "/*") {
|
|
254
|
+
reToken.lastIndex = RustFile.findBlockCommentEnd(data, match.index);
|
|
255
|
+
token = data.substring(match.index, reToken.lastIndex);
|
|
256
|
+
}
|
|
257
|
+
result += data.substring(last, match.index);
|
|
258
|
+
last = reToken.lastIndex;
|
|
259
|
+
if (token[0] === '"' || token[0] === "'") {
|
|
260
|
+
result += token;
|
|
261
|
+
} else if (token[1] === "/") {
|
|
262
|
+
if (/^\/\/[ \t]*i18n/.test(token)) result += token;
|
|
263
|
+
} else if (/^\/\*[ \t]*i18n/.test(token)) {
|
|
264
|
+
// like a line comment, a block comment is an i18n comment only
|
|
265
|
+
// when it starts with "i18n" (not "TODO: i18n" etc.)
|
|
266
|
+
result += token;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
return result + data.substring(last);
|
|
270
|
+
};
|
|
271
|
+
|
|
272
|
+
/*
|
|
273
|
+
* The resource bundle of webOS Rust apps provides only these two methods:
|
|
274
|
+
*
|
|
275
|
+
* bundle.get_loc_string("source")
|
|
276
|
+
* bundle.get_loc_string_with_key("key", "source")
|
|
277
|
+
*
|
|
278
|
+
* So the regexes match only the method-call form on a receiver ("." before the
|
|
279
|
+
* method name) with exactly one or two string literal arguments. A trailing comma
|
|
280
|
+
* is allowed because rustfmt adds one when it breaks the arguments into lines.
|
|
281
|
+
* Calls without a receiver, such as get_loc_string(res_bundle, "...") or
|
|
282
|
+
* get_loc_string("..."), and path calls such as ResBundle::get_loc_string(...)
|
|
283
|
+
* are not matched.
|
|
284
|
+
*
|
|
285
|
+
* reGetLocString: [1] source
|
|
286
|
+
* reGetLocStringWithKey: [1] key, [2] source
|
|
287
|
+
*/
|
|
288
|
+
var reGetLocString = new RegExp(/\.\s*get_loc_string\s*\(\s*"((?:\\[\s\S]|[^"\\])*)"\s*,?\s*\)/g);
|
|
289
|
+
var reGetLocStringWithKey = new RegExp(/\.\s*get_loc_string_with_key\s*\(\s*"((?:\\[\s\S]|[^"\\])*)"\s*,\s*"((?:\\[\s\S]|[^"\\])*)"\s*,?\s*\)/g);
|
|
290
|
+
// [1] line comment text, [2] block comment text (up to the first "*/")
|
|
291
|
+
var reI18nComment = new RegExp(/\/\/\s*i18n\s*(.*)|\/\*\s*i18n\s*(.*?)(?:\*\/|$)/);
|
|
292
|
+
var reGetLocStringCall = new RegExp(/\.\s*get_loc_string(?:_with_key)?\s*\(/);
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* Find the i18n comment for the call that ends at the given index. The comment
|
|
296
|
+
* must be on the same line as the end of the call, and there must be no other
|
|
297
|
+
* get_loc_string() or get_loc_string_with_key() call between them, so that the
|
|
298
|
+
* comment is attached only to the last call on the line:
|
|
299
|
+
*
|
|
300
|
+
* bundle.get_loc_string("X"); bundle.get_loc_string("Y"); // i18n for Y
|
|
301
|
+
*
|
|
302
|
+
* @private
|
|
303
|
+
* @param {String} data the data that is being parsed
|
|
304
|
+
* @param {Number} end the index just after the end of the call
|
|
305
|
+
* @returns {String|undefined} the comment, or undefined if there is none
|
|
306
|
+
*/
|
|
307
|
+
RustFile.getI18nComment = function(data, end) {
|
|
308
|
+
var last = data.indexOf('\n', end);
|
|
309
|
+
last = (last === -1) ? data.length : last;
|
|
310
|
+
// drop the "\r" of a CRLF line ending
|
|
311
|
+
var line = data.substring(end, last).replace(/\r$/, "");
|
|
312
|
+
var commentResult = reI18nComment.exec(line);
|
|
313
|
+
if (!commentResult) return undefined;
|
|
314
|
+
if (reGetLocStringCall.test(line.substring(0, commentResult.index))) return undefined;
|
|
315
|
+
return commentResult[1] !== undefined ? commentResult[1] : commentResult[2];
|
|
316
|
+
};
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
* Parse the data string looking for the localizable strings and add them to the
|
|
320
|
+
* project's translation set.
|
|
321
|
+
* @param {String} data the string to parse
|
|
322
|
+
*/
|
|
323
|
+
RustFile.prototype.parse = function(data) {
|
|
324
|
+
this.logger.debug("Extracting strings from " + this.pathName);
|
|
325
|
+
|
|
326
|
+
data = RustFile.removeCommentLines(data);
|
|
327
|
+
this.resourceIndex = 0;
|
|
328
|
+
|
|
329
|
+
var comment, match, key;
|
|
330
|
+
|
|
331
|
+
// To extract get_loc_string()
|
|
332
|
+
reGetLocString.lastIndex = 0; // just to be safe
|
|
333
|
+
var result = reGetLocString.exec(data);
|
|
334
|
+
while (result && result.length > 1) {
|
|
335
|
+
match = result[1];
|
|
336
|
+
if (match && match.length) {
|
|
337
|
+
this.logger.trace("Found string key: " + this.makeKey(match) + ", string: '" + match + "'");
|
|
338
|
+
|
|
339
|
+
comment = RustFile.getI18nComment(data, reGetLocString.lastIndex);
|
|
340
|
+
|
|
341
|
+
match = RustFile.unescapeString(match);
|
|
342
|
+
|
|
343
|
+
var r = this.API.newResource({
|
|
344
|
+
resType: "string",
|
|
345
|
+
project: this.project.getProjectId(),
|
|
346
|
+
key: match,
|
|
347
|
+
sourceLocale: this.project.sourceLocale,
|
|
348
|
+
source: match,
|
|
349
|
+
autoKey: true,
|
|
350
|
+
pathName: this.pathName,
|
|
351
|
+
state: "new",
|
|
352
|
+
comment: RustFile.trimComment(comment),
|
|
353
|
+
datatype: this.type.datatype,
|
|
354
|
+
index: this.resourceIndex++
|
|
355
|
+
});
|
|
356
|
+
r.mapping = this.mapping;
|
|
357
|
+
this.set.add(r);
|
|
358
|
+
} else {
|
|
359
|
+
this.logger.warn("Warning: Bogus empty string in get string call: ");
|
|
360
|
+
this.logger.warn("... " + data.substring(result.index, reGetLocString.lastIndex) + " ...");
|
|
361
|
+
}
|
|
362
|
+
result = reGetLocString.exec(data);
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
// To extract get_loc_string_with_key()
|
|
366
|
+
reGetLocStringWithKey.lastIndex = 0; // just to be safe
|
|
367
|
+
var result = reGetLocStringWithKey.exec(data);
|
|
368
|
+
while (result && result.length > 2) {
|
|
369
|
+
match = result[2];
|
|
370
|
+
key = result[1];
|
|
371
|
+
|
|
372
|
+
if (match && match.length && key && key.length) {
|
|
373
|
+
this.logger.trace("Found string key: " + this.makeKey(match) + ", string: '" + match + "'");
|
|
374
|
+
|
|
375
|
+
comment = RustFile.getI18nComment(data, reGetLocStringWithKey.lastIndex);
|
|
376
|
+
match = RustFile.unescapeString(match);
|
|
377
|
+
key = RustFile.unescapeString(key);
|
|
378
|
+
var r = this.API.newResource({
|
|
379
|
+
resType: "string",
|
|
380
|
+
project: this.project.getProjectId(),
|
|
381
|
+
key: key,
|
|
382
|
+
sourceLocale: this.project.sourceLocale,
|
|
383
|
+
source: match,
|
|
384
|
+
autoKey: true,
|
|
385
|
+
pathName: this.pathName,
|
|
386
|
+
state: "new",
|
|
387
|
+
comment: RustFile.trimComment(comment),
|
|
388
|
+
datatype: this.type.datatype,
|
|
389
|
+
index: this.resourceIndex++
|
|
390
|
+
});
|
|
391
|
+
r.mapping = this.mapping;
|
|
392
|
+
this.set.add(r);
|
|
393
|
+
} else {
|
|
394
|
+
this.logger.warn("Warning: Bogus empty string in get string call: ");
|
|
395
|
+
this.logger.warn("... " + data.substring(result.index, reGetLocStringWithKey.lastIndex) + " ...");
|
|
396
|
+
}
|
|
397
|
+
result = reGetLocStringWithKey.exec(data);
|
|
398
|
+
}
|
|
399
|
+
};
|
|
400
|
+
|
|
401
|
+
/**
|
|
402
|
+
* Extract all the localizable strings from the Rust file and add them to the
|
|
403
|
+
* project's translation set.
|
|
404
|
+
*/
|
|
405
|
+
RustFile.prototype.extract = function() {
|
|
406
|
+
this.logger.debug("Extracting strings from " + this.pathName);
|
|
407
|
+
if (this.pathName) {
|
|
408
|
+
var p = path.join(this.project.root, this.pathName);
|
|
409
|
+
try {
|
|
410
|
+
var data = fs.readFileSync(p, "utf-8");
|
|
411
|
+
if (data) {
|
|
412
|
+
this.parse(data);
|
|
413
|
+
}
|
|
414
|
+
} catch (e) {
|
|
415
|
+
this.logger.warn("Could not read file: " + p);
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
};
|
|
419
|
+
|
|
420
|
+
/**
|
|
421
|
+
* Return the set of resources found in the current Rust file.
|
|
422
|
+
*
|
|
423
|
+
* @returns {TranslationSet} The set of resources found in the
|
|
424
|
+
* current Rust file.
|
|
425
|
+
*/
|
|
426
|
+
RustFile.prototype.getTranslationSet = function() {
|
|
427
|
+
return this.set;
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
// we don't localize or write rust source files
|
|
431
|
+
RustFile.prototype.localize = function() {};
|
|
432
|
+
RustFile.prototype.write = function() {};
|
|
433
|
+
|
|
434
|
+
module.exports = RustFile;
|
package/RustFileType.js
ADDED
|
@@ -0,0 +1,401 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* RustFileType.js - Represents a collection of Rust files
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2026 JEDLSoft
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
*
|
|
16
|
+
* See the License for the specific language governing permissions and
|
|
17
|
+
* limitations under the License.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
var path = require("path");
|
|
21
|
+
var mm = require("micromatch");
|
|
22
|
+
var Locale = require("ilib/lib/Locale.js");
|
|
23
|
+
var RustFile = require("./RustFile.js");
|
|
24
|
+
var JsonResourceFileType = require("ilib-loctool-webos-json-resource");
|
|
25
|
+
var Utils = require("loctool/lib/utils.js")
|
|
26
|
+
var { utils: pluginUtils, translationResolver, pseudoWriter, generateModeWriter } = require("ilib-loctool-webos-common");
|
|
27
|
+
var buildResolver = translationResolver.buildResolver;
|
|
28
|
+
var resolveTranslation = translationResolver.resolveTranslation;
|
|
29
|
+
var writePseudoResources = pseudoWriter.writePseudoResources;
|
|
30
|
+
var writeGenerateModeResources = generateModeWriter.writeGenerateModeResources;
|
|
31
|
+
|
|
32
|
+
var RustFileType = function(project) {
|
|
33
|
+
this.type = "x-rust";
|
|
34
|
+
this.datatype = "x-rust";
|
|
35
|
+
this.resourceType = "json";
|
|
36
|
+
this.project = project;
|
|
37
|
+
this.API = project.getAPI();
|
|
38
|
+
this.extensions = [ ".rs"];
|
|
39
|
+
this.extracted = this.API.newTranslationSet(project.getSourceLocale());
|
|
40
|
+
this.newres = this.API.newTranslationSet(project.getSourceLocale());
|
|
41
|
+
this.pseudo = this.API.newTranslationSet(project.getSourceLocale());
|
|
42
|
+
this.logger = this.API.getLogger("loctool.plugin.webOSRustFileType");
|
|
43
|
+
|
|
44
|
+
// generate all the pseudo bundles we'll need. loctool normalizes the
|
|
45
|
+
// pseudoLocale option of project.json into the project.pseudoLocales object.
|
|
46
|
+
if (project.pseudoLocales && typeof project.pseudoLocales == 'object') {
|
|
47
|
+
this.pseudos = {};
|
|
48
|
+
for (var locale in project.pseudoLocales) {
|
|
49
|
+
var pseudo = this.API.getPseudoBundle(locale, this, project);
|
|
50
|
+
if (pseudo) {
|
|
51
|
+
this.pseudos[locale] = pseudo;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
if (Object.keys(project.localeMap).length > 0) {
|
|
57
|
+
Utils.setBaseLocale(project.localeMap);
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* The locale whose resources are written at the root of the
|
|
63
|
+
* [localeDir] path (i.e. [localeDir] is replaced with an empty string).
|
|
64
|
+
* @private
|
|
65
|
+
*/
|
|
66
|
+
var rootLocale = "en-US";
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* The default mappings used when the project does not configure
|
|
70
|
+
* `settings.rust.mappings` in its project.json file.
|
|
71
|
+
* @private
|
|
72
|
+
*/
|
|
73
|
+
var defaultMappings = {
|
|
74
|
+
"**/*.rs": {
|
|
75
|
+
"template": "[resourceDir]/[localeDir]/rsstrings.json"
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Return the mappings configured for this project, or the default
|
|
81
|
+
* mappings if none are configured. An empty mappings object is
|
|
82
|
+
* treated as not configured, because it would otherwise match no
|
|
83
|
+
* Rust files at all. In that case, a warning is logged once.
|
|
84
|
+
* @private
|
|
85
|
+
* @returns {Object} the mappings object keyed by glob pattern
|
|
86
|
+
*/
|
|
87
|
+
RustFileType.prototype._getMappings = function() {
|
|
88
|
+
var rustSettings = this.project.settings && this.project.settings.rust;
|
|
89
|
+
var mappings = rustSettings && rustSettings.mappings;
|
|
90
|
+
if (mappings && Object.keys(mappings).length) {
|
|
91
|
+
return mappings;
|
|
92
|
+
}
|
|
93
|
+
if (mappings && !this.emptyMappingsWarned) {
|
|
94
|
+
this.logger.warn("settings.rust.mappings is empty. The default mappings are used.");
|
|
95
|
+
this.emptyMappingsWarned = true;
|
|
96
|
+
}
|
|
97
|
+
return defaultMappings;
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Return the mapping corresponding to this path.
|
|
102
|
+
* @param {String} pathName the path to check
|
|
103
|
+
* @returns {Object} the mapping object corresponding to the
|
|
104
|
+
* path or undefined if none of the mappings match
|
|
105
|
+
*/
|
|
106
|
+
RustFileType.prototype.getMapping = function(pathName) {
|
|
107
|
+
if (typeof(pathName) === "undefined") {
|
|
108
|
+
return undefined;
|
|
109
|
+
}
|
|
110
|
+
var mappings = this._getMappings();
|
|
111
|
+
var patterns = Object.keys(mappings);
|
|
112
|
+
var normalized = path.normalize(pathName);
|
|
113
|
+
|
|
114
|
+
var match = patterns.find(function(pattern) {
|
|
115
|
+
return mm.isMatch(normalized, pattern);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
return match && mappings[match];
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Return the mapping used when there is no source file to match
|
|
123
|
+
* against, such as in generate mode where the resources come from
|
|
124
|
+
* the translation files rather than from the Rust source files.
|
|
125
|
+
* This is the first configured mapping, or the default mapping.
|
|
126
|
+
* @returns {Object} the mapping object
|
|
127
|
+
*/
|
|
128
|
+
RustFileType.prototype.getDefaultMapping = function() {
|
|
129
|
+
var mappings = this._getMappings();
|
|
130
|
+
var patterns = Object.keys(mappings);
|
|
131
|
+
return patterns.length ? mappings[patterns[0]] : defaultMappings["**/*.rs"];
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Return the location on disk where the resource file for the given
|
|
136
|
+
* locale should be written. The path is built from the template of the
|
|
137
|
+
* mapping. In addition to the substitutions supported by loctool's
|
|
138
|
+
* formatPath (eg. [dir], [resourceDir], [locale], [language],
|
|
139
|
+
* [localeUnder]), the [localeDir] keyword follows the webOS resource
|
|
140
|
+
* directory layout:
|
|
141
|
+
* <ul>
|
|
142
|
+
* <li>en-US is written at the root (eg. resources/rsstrings.json)
|
|
143
|
+
* <li>a language default locale uses the language only
|
|
144
|
+
* (eg. ko-KR -> resources/ko/rsstrings.json)
|
|
145
|
+
* <li>other locales use the full locale as directories
|
|
146
|
+
* (eg. en-GB -> resources/en/GB/rsstrings.json)
|
|
147
|
+
* </ul>
|
|
148
|
+
*
|
|
149
|
+
* @param {Object|undefined} mapping the mapping for the source file.
|
|
150
|
+
* If undefined, it is looked up from the pathname.
|
|
151
|
+
* @param {String|undefined} pathname path to the source file relative
|
|
152
|
+
* to the project root
|
|
153
|
+
* @param {String} locale the locale spec for the target locale
|
|
154
|
+
* @returns {String} the localized path name
|
|
155
|
+
*/
|
|
156
|
+
RustFileType.prototype.getLocalizedPath = function(mapping, pathname, locale) {
|
|
157
|
+
if (!mapping) {
|
|
158
|
+
mapping = this.getMapping(pathname);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
var template = mapping && mapping.template;
|
|
162
|
+
if (!template) {
|
|
163
|
+
template = defaultMappings["**/*.rs"].template;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
var loc = new Locale(locale);
|
|
167
|
+
var isBaseLocale = Utils.isBaseLocale(locale);
|
|
168
|
+
var lo = isBaseLocale ? loc.getLanguage() : loc.getSpec();
|
|
169
|
+
var localeDir = "";
|
|
170
|
+
|
|
171
|
+
if (isBaseLocale) {
|
|
172
|
+
if (loc.getSpec() !== rootLocale) {
|
|
173
|
+
localeDir = loc.getLanguage();
|
|
174
|
+
}
|
|
175
|
+
} else {
|
|
176
|
+
localeDir = loc.getSpec().replace(/-/g, "/");
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
var resourceDir = this.project.getResourceDirs(this.resourceType)[0] || "resources";
|
|
180
|
+
var output = Utils.formatPath(template.replace(/\[localeDir\]/g, localeDir), {
|
|
181
|
+
sourcepath: pathname,
|
|
182
|
+
locale: lo,
|
|
183
|
+
resourceDir: resourceDir
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
return path.join(this.project.target, output);
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Return true if the given path is a Rust file and is handled
|
|
191
|
+
* by the current file type. A Rust file is handled only if it
|
|
192
|
+
* matches one of the mappings, so that the mappings decide both
|
|
193
|
+
* which files are extracted and where their resources are written.
|
|
194
|
+
* Rust files that do not match any of the mappings are not handled.
|
|
195
|
+
*
|
|
196
|
+
* @param {String} pathName path to the file being questioned
|
|
197
|
+
* @returns {boolean} true if the path is a Rust file that matches
|
|
198
|
+
* one of the mappings, or false otherwise
|
|
199
|
+
*/
|
|
200
|
+
RustFileType.prototype.handles = function(pathName) {
|
|
201
|
+
this.logger.debug("RustFileType handles " + pathName + "?");
|
|
202
|
+
var ret = false;
|
|
203
|
+
if (pathName && pathName.length > 3 && pathName.substring(pathName.length - 3) === ".rs") {
|
|
204
|
+
ret = !!this.getMapping(pathName);
|
|
205
|
+
if (!ret) {
|
|
206
|
+
this.logger.debug("No mapping matches " + pathName);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
this.logger.debug(ret ? "Yes" : "No");
|
|
211
|
+
return ret;
|
|
212
|
+
};
|
|
213
|
+
|
|
214
|
+
RustFileType.prototype.name = function() {
|
|
215
|
+
return "Rust File Type";
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Write out the aggregated resources for this file type. In
|
|
220
|
+
* some cases, the string are written out to a common resource
|
|
221
|
+
* file, and in other cases, to a type-specific resource file.
|
|
222
|
+
* In yet other cases, nothing is written out, as the each of
|
|
223
|
+
* the files themselves are localized individually, so there
|
|
224
|
+
* are no aggregated strings.
|
|
225
|
+
* @param {TranslationSet} translations the set of translations from the
|
|
226
|
+
* repository
|
|
227
|
+
* @param {Array.<String>} locales the list of locales to localize to
|
|
228
|
+
*/
|
|
229
|
+
RustFileType.prototype.write = function(translations, locales) {
|
|
230
|
+
// distribute all the resources to their resource files
|
|
231
|
+
// and then let them write themselves out
|
|
232
|
+
|
|
233
|
+
var resFileType = this.project.getResourceFileType(this.resourceType);
|
|
234
|
+
var mode = this.project.settings.mode;
|
|
235
|
+
var deviceType = pluginUtils.getDeviceType(this.project.settings);
|
|
236
|
+
var customInheritLocale;
|
|
237
|
+
var res, resPath,
|
|
238
|
+
resources = this.extracted.getAll(),
|
|
239
|
+
db = this.project.db,
|
|
240
|
+
translationLocales = locales.filter(function(locale) {
|
|
241
|
+
return locale !== this.project.sourceLocale && locale !== this.project.pseudoLocale;
|
|
242
|
+
}.bind(this));
|
|
243
|
+
|
|
244
|
+
if (mode === "localize") {
|
|
245
|
+
// Build resolver: detects common project data and prepares policy-based lookup.
|
|
246
|
+
var resolver = buildResolver(db, translations, this.project.getProjectId(), { includeUniversal: true });
|
|
247
|
+
for (var i = 0; i < resources.length; i++) {
|
|
248
|
+
res = resources[i];
|
|
249
|
+
// for each extracted string, write out the translations of it
|
|
250
|
+
translationLocales.forEach(function(locale) {
|
|
251
|
+
this.logger.trace("Localizing Rust strings to " + locale);
|
|
252
|
+
resPath = this.getLocalizedPath(res.mapping, res.getPath(), locale);
|
|
253
|
+
|
|
254
|
+
customInheritLocale = this.project.getLocaleInherit(locale);
|
|
255
|
+
// Resolve translation via fallback chain (direct → policy → inherit) with dedup.
|
|
256
|
+
resolveTranslation({
|
|
257
|
+
resolver: resolver,
|
|
258
|
+
resFileType: resFileType,
|
|
259
|
+
newres: this.newres,
|
|
260
|
+
res: res,
|
|
261
|
+
locale: locale,
|
|
262
|
+
customInheritLocale: customInheritLocale,
|
|
263
|
+
dedupByBaseTranslation: true,
|
|
264
|
+
translationLocales: translationLocales,
|
|
265
|
+
resPath: resPath,
|
|
266
|
+
deviceType: deviceType,
|
|
267
|
+
API: this.API
|
|
268
|
+
});
|
|
269
|
+
}.bind(this));
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
// write pseudo resources
|
|
273
|
+
writePseudoResources({
|
|
274
|
+
pseudo: this.pseudo,
|
|
275
|
+
settings: this.project.settings,
|
|
276
|
+
settingsKey: this.type.replace("x-", ""),
|
|
277
|
+
datatype: this.datatype,
|
|
278
|
+
resFileType: resFileType,
|
|
279
|
+
sourceLocale: this.project.sourceLocale,
|
|
280
|
+
deviceType: deviceType,
|
|
281
|
+
resPathFn: function(res) { return this.getLocalizedPath(res.mapping, res.getPath(), res.getTargetLocale()); }.bind(this)
|
|
282
|
+
});
|
|
283
|
+
} else {
|
|
284
|
+
// generate mode
|
|
285
|
+
writeGenerateModeResources({
|
|
286
|
+
project: this.project,
|
|
287
|
+
translationLocales: translationLocales,
|
|
288
|
+
resources: this.project.getTranslations(translationLocales),
|
|
289
|
+
selfDatatype: this.datatype,
|
|
290
|
+
resFileType: resFileType,
|
|
291
|
+
db: db,
|
|
292
|
+
deviceType: deviceType,
|
|
293
|
+
dedupByBaseTranslation: true,
|
|
294
|
+
includeUniversal: true,
|
|
295
|
+
// generate mode has no source files to match, so use the default mapping
|
|
296
|
+
resPathFn: function(res) { return this.getLocalizedPath(this.getDefaultMapping(), undefined, res.getTargetLocale()); }.bind(this)
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
};
|
|
300
|
+
|
|
301
|
+
RustFileType.prototype.newFile = function(path) {
|
|
302
|
+
return new RustFile({
|
|
303
|
+
project: this.project,
|
|
304
|
+
pathName: path,
|
|
305
|
+
type: this
|
|
306
|
+
});
|
|
307
|
+
};
|
|
308
|
+
|
|
309
|
+
RustFileType.prototype.getDataType = function() {
|
|
310
|
+
return this.datatype;
|
|
311
|
+
};
|
|
312
|
+
|
|
313
|
+
RustFileType.prototype.getResourceTypes = function() {
|
|
314
|
+
return {};
|
|
315
|
+
};
|
|
316
|
+
|
|
317
|
+
/**
|
|
318
|
+
* Return the resource file type that this file type uses to write
|
|
319
|
+
* out its resources.
|
|
320
|
+
* @returns {Function|undefined} the resource file type, or undefined
|
|
321
|
+
* if this file type does not need resource files
|
|
322
|
+
*/
|
|
323
|
+
RustFileType.prototype.getResourceFileType = function() {
|
|
324
|
+
return JsonResourceFileType;
|
|
325
|
+
};
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* Return the translation set containing all of the extracted
|
|
329
|
+
* resources for all instances of this type of file. This includes
|
|
330
|
+
* all new strings and all existing strings. If it was extracted
|
|
331
|
+
* from a source file, it should be returned here.
|
|
332
|
+
*
|
|
333
|
+
* @returns {TranslationSet} the set containing all of the
|
|
334
|
+
* extracted resources
|
|
335
|
+
*/
|
|
336
|
+
RustFileType.prototype.getExtracted = function() {
|
|
337
|
+
return this.extracted;
|
|
338
|
+
};
|
|
339
|
+
|
|
340
|
+
/**
|
|
341
|
+
* Ensure that all resources collected so far have a pseudo translation.
|
|
342
|
+
*/
|
|
343
|
+
RustFileType.prototype.generatePseudo = function(locale, pb) {
|
|
344
|
+
var resources = this.extracted.getBy({
|
|
345
|
+
sourceLocale: pb.getSourceLocale()
|
|
346
|
+
});
|
|
347
|
+
this.logger.trace("Found " + resources.length + " source resources for " + pb.getSourceLocale());
|
|
348
|
+
var deviceType = pluginUtils.getDeviceType(this.project.settings);
|
|
349
|
+
|
|
350
|
+
resources.forEach(function(resource) {
|
|
351
|
+
this.logger.trace("Generating pseudo for " + resource.getKey());
|
|
352
|
+
var res = resource.generatePseudo(locale, pb);
|
|
353
|
+
if (res && res.getSource() !== pluginUtils.getTarget(res, deviceType)) {
|
|
354
|
+
this.pseudo.add(res);
|
|
355
|
+
}
|
|
356
|
+
}.bind(this));
|
|
357
|
+
};
|
|
358
|
+
|
|
359
|
+
/**
|
|
360
|
+
* Add the contents of the given translation set to the extracted resources
|
|
361
|
+
* for this file type.
|
|
362
|
+
*
|
|
363
|
+
* @param {TranslationSet} set set of resources to add to the current set
|
|
364
|
+
*/
|
|
365
|
+
RustFileType.prototype.addSet = function(set) {
|
|
366
|
+
this.extracted.addSet(set);
|
|
367
|
+
};
|
|
368
|
+
|
|
369
|
+
/**
|
|
370
|
+
* Return the translation set containing all of the new
|
|
371
|
+
* resources for all instances of this type of file.
|
|
372
|
+
*
|
|
373
|
+
* @returns {TranslationSet} the set containing all of the
|
|
374
|
+
* new resources
|
|
375
|
+
*/
|
|
376
|
+
RustFileType.prototype.getNew = function() {
|
|
377
|
+
return this.newres;
|
|
378
|
+
};
|
|
379
|
+
|
|
380
|
+
/**
|
|
381
|
+
* Return the translation set containing all of the pseudo
|
|
382
|
+
* localized resources for all instances of this type of file.
|
|
383
|
+
*
|
|
384
|
+
* @returns {TranslationSet} the set containing all of the
|
|
385
|
+
* pseudo localized resources
|
|
386
|
+
*/
|
|
387
|
+
RustFileType.prototype.getPseudo = function() {
|
|
388
|
+
return this.pseudo;
|
|
389
|
+
};
|
|
390
|
+
|
|
391
|
+
/**
|
|
392
|
+
* Return the list of file name extensions that this plugin can
|
|
393
|
+
* process.
|
|
394
|
+
*
|
|
395
|
+
* @returns {Array.<string>} the list of file name extensions
|
|
396
|
+
*/
|
|
397
|
+
RustFileType.prototype.getExtensions = function() {
|
|
398
|
+
return this.extensions;
|
|
399
|
+
};
|
|
400
|
+
|
|
401
|
+
module.exports = RustFileType;
|
package/package.json
CHANGED
|
@@ -1,6 +1,68 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ilib-loctool-webos-rust",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"description": "
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"main": "./RustFileType.js",
|
|
5
|
+
"description": "A loctool plugin that knows how to process Rust files",
|
|
6
|
+
"license": "Apache-2.0",
|
|
7
|
+
"keywords": [
|
|
8
|
+
"internationalization",
|
|
9
|
+
"i18n",
|
|
10
|
+
"localization",
|
|
11
|
+
"l10n",
|
|
12
|
+
"globalization",
|
|
13
|
+
"g11n",
|
|
14
|
+
"strings",
|
|
15
|
+
"resources",
|
|
16
|
+
"locale",
|
|
17
|
+
"translation",
|
|
18
|
+
"rust",
|
|
19
|
+
"rs"
|
|
20
|
+
],
|
|
21
|
+
"author": {
|
|
22
|
+
"name": "Goun Lee",
|
|
23
|
+
"email": "goun.lee@lge.com"
|
|
24
|
+
},
|
|
25
|
+
"contributors": [
|
|
26
|
+
{
|
|
27
|
+
"name": "Goun Lee",
|
|
28
|
+
"email": "goun.lee@lge.com"
|
|
29
|
+
}
|
|
30
|
+
],
|
|
31
|
+
"files": [
|
|
32
|
+
"CHANGELOG.md",
|
|
33
|
+
"README.md",
|
|
34
|
+
"LICENSE",
|
|
35
|
+
"RustFileType.js",
|
|
36
|
+
"RustFile.js"
|
|
37
|
+
],
|
|
38
|
+
"repository": {
|
|
39
|
+
"type": "git",
|
|
40
|
+
"url": "https://github.com/iLib-js/ilib-mono-webos",
|
|
41
|
+
"directory": "packages/ilib-loctool-webos-rust"
|
|
42
|
+
},
|
|
43
|
+
"homepage": "https://github.com/iLib-js/ilib-mono-webos/blob/main/packages/ilib-loctool-webos-rust",
|
|
44
|
+
"engines": {
|
|
45
|
+
"node": ">=10.0.0"
|
|
46
|
+
},
|
|
47
|
+
"dependencies": {
|
|
48
|
+
"ilib": "^15.0.0",
|
|
49
|
+
"micromatch": "^4.0.8",
|
|
50
|
+
"ilib-loctool-webos-json-resource": "1.7.11",
|
|
51
|
+
"ilib-loctool-webos-common": "1.3.0"
|
|
52
|
+
},
|
|
53
|
+
"peerDependencies": {
|
|
54
|
+
"loctool": "2.34.1"
|
|
55
|
+
},
|
|
56
|
+
"devDependencies": {
|
|
57
|
+
"jest": "^30.5.2",
|
|
58
|
+
"loctool": "2.34.1"
|
|
59
|
+
},
|
|
60
|
+
"scripts": {
|
|
61
|
+
"coverage": "pnpm test --coverage",
|
|
62
|
+
"test": "pnpm test:jest",
|
|
63
|
+
"test:jest": "LANG=en_US.UTF8 node node_modules/jest/bin/jest.js",
|
|
64
|
+
"test:watch": "pnpm test:jest --watch",
|
|
65
|
+
"debug": "node --inspect-brk node_modules/jest/bin/jest.js -i",
|
|
66
|
+
"clean": "git clean -f -d *"
|
|
67
|
+
}
|
|
6
68
|
}
|