Skip to content

Commit b0b0bf9

Browse files
committed
Do not encode simple keys
1 parent 1a75a62 commit b0b0bf9

File tree

4 files changed

+34
-16
lines changed

4 files changed

+34
-16
lines changed

src/hooks/converter.android.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,26 @@ export class ConverterAndroid extends ConverterCommon {
4646
);
4747
this.createDirectoryIfNeeded(languageResourcesDir);
4848
let strings = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<resources>\n";
49-
i18nEntries.forEach((value, key) => {
50-
const encodedKey = encodeKey(key);
51-
const encodedValue = encodeValue(value);
49+
this.encodeI18nEntries(i18nEntries).forEach((encodedValue, encodedKey) => {
5250
strings += ` <string name="${encodedKey}">${encodedValue}</string>\n`;
53-
if (key === "app.name") {
54-
strings += ` <string name="app_name">${encodedValue}</string>\n`;
55-
strings += ` <string name="title_activity_kimera">${encodedValue}</string>\n`;
56-
}
5751
});
5852
strings += "</resources>\n";
5953
const resourceFilePath = path.join(languageResourcesDir, "strings.xml");
6054
this.writeFileSyncIfNeeded(resourceFilePath, strings);
6155
return this;
6256
}
57+
58+
private encodeI18nEntries(i18nEntries: I18nEntries): I18nEntries {
59+
const encodedI18nEntries: I18nEntries = new Map();
60+
i18nEntries.forEach((value, key) => {
61+
const encodedKey = encodeKey(key);
62+
const encodedValue = encodeValue(value);
63+
encodedI18nEntries.set(encodedKey, encodedValue);
64+
if (key === "app.name") {
65+
encodedI18nEntries.set("app_name", encodedValue);
66+
encodedI18nEntries.set("title_activity_kimera", encodedValue);
67+
}
68+
});
69+
return encodedI18nEntries;
70+
}
6371
}

src/hooks/converter.common.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ export abstract class ConverterCommon {
2424
.platformProjectService
2525
.getAppResourcesDestinationDirectoryPath(projectData)
2626
;
27-
2827
this.i18nDirectoryPath = path.join(projectData.appDirectoryPath, "i18n");
2928
}
3029

@@ -42,7 +41,9 @@ export abstract class ConverterCommon {
4241
public run(): this {
4342
this.dataProvider.getLanguages().forEach((languageI18nEntries, language) => {
4443
this.createLanguageResourcesFiles(
45-
language, language === this.dataProvider.getDefaultLanguage(), languageI18nEntries
44+
language,
45+
language === this.dataProvider.getDefaultLanguage(),
46+
languageI18nEntries
4647
);
4748
});
4849
if (fs.existsSync(this.appResourcesDirectoryPath) && fs.statSync(this.appResourcesDirectoryPath).isDirectory()) {

src/hooks/converter.ios.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ export class ConverterIOS extends ConverterCommon {
4242
const languageResourcesDir = path.join(this.appResourcesDirectoryPath, `${language}.lproj`);
4343
this
4444
.createDirectoryIfNeeded(languageResourcesDir)
45-
.writeStrings(languageResourcesDir, "Localizable.strings", i18nEntries, true)
46-
.writeStrings(languageResourcesDir, "InfoPlist.strings", infoPlistStrings, false)
45+
.writeStrings(languageResourcesDir, "Localizable.strings", i18nEntries)
46+
.writeStrings(languageResourcesDir, "InfoPlist.strings", infoPlistStrings)
4747
;
4848
if (isDefaultLanguage) {
4949
infoPlistStrings.set("CFBundleDevelopmentRegion", language);
@@ -52,15 +52,24 @@ export class ConverterIOS extends ConverterCommon {
5252
return this;
5353
}
5454

55+
private encodeI18nEntries(i18nEntries: I18nEntries): I18nEntries {
56+
const encodedI18nEntries: I18nEntries = new Map();
57+
i18nEntries.forEach((value, key) => {
58+
const encodedKey = encodeKey(key);
59+
const encodedValue = encodeValue(value);
60+
encodedI18nEntries.set(encodedKey, encodedValue);
61+
});
62+
return encodedI18nEntries;
63+
}
64+
5565
private writeStrings(
5666
languageResourcesDir: string,
5767
resourceFileName: string,
58-
strings: I18nEntries,
59-
encodeKeys: boolean
68+
i18nEntries: I18nEntries,
6069
): this {
6170
let content = "";
62-
strings.forEach((value, key) => {
63-
content += `"${encodeKeys ? encodeKey(key) : key}" = "${encodeValue(value)}";\n`;
71+
this.encodeI18nEntries(i18nEntries).forEach((encodedValue, encodedKey) => {
72+
content += `"${encodedKey}" = "${encodedValue}";\n`;
6473
});
6574
const resourceFilePath = path.join(languageResourcesDir, resourceFileName);
6675
this.writeFileSyncIfNeeded(resourceFilePath, content);

src/resource.common.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as shorthash from "shorthash";
22

33
export function encodeKey(key: string): string {
4-
return `_${key.replace(/[^\w]/g, "_")}_${shorthash.unique(key)}`;
4+
return key.match(/^[_a-zA-Z]\w*$/) ? key : `_${key.replace(/[^\w]/g, "_")}_${shorthash.unique(key)}`;
55
}
66

77
export function replace(find: string[], replace: string[], subject: string): string {

0 commit comments

Comments
 (0)