From 4af898743e4df231635014f5fa2b8fb3096aeb9a Mon Sep 17 00:00:00 2001 From: Isabella Date: Sun, 28 Jul 2024 21:47:19 -0700 Subject: [PATCH] Prevent outputting invalid key names 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 --- Program.cs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/Program.cs b/Program.cs index 321b50c..dbc4733 100644 --- a/Program.cs +++ b/Program.cs @@ -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(); @@ -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(); @@ -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("\\", "\\\\");