Changes

→‎SyntaxAbstractor: + TranslationValidator in 1.6.9 patch 85
Line 356: Line 356:  
</syntaxhighlight>
 
</syntaxhighlight>
   −
This is meant for unit tests that compare translations with the base text to make sure the basic format is the same. For example:
+
This is meant for unit tests that compare translations with the base text to make sure the basic format is the same. For unit tests, you'd usually use this through the [[#TranslationValidator|<samp>TranslationValidator</samp>]] instead of directly.
 +
 
 +
===TranslationValidator===
 +
The new <samp>TranslationValidator</samp> compares translations to the original data, and returns a list of detected issues like missing or unknown keys, [[#SyntaxAbstractor|mismatched syntax]], or malformed syntax (e.g. invalid gender-switch blocks).
 +
 
 +
For example, to validate a C# mod's translations:
 
<syntaxhighlight lang="c#">
 
<syntaxhighlight lang="c#">
SyntaxAbstractor abstractor = new();
+
// get translations
 +
var translatedData = helper.Translation.GetTranslations().ToDictionary(p => p.Key);
   −
string baseSyntax = abstractor.ExtractDialogueSyntax(baseText);
+
// get base text
string translatedSyntax = abstractor.ExtractDialogueSyntax(translatedText);
+
var baseData = new Dictionary<string, Translation>();
 
+
foreach (string key in translatedData.Keys)
if (baseSyntax != translatedSyntax)
   
{
 
{
     Assert.Fail(
+
     if (helper.Translation.GetInAllLocales(key).TryGetValue("default", out Translation? baseText))
        $"""
+
         baseData[key] = baseText;
        Syntax:
  −
            base:  {baseSyntax}
  −
            local: {translatedSyntax}
  −
                  {"".PadRight(GetDiffIndex(baseSyntax, translatedSyntax))}^
  −
        Text:
  −
            base:  {baseText}
  −
            local: {translatedText}
  −
                  {"".PadRight(GetDiffIndex(baseText, translatedText))}^
  −
         """
  −
    );
   
}
 
}
   −
// get the index at which two strings differ
+
// assert all tests succeed
int GetDiffIndex(string baseText, string translatedText)
+
SyntaxAbstractor abstractor = new();
{
+
foreach (var issue in TranslationValidator.Compare(baseData, translatedData, element => element.Value, (key, value) => abstractor.ExtractDialogueSyntax(value)))
    int minLength = Math.Min(baseText.Length, translatedText.Length);
+
     Assert.Fail($"Validation failed for '{issue.Key}': {issue.SuggestedError}.");
 
  −
     for (int i = 0; i < minLength; i++)
  −
    {
  −
        if (baseText[i] != translatedText[i])
  −
            return i;
  −
    }
  −
 
  −
    return minLength;
  −
}
   
</syntaxhighlight>
 
</syntaxhighlight>
  
manager
8,549

edits