Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions internal/translations/language_file.android.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,28 @@ func (f *androidLanguageFile) Write(translations *translationData) error {
}

func (f *androidLanguageFile) write(translation *translation, io io.Writer) error {
regex := regexp.MustCompile(`%([0-9]+)`)
regex := regexp.MustCompile(`%%([0-9]+)`)

escape := strings.NewReplacer(
"&", "&",
"<", "&lt;",
">", "&gt;",
"\"", "\\\"",
"'", "\\'",
"\n", "\\n")
"\n", "\\n",
"%", "%%")

_, err := io.Write([]byte("<resources>\n"))
_, err := io.Write([]byte("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<resources>\n"))
if err != nil {
return err
}

for _, k := range translation.keys {
key := strings.ToLower(k)
value := regex.ReplaceAllString(translation.get(k), "%${1}$$s")
_, err = io.Write([]byte(fmt.Sprintf("\t<string name=\"%s\">%s</string>\n", key, escape.Replace(value))))
translationValue := translation.get(k)
escapedValue := escape.Replace(translationValue)
value := regex.ReplaceAllString(escapedValue, "%${1}$$s")
_, err = io.Write([]byte(fmt.Sprintf(" <string name=\"%s\">%s</string>\n", key, value)))
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions internal/translations/language_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func TestLanguageFileWriteInputFile(t *testing.T) {
}

func TestEscapingSingleColumn(t *testing.T) {
output := "<resources>\n\t<string name=\"that\">Another string including a | even.</string>\n\t<string name=\"this\">This is a longer sentence, which includes a comma.</string>\n</resources>\n"
output := "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<resources>\n <string name=\"that\">Another string including a | even.</string>\n <string name=\"this\">This is a longer sentence, which includes a comma.</string>\n</resources>\n"
outputPath := "../../build/out.csv"
inputPath := "../../build/test.csv"
err := os.WriteFile("../../build/test.csv", []byte("key,en\nTHIS,\"This is a longer sentence, which includes a comma.\"\nTHAT,Another string including a | even."), 0777)
Expand Down Expand Up @@ -155,7 +155,7 @@ func TestEscapingSingleColumn(t *testing.T) {
}

func TestEscapingAllColumns(t *testing.T) {
output := "<resources>\n\t<string name=\"that\">Another string including a | even.</string>\n\t<string name=\"this\">This is a longer sentence, which includes a comma.</string>\n</resources>\n"
output := "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<resources>\n <string name=\"that\">Another string including a | even.</string>\n <string name=\"this\">This is a longer sentence, which includes a comma.</string>\n</resources>\n"
outputPath := "../../build/out.csv"
inputPath := "../../build/test.csv"
err := os.WriteFile("../../build/test.csv", []byte("\"key\",\"en\"\n\"THIS\",\"This is a longer sentence, which includes a comma.\"\n\"THAT\",\"Another string including a | even.\""), 0777)
Expand Down
14 changes: 8 additions & 6 deletions internal/translations/testdata/android-da.expected
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="something">Noget</string>
<string name="something_escaped">Noget med ,</string>
<string name="something_for_xml">Det er som &amp; og &lt;b&gt;fremhævet&lt;/b&gt; \"tekst\" eller 官话</string>
<string name="something_with_arguments">Noget med %1$s og %2$s</string>
<string name="watch_out_for_new_lines">Tekst\nmed\nline\nskift</string>
<string name="ioscasesensitivekey">Noget</string>
<string name="something">Noget</string>
<string name="something_escaped">Noget med ,</string>
<string name="something_for_xml">Det er som &amp; og &lt;b&gt;fremhævet&lt;/b&gt; \"tekst\" eller 官话</string>
<string name="something_with_arguments">Noget med %1$s og %2$s</string>
<string name="something_with_escaped_percentage">Noget med %% %1$s %%%2$s</string>
<string name="watch_out_for_new_lines">Tekst\nmed\nline\nskift</string>
<string name="ioscasesensitivekey">Noget</string>
</resources>
1 change: 1 addition & 0 deletions internal/translations/testdata/android-empty.expected
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
</resources>
14 changes: 8 additions & 6 deletions internal/translations/testdata/android-en.expected
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="something">Something</string>
<string name="something_escaped">Something with ,</string>
<string name="something_for_xml">It\'s like &amp; and &lt;b&gt;bold&lt;/b&gt; \"text\" or 官话</string>
<string name="something_with_arguments">Something with %1$s and %2$s</string>
<string name="watch_out_for_new_lines">Text\nwith\nnew\nlines</string>
<string name="ioscasesensitivekey">Something</string>
<string name="something">Something</string>
<string name="something_escaped">Something with ,</string>
<string name="something_for_xml">It\'s like &amp; and &lt;b&gt;bold&lt;/b&gt; \"text\" or 官话</string>
<string name="something_with_arguments">Something with %1$s and %2$s</string>
<string name="something_with_escaped_percentage">Something with %% %1$s %%%2$s</string>
<string name="watch_out_for_new_lines">Text\nwith\nnew\nlines</string>
<string name="ioscasesensitivekey">Something</string>
</resources>
5 changes: 3 additions & 2 deletions internal/translations/testdata/fill-in/android-da.expected
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="something">Noget</string>
<string name="something_only_in_en">Something only in English</string>
<string name="something">Noget</string>
<string name="something_only_in_en">Something only in English</string>
</resources>
5 changes: 3 additions & 2 deletions internal/translations/testdata/fill-in/android-en.expected
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="something">Something</string>
<string name="something_only_in_en">Something only in English</string>
<string name="something">Something</string>
<string name="something_only_in_en">Something only in English</string>
</resources>
1 change: 1 addition & 0 deletions internal/translations/testdata/input.csv
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ KEY,UPDATE NEEDED,EN,DA,COMMENT
SOMETHING,,Something,Noget,
SOMETHING_ESCAPED,,"Something with ,","Noget med ,",
SOMETHING_WITH_ARGUMENTS,,Something with %1 and %2,Noget med %1 og %2,
SOMETHING_WITH_ESCAPED_PERCENTAGE,,Something with % %1 %%2,Noget med % %1 %%2,
SOMETHING_FOR_XML,,"It's like & and <b>bold</b> ""text"" or 官话","Det er som & og <b>fremhævet</b> ""tekst"" eller 官话",
WATCH_OUT_FOR_NEW_LINES,,"Text
with
Expand Down
1 change: 1 addition & 0 deletions internal/translations/testdata/ios-da.expected
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"SOMETHING_ESCAPED" = "Noget med ,";
"SOMETHING_FOR_XML" = "Det er som & og <b>fremhævet</b> \"tekst\" eller 官话";
"SOMETHING_WITH_ARGUMENTS" = "Noget med %1 og %2";
"SOMETHING_WITH_ESCAPED_PERCENTAGE" = "Noget med % %1 %%2";
"WATCH_OUT_FOR_NEW_LINES" = "Tekst\nmed\nline\nskift";
"iOSCaseSensitiveKey" = "Noget";
1 change: 1 addition & 0 deletions internal/translations/testdata/ios-en.expected
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"SOMETHING_ESCAPED" = "Something with ,";
"SOMETHING_FOR_XML" = "It's like & and <b>bold</b> \"text\" or 官话";
"SOMETHING_WITH_ARGUMENTS" = "Something with %1 and %2";
"SOMETHING_WITH_ESCAPED_PERCENTAGE" = "Something with % %1 %%2";
"WATCH_OUT_FOR_NEW_LINES" = "Text\nwith\nnew\nlines";
"iOSCaseSensitiveKey" = "Something";
1 change: 1 addition & 0 deletions internal/translations/testdata/ios-swift.expected
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ public struct Translations {
static let SOMETHING_ESCAPED = NSLocalizedString("SOMETHING_ESCAPED", comment: "")
static let SOMETHING_FOR_XML = NSLocalizedString("SOMETHING_FOR_XML", comment: "")
static func SOMETHING_WITH_ARGUMENTS(_ p1: String, _ p2: String) -> String { return NSLocalizedString("SOMETHING_WITH_ARGUMENTS", comment: "").replacingOccurrences(of: "%1", with: p1).replacingOccurrences(of: "%2", with: p2) }
static func SOMETHING_WITH_ESCAPED_PERCENTAGE(_ p1: String, _ p2: String) -> String { return NSLocalizedString("SOMETHING_WITH_ESCAPED_PERCENTAGE", comment: "").replacingOccurrences(of: "%1", with: p1).replacingOccurrences(of: "%2", with: p2) }
static let WATCH_OUT_FOR_NEW_LINES = NSLocalizedString("WATCH_OUT_FOR_NEW_LINES", comment: "")
static let iOSCaseSensitiveKey = NSLocalizedString("iOSCaseSensitiveKey", comment: "")
}
1 change: 1 addition & 0 deletions internal/translations/testdata/json-en.expected
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
"something_escaped": "Something with ,",
"something_for_xml": "It's like \u0026 and \u003cb\u003ebold\u003c/b\u003e \"text\" or 官话",
"something_with_arguments": "Something with %1 and %2",
"something_with_escaped_percentage": "Something with % %1 %%2",
"watch_out_for_new_lines": "Text\nwith\nnew\nlines"
}