Skip to content

Commit

Permalink
Prevent outputting invalid key names
Browse files Browse the repository at this point in the history
Now key names are escaped if they begin with a number or contain an invalid
character, so they shouldn't cause a parse error on the CoffeeTranslation
end.

Signed-off-by: Isabella <[email protected]>
  • Loading branch information
kawapure committed Jul 29, 2024
1 parent f4e5b7c commit 4af8987
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ internal static void WriteCoffeeTranslationFileFromCldrXml(XDocument cldr)
string content = (node.FirstNode as XText).Value;
string formattedContent = FormatStringLiteral(content);

langFileWriter.WriteLine($"{languageName}: {formattedContent}");
langFileWriter.WriteLine($"{EscapeKeyName(languageName)}: {formattedContent}");
}

string langFileContents = langFileWriter.ToString();
Expand Down Expand Up @@ -219,7 +219,7 @@ internal static void WriteCoffeeTranslationFileFromCldrXml(XDocument cldr)

string formattedContent = FormatStringLiteral(content);

countryFileWriter.WriteLine($"{countryName}: {formattedContent}");
countryFileWriter.WriteLine($"{EscapeKeyName(countryName)}: {formattedContent}");
}

string countryFileContents = countryFileWriter.ToString();
Expand All @@ -233,6 +233,20 @@ internal static void WriteFileHeaderComment(StringWriter writer)
writer.WriteLine();
}

internal static string EscapeKeyName(string keyName)
{
string result = string.Empty;

if (char.IsDigit(keyName[0]))
{
result += "_";
}

result += keyName.Replace("-", "_");

return result;
}

internal static string FormatStringLiteral(string srcValue)
{
srcValue = srcValue.Replace("\\", "\\\\");
Expand Down

0 comments on commit 4af8987

Please sign in to comment.