@l10nmonster/helpers-ios 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/README.md +18 -0
- package/index.js +78 -0
- package/package.json +17 -0
package/README.md
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# L10n Monster iOS Helpers
|
|
2
|
+
|
|
3
|
+
|Module|Export|Description|
|
|
4
|
+
|---|---|---|
|
|
5
|
+
|`helpers-ios`|`StringsFilter`|Filter for .strings files.|
|
|
6
|
+
|`helpers-ios`|`escapesDecoder`|Decoder for escaped chars like `\n` and `\U00a0`.|
|
|
7
|
+
|`helpers-ios`|`phDecoder`|Decoder for `%d` style placeholders.|
|
|
8
|
+
|`helpers-ios`|`escapesEncoder`|Encoder for escaped chars like `\n`.|
|
|
9
|
+
|
|
10
|
+
### iOS Strings Filter
|
|
11
|
+
|
|
12
|
+
```js
|
|
13
|
+
this.resourceFilter = new ios.StringsFilter();
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
A filter for strings files used in iOS apps.
|
|
17
|
+
|
|
18
|
+
* [LIMITATION] it doesn't support files encoded in UTF-16.
|
package/index.js
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
const i18nStringsFiles = require('@l10nmonster/i18n-strings-files');
|
|
2
|
+
const { regex } = require('@l10nmonster/helpers');
|
|
3
|
+
|
|
4
|
+
// filter for iOS .strings file (in utf-8)
|
|
5
|
+
exports.StringsFilter = class IosStringsFilter {
|
|
6
|
+
constructor(params) {
|
|
7
|
+
this.emitComments = params?.emitComments || false;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
async parseResource({ resource }) {
|
|
11
|
+
const parsedResource = i18nStringsFiles.parse(resource, { 'wantsComments' : true });
|
|
12
|
+
const segments = Object.entries(parsedResource).map(([k, v]) => ({
|
|
13
|
+
sid: k,
|
|
14
|
+
str: v.text,
|
|
15
|
+
notes: v.comment,
|
|
16
|
+
}));
|
|
17
|
+
return {
|
|
18
|
+
segments,
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
async translateResource({ resource, translator }) {
|
|
23
|
+
const parsedResource = i18nStringsFiles.parse(resource, { 'wantsComments' : true });
|
|
24
|
+
for (const [sid, source] of Object.entries(parsedResource)) {
|
|
25
|
+
const translation = await translator(sid, source.text);
|
|
26
|
+
if (translation === undefined) {
|
|
27
|
+
delete parsedResource[sid];
|
|
28
|
+
} else {
|
|
29
|
+
parsedResource[sid].text = translation;
|
|
30
|
+
!this.emitComments && parsedResource[sid].comment && delete parsedResource[sid].comment;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return Object.keys(parsedResource).length > 0 ?
|
|
34
|
+
i18nStringsFiles.compile(parsedResource, { 'wantsComments' : this.emitComments }) :
|
|
35
|
+
null;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/LoadingResources/Strings/Strings.html#//apple_ref/doc/uid/10000051i-CH6-97055-CJBFDJGF
|
|
40
|
+
const iosControlCharsToDecode = {
|
|
41
|
+
t: '\t',
|
|
42
|
+
n: '\n',
|
|
43
|
+
r: '\r',
|
|
44
|
+
f: '\f',
|
|
45
|
+
};
|
|
46
|
+
exports.escapesDecoder = regex.decoderMaker(
|
|
47
|
+
'iosEscapesDecoder',
|
|
48
|
+
/(?<node>\\(?<escapedChar>['"\\])|\\(?<escapedControl>[tbnrf])|\\U(?<codePoint>[0-9A-Za-z]{4}))/g, // note that in ios the \U is uppercase!
|
|
49
|
+
(groups) => (groups.escapedChar ??
|
|
50
|
+
(groups.escapedControl ?
|
|
51
|
+
(iosControlCharsToDecode[groups.escapedControl] ?? `\\${groups.escapedControl}`) :
|
|
52
|
+
String.fromCharCode(parseInt(groups.codePoint, 16))
|
|
53
|
+
)
|
|
54
|
+
)
|
|
55
|
+
);
|
|
56
|
+
|
|
57
|
+
exports.escapesEncoder = regex.encoderMaker(
|
|
58
|
+
'iosEscapesEncoder',
|
|
59
|
+
// eslint-disable-next-line prefer-named-capture-group
|
|
60
|
+
/(\t)|(\n)|(\r)|(\f)/g,
|
|
61
|
+
{
|
|
62
|
+
'\t': '\\t',
|
|
63
|
+
'\n': '\\n',
|
|
64
|
+
'\r': '\\r',
|
|
65
|
+
'\f': '\\f',
|
|
66
|
+
}
|
|
67
|
+
);
|
|
68
|
+
|
|
69
|
+
// iOS-style and C-style placeholders
|
|
70
|
+
// full specs at https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html
|
|
71
|
+
// and https://pubs.opengroup.org/onlinepubs/009695399/functions/printf.html
|
|
72
|
+
// loosely based on https://stackoverflow.com/questions/45215648/regex-capture-type-specifiers-in-format-string
|
|
73
|
+
// space and quote tags have been omitted to avoid matching unexpected combinations
|
|
74
|
+
exports.phDecoder = regex.decoderMaker(
|
|
75
|
+
'iosPHDecoder',
|
|
76
|
+
/(?<tag>%(?:\d\$)?[0#+-]?[0-9*]*\.?\d*[hl]{0,2}[jztL]?[diuoxXeEfgGaAcpsSn@])/g,
|
|
77
|
+
(groups) => ({ t: 'x', v: groups.tag })
|
|
78
|
+
);
|
package/package.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@l10nmonster/helpers-ios",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Helpers to deal with iOS file formats",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
|
+
},
|
|
9
|
+
"author": "Diego Lagunas",
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"dependencies": {
|
|
12
|
+
"@l10nmonster/i18n-strings-files": "^3.0.0"
|
|
13
|
+
},
|
|
14
|
+
"peerDependencies": {
|
|
15
|
+
"@l10nmonster/helpers": "^1"
|
|
16
|
+
}
|
|
17
|
+
}
|