Skip to content

Commit

Permalink
Correctly encode percentage signs for Android and iOS
Browse files Browse the repository at this point in the history
Fixes #30, #21 and #31
  • Loading branch information
larslockefeer committed Apr 30, 2020
1 parent 6cd8681 commit 1c08697
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 51 deletions.
40 changes: 23 additions & 17 deletions src/actions/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const { accessiblityKeywords, groupKeywords, platformKeywords } = require("../mo
const { groupByKey } = require("../utils/arrayUtils");
const { flatten } = require("../utils/arrayUtils");

const percentEncodingPattern = /%(?!\d+{{.}})/;

const render = (data, outputPath, platforms, languages) => {
const translations = normalizeYaml(data, platforms, languages);
const translationsPerLanguage = groupByKey(translations, t => t.language);
Expand Down Expand Up @@ -118,23 +120,27 @@ const substitutionsForPlatform = platform => {
switch (platform) {
case platformKeywords.ANDROID:
// prettier-ignore
return {
"{{s}}": "$s",
"{{d}}": "$d",
"\n": "\\n",
"@": "\@", // eslint-disable-line no-useless-escape
"?": "\?", // eslint-disable-line no-useless-escape
return [
// Important: & should be substituted before we introduce new ampersands as part of our substitutions
"&": "&",
"<": "&lt;",
">": "&gt;",
"\"": "&quot;"
};
{ search: "&", replace: "&amp;" },
// Important: % should be substituted before we substitute {{s}} and {{d}}
{ search: percentEncodingPattern, replace: "&#37;" },
{ search: "{{s}}", replace: "$s" },
{ search: "{{d}}", replace: "$d" },
{ search: "\n", replace: "\\n" },
{ search: "@", replace: "\@" }, // eslint-disable-line no-useless-escape
{ search: "?", replace: "\?" }, // eslint-disable-line no-useless-escape
{ search: "<", replace: "&lt;" },
{ search: ">", replace: "&gt;" },
{ search: "\"", replace: "&quot;" },
];
case platformKeywords.IOS:
return {
"{{s}}": "$@",
"{{d}}": "$d"
};
return [
// Important: % should be substituted before we substitute {{s}} and {{d}}
{ search: percentEncodingPattern, replace: "%%" },
{ search: "{{s}}", replace: "$@" },
{ search: "{{d}}", replace: "$d" }
];
}
};

Expand All @@ -148,8 +154,8 @@ const keyDelimiterForPlatform = platform => {
};

const substitute = (value, valueSubstitutions) => {
Object.keys(valueSubstitutions).forEach(search => {
value = value.split(`${search}`).join(valueSubstitutions[search]);
valueSubstitutions.forEach(substitution => {
value = value.split(substitution.search).join(substitution.replace);
});
return value;
};
Expand Down
24 changes: 12 additions & 12 deletions tests/actions/__snapshots__/normalize.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ Array [
},
Object {
"COPY": Object {
"containsFormatting": false,
"translation": "The following characters need special handling on some platforms: @ ? < & ' \\"",
"containsFormatting": true,
"translation": "The following characters need special handling on some platforms: @ ? < & ' \\". And then there is the percent-sign: most cases should be replaced (e.g. 99.9% and %1 and %.1) but not the percentage-sign in strings like %1{{s}} or %42{{d}}.",
"type": "SINGULAR",
},
"keyPath": Array [
Expand All @@ -174,8 +174,8 @@ Array [
},
Object {
"COPY": Object {
"containsFormatting": false,
"translation": "De volgende karakters moeten herschreven worden op sommige platforms: @ ? < & ' \\"",
"containsFormatting": true,
"translation": "De volgende karakters moeten herschreven worden op sommige platforms: @ ? < & ' \\". En let op met het procent-teken: in de meeste gevallen moet het vervangen worden (e.g. 99.9% en %1 en %.1) maar niet het procent-teken in strings als %1{{s}} of %42{{d}}.",
"type": "SINGULAR",
},
"keyPath": Array [
Expand Down Expand Up @@ -575,8 +575,8 @@ Array [
},
Object {
"COPY": Object {
"containsFormatting": false,
"translation": "The following characters need special handling on some platforms: @ ? < & ' \\"",
"containsFormatting": true,
"translation": "The following characters need special handling on some platforms: @ ? < & ' \\". And then there is the percent-sign: most cases should be replaced (e.g. 99.9% and %1 and %.1) but not the percentage-sign in strings like %1{{s}} or %42{{d}}.",
"type": "SINGULAR",
},
"keyPath": Array [
Expand All @@ -589,8 +589,8 @@ Array [
},
Object {
"COPY": Object {
"containsFormatting": false,
"translation": "De volgende karakters moeten herschreven worden op sommige platforms: @ ? < & ' \\"",
"containsFormatting": true,
"translation": "De volgende karakters moeten herschreven worden op sommige platforms: @ ? < & ' \\". En let op met het procent-teken: in de meeste gevallen moet het vervangen worden (e.g. 99.9% en %1 en %.1) maar niet het procent-teken in strings als %1{{s}} of %42{{d}}.",
"type": "SINGULAR",
},
"keyPath": Array [
Expand Down Expand Up @@ -934,8 +934,8 @@ Array [
},
Object {
"COPY": Object {
"containsFormatting": false,
"translation": "The following characters need special handling on some platforms: @ ? < & ' \\"",
"containsFormatting": true,
"translation": "The following characters need special handling on some platforms: @ ? < & ' \\". And then there is the percent-sign: most cases should be replaced (e.g. 99.9% and %1 and %.1) but not the percentage-sign in strings like %1{{s}} or %42{{d}}.",
"type": "SINGULAR",
},
"keyPath": Array [
Expand All @@ -948,8 +948,8 @@ Array [
},
Object {
"COPY": Object {
"containsFormatting": false,
"translation": "De volgende karakters moeten herschreven worden op sommige platforms: @ ? < & ' \\"",
"containsFormatting": true,
"translation": "De volgende karakters moeten herschreven worden op sommige platforms: @ ? < & ' \\". En let op met het procent-teken: in de meeste gevallen moet het vervangen worden (e.g. 99.9% en %1 en %.1) maar niet het procent-teken in strings als %1{{s}} of %42{{d}}.",
"type": "SINGULAR",
},
"keyPath": Array [
Expand Down
Loading

0 comments on commit 1c08697

Please sign in to comment.