[msbuild] Localization String Test for All Strings (#9971)

This test will run through all available strings in MSBStrings.Designer.cs in the each of the supported languages.
If any strings should fire in other languages but are still in english, an error will be thrown unless that error code is in an ignore file.

There are two types of ignore files introduced:

LocalizationIgnore/common-Translations.ignore
for error codes that are not yet translated in all languages
LocalizationIgnore/<LOCALE>-Translations.ignore
for error codes that are not yet translated in the specific locale

If an error code is in an ignore file and it turns out it is translated, an error will be thrown to remove the error code from the ignore file.

If there is a non-valid error code in any ignore file, an error will also be thrown.

Wiki will be updated for the team to reflect this test: https://github.com/xamarin/maccore/wiki/Localization
This commit is contained in:
TJ Lambert 2020-10-30 10:19:55 -05:00 коммит произвёл GitHub
Родитель 6e5f39a5b5
Коммит d51d34a0eb
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
2 изменённых файлов: 75 добавлений и 0 удалений

Просмотреть файл

@ -0,0 +1,4 @@
# Insert Error codes that are waiting on translations below (one per line)
W0111
E7069
E7070

Просмотреть файл

@ -7,6 +7,9 @@ using System.Globalization;
using System.Threading;
using System.Collections.Generic;
using Xamarin.Localization.MSBuild;
using System.ComponentModel;
using System.Linq;
using System.Text;
namespace Xamarin.iOS.Tasks {
[TestFixture]
@ -83,5 +86,73 @@ namespace Xamarin.iOS.Tasks {
PropertyInfo propertyInfo = typeof (MSBStrings).GetProperty (errorCode);
return (string) propertyInfo.GetValue (null, null);
}
IList<string> commonIgnoreList = null;
static string shortCommonPath = "xamarin-macios/tests/msbuild/Xamarin.MacDev.Tasks.Tests/TaskTests/LocalizationIgnore/common-Translations.ignore";
[SetUp]
public void SetUp ()
{
commonIgnoreList = ReadFile ($"{Directory.GetCurrentDirectory ()}/TaskTests/LocalizationIgnore/common-Translations.ignore");
}
[TestCase ("cs-CZ")]
[TestCase ("de-DE")]
[TestCase ("es-ES")]
[TestCase ("fr-FR")]
[TestCase ("it-IT")]
[TestCase ("ja-JP")]
[TestCase ("ko-KR")]
[TestCase ("pl-PL")]
[TestCase ("pt-BR")]
[TestCase ("ru-RU")]
[TestCase ("tr-TR")]
[TestCase ("zh-CN")]
[TestCase ("zh-TW")]
public void AllErrorTranslation (string culture)
{
StringBuilder errorList = new StringBuilder (string.Empty);
IList<string> cultureIgnoreList = null;
List<string> commonValidEntries = new List<string> ();
List<string> cultureValidEntries = new List<string> ();
string fullCulturePath = $"{Directory.GetCurrentDirectory ()}/TaskTests/LocalizationIgnore/{culture}-Translations.ignore";
string shortCulturePath = $"xamarin-macios/tests/msbuild/Xamarin.MacDev.Tasks.Tests/TaskTests/LocalizationIgnore/{culture}-Translations.ignore";
CultureInfo originalCulture = Thread.CurrentThread.CurrentUICulture;
cultureIgnoreList = ReadFile (fullCulturePath);
foreach (var errorCodeInfo in typeof (MSBStrings).GetProperties ()) {
try {
var errorCode = errorCodeInfo.Name;
if (errorCode == "ResourceManager" || errorCode == "Culture")
continue;
string englishError = TranslateError ("en-US", errorCode);
string newCultureError = TranslateError (culture, errorCode);
if (commonIgnoreList.Contains (errorCode)) {
Assert.AreEqual (englishError, newCultureError, $"{errorCode} is translated. Remove {errorCode} from {shortCommonPath}");
commonValidEntries.Add (errorCode);
} else if (cultureIgnoreList.Contains (errorCode)) {
Assert.AreEqual (englishError, newCultureError, $"{errorCode} is translated. Remove {errorCode} from {shortCulturePath}");
cultureValidEntries.Add (errorCode);
} else if (englishError == newCultureError)
errorList.Append ($"{errorCode} ");
} finally {
Thread.CurrentThread.CurrentUICulture = originalCulture;
}
}
Assert.IsEmpty (errorList.ToString (), $"The following errors were not translated. Add them to {shortCommonPath} or {shortCulturePath}");
Assert.IsEmpty (cultureIgnoreList.Except (cultureValidEntries), $"Not all error codes in {shortCulturePath} are valid or are repeated. Please remove.");
Assert.IsEmpty (commonIgnoreList.Except (commonValidEntries), $"Not all error codes in {shortCommonPath} are valid or are repeated. Please remove.");
}
IList<string> ReadFile (string path)
{
if (!File.Exists (path))
return Array.Empty<string> ();
return File.ReadAllLines (path).Where (line => !line.StartsWith ("#", StringComparison.Ordinal) && line != string.Empty).ToList ();
}
}
}