2021-05-20 15:11:43 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
|
|
|
namespace Frontend.Translations.Tests
|
|
|
|
{
|
|
|
|
public class Tests
|
|
|
|
{
|
|
|
|
public static string BasePath
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return "..\\..\\..\\..\\..\\..\\";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<string> Workspaces { get; set; }
|
|
|
|
|
|
|
|
public List<TranslationFile> TranslationFiles { get; set; }
|
|
|
|
public List<JavaScriptFile> JavaScriptFiles { get; set; }
|
|
|
|
|
2021-05-27 12:51:44 +00:00
|
|
|
public List<ModuleFolder> ModuleFolders { get; set; }
|
|
|
|
|
2021-05-20 15:11:43 +00:00
|
|
|
[SetUp]
|
|
|
|
public void Setup()
|
|
|
|
{
|
|
|
|
var packageJsonPath = Path.Combine(BasePath, @"package.json");
|
|
|
|
var jsonPackage = JObject.Parse(File.ReadAllText(packageJsonPath));
|
|
|
|
|
2021-05-27 12:51:44 +00:00
|
|
|
var moduleWorkspaces = ((JArray)jsonPackage["workspaces"]).Select(p => ((string)p).Replace("/", "\\")).ToList();
|
|
|
|
|
|
|
|
Workspaces = new List<string>();
|
|
|
|
|
|
|
|
Workspaces.AddRange(moduleWorkspaces);
|
2021-05-20 15:11:43 +00:00
|
|
|
|
|
|
|
Workspaces.Add("public\\locales");
|
|
|
|
|
|
|
|
var translationFiles = from wsPath in Workspaces
|
|
|
|
let clientDir = Path.Combine(BasePath, wsPath)
|
|
|
|
from file in Directory.EnumerateFiles(clientDir, "*.json", SearchOption.AllDirectories)
|
|
|
|
where file.Contains("public\\locales\\")
|
|
|
|
select file;
|
|
|
|
|
|
|
|
TranslationFiles = new List<TranslationFile>();
|
|
|
|
|
|
|
|
foreach (var path in translationFiles)
|
|
|
|
{
|
|
|
|
var jsonTranslation = JObject.Parse(File.ReadAllText(path));
|
|
|
|
|
|
|
|
var translationFile = new TranslationFile(path);
|
|
|
|
|
2021-05-21 12:27:56 +00:00
|
|
|
translationFile.Translations = jsonTranslation.Properties()
|
|
|
|
.Select(p => new TranslationItem(p.Name, (string)p.Value))
|
|
|
|
.ToList();
|
2021-05-20 15:11:43 +00:00
|
|
|
|
|
|
|
TranslationFiles.Add(translationFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
var javascriptFiles = (from wsPath in Workspaces
|
|
|
|
let clientDir = Path.Combine(BasePath, wsPath)
|
|
|
|
from file in Directory.EnumerateFiles(clientDir, "*.js", SearchOption.AllDirectories)
|
2021-05-21 12:39:49 +00:00
|
|
|
where !file.Contains("dist\\")
|
2021-05-20 15:11:43 +00:00
|
|
|
select file)
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
javascriptFiles.AddRange(from wsPath in Workspaces
|
|
|
|
let clientDir = Path.Combine(BasePath, wsPath)
|
|
|
|
from file in Directory.EnumerateFiles(clientDir, "*.jsx", SearchOption.AllDirectories)
|
2021-05-21 12:39:49 +00:00
|
|
|
where !file.Contains("dist\\")
|
2021-05-20 15:11:43 +00:00
|
|
|
select file);
|
|
|
|
|
|
|
|
JavaScriptFiles = new List<JavaScriptFile>();
|
|
|
|
|
2021-05-21 11:38:11 +00:00
|
|
|
var pattern1 = "[.{\\s\\(]t\\(\\s*[\"\'`]([a-zA-Z0-9_.:_\\s{}/_-]+)[\"\'`]\\s*[\\),]";
|
2021-05-20 15:11:43 +00:00
|
|
|
var pattern2 = "i18nKey=\"([a-zA-Z0-9_.-]+)\"";
|
|
|
|
|
|
|
|
var regexp = new Regex($"({pattern1})|({pattern2})", RegexOptions.Multiline | RegexOptions.ECMAScript);
|
|
|
|
|
|
|
|
foreach (var path in javascriptFiles)
|
|
|
|
{
|
|
|
|
var jsFileText = File.ReadAllText(path);
|
|
|
|
|
|
|
|
var matches = regexp.Matches(jsFileText);
|
|
|
|
|
2021-05-21 12:27:56 +00:00
|
|
|
var translationKeys = matches
|
|
|
|
.Select(m => m.Groups[2].Value == ""
|
|
|
|
? m.Groups[4].Value
|
|
|
|
: m.Groups[2].Value)
|
|
|
|
.ToList();
|
2021-05-20 15:11:43 +00:00
|
|
|
|
|
|
|
if (!translationKeys.Any())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
var jsFile = new JavaScriptFile(path);
|
|
|
|
|
|
|
|
jsFile.TranslationKeys = translationKeys;
|
|
|
|
|
|
|
|
JavaScriptFiles.Add(jsFile);
|
|
|
|
}
|
2021-05-27 12:51:44 +00:00
|
|
|
|
|
|
|
ModuleFolders = new List<ModuleFolder>();
|
|
|
|
|
|
|
|
var list = TranslationFiles
|
|
|
|
//.Where(file => file.Language == "en")
|
|
|
|
.Select(t => new
|
|
|
|
{
|
|
|
|
ModulePath = moduleWorkspaces.FirstOrDefault(m => t.Path.Contains(m)),
|
|
|
|
Language = new LanguageItem
|
|
|
|
{
|
|
|
|
Path = t.Path,
|
|
|
|
Language = t.Language,
|
|
|
|
Translations = t.Translations
|
|
|
|
},
|
|
|
|
lng = t.Language
|
|
|
|
}).ToList();
|
|
|
|
|
|
|
|
var moduleTranslations = list
|
|
|
|
.GroupBy(t => t.ModulePath)
|
|
|
|
.Select(g => new
|
|
|
|
{
|
|
|
|
ModulePath = g.Key,
|
|
|
|
Languages = g.ToList().Select(t => t.Language).ToList()
|
|
|
|
.ToList()
|
|
|
|
})
|
|
|
|
//.GroupBy(t => new { t.ModulePath, t.Language.Language })
|
|
|
|
//.Select(g => new
|
|
|
|
//{
|
|
|
|
// ModulePath = g.Key.ModulePath,
|
|
|
|
// Languages = g.ToList()
|
|
|
|
//})
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
var moduleJsTranslatedFiles = JavaScriptFiles
|
|
|
|
.Select(t => new
|
|
|
|
{
|
|
|
|
ModulePath = moduleWorkspaces.FirstOrDefault(m => t.Path.Contains(m)),
|
|
|
|
t.Path,
|
|
|
|
t.TranslationKeys
|
|
|
|
})
|
|
|
|
.GroupBy(t => t.ModulePath)
|
|
|
|
.Select(g => new
|
|
|
|
{
|
|
|
|
ModulePath = g.Key,
|
|
|
|
TranslationKeys = g.ToList().SelectMany(t => t.TranslationKeys).ToList()
|
|
|
|
})
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
foreach (var ws in moduleWorkspaces)
|
|
|
|
{
|
|
|
|
var t = moduleTranslations.FirstOrDefault(t => t.ModulePath == ws);
|
|
|
|
var j = moduleJsTranslatedFiles.FirstOrDefault(t => t.ModulePath == ws);
|
|
|
|
|
|
|
|
if (j == null && t == null)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
ModuleFolders.Add(new ModuleFolder
|
|
|
|
{
|
|
|
|
Path = ws,
|
|
|
|
AvailableLanguages = t?.Languages,
|
|
|
|
AppliedJsTranslationKeys = j?.TranslationKeys
|
|
|
|
});
|
|
|
|
}
|
2021-05-20 15:11:43 +00:00
|
|
|
}
|
|
|
|
|
2021-05-25 14:19:05 +00:00
|
|
|
[Test]
|
|
|
|
public void FullDublicatesTest()
|
|
|
|
{
|
|
|
|
var fullEnDuplicates = TranslationFiles
|
|
|
|
.Where(file => file.Language == "en")
|
|
|
|
.SelectMany(item => item.Translations)
|
|
|
|
.GroupBy(t => new { t.Key, t.Value })
|
|
|
|
.Where(grp => grp.Count() > 1)
|
|
|
|
.Select(grp => new { Key = grp.Key, Count = grp.Count(), Keys = grp.ToList() })
|
|
|
|
//.Where(grp => grp.Grouped.GroupBy(n => n.Key).Any(c => c.Count() > 1))
|
|
|
|
.OrderByDescending(itm => itm.Count)
|
|
|
|
.Select(grp => new { Key = grp.Key.Key, Value = grp.Key.Value, grp.Count })
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
Assert.AreEqual(0, fullEnDuplicates.Count, string.Join("\r\n", fullEnDuplicates.Select(d => JObject.FromObject(d).ToString())));
|
|
|
|
}
|
|
|
|
|
2021-05-20 15:11:43 +00:00
|
|
|
[Test]
|
|
|
|
public void DublicatesByContentTest()
|
|
|
|
{
|
2021-05-25 14:19:05 +00:00
|
|
|
var allRuTranslations = TranslationFiles
|
|
|
|
.Where(file => file.Language == "ru")
|
|
|
|
.SelectMany(item => item.Translations)
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
var allEnDuplicates = TranslationFiles
|
|
|
|
.Where(file => file.Language == "en")
|
2021-05-20 15:11:43 +00:00
|
|
|
.SelectMany(item => item.Translations)
|
|
|
|
.GroupBy(t => t.Value)
|
2021-05-21 08:05:37 +00:00
|
|
|
.Where(grp => grp.Count() > 1)
|
2021-05-25 14:19:05 +00:00
|
|
|
.Select(grp => new { ContentKey = grp.Key, Count = grp.Count(), List = grp.ToList() })
|
2021-05-21 08:05:37 +00:00
|
|
|
//.Where(grp => grp.Grouped.GroupBy(n => n.Key).Any(c => c.Count() > 1))
|
2021-05-20 15:11:43 +00:00
|
|
|
.OrderByDescending(itm => itm.Count)
|
|
|
|
.ToList();
|
|
|
|
|
2021-05-25 14:19:05 +00:00
|
|
|
var duplicatesKeys = new List<TranslationItem>();
|
|
|
|
|
|
|
|
foreach (var item in allEnDuplicates)
|
|
|
|
{
|
|
|
|
var ruEquivalents = allRuTranslations
|
|
|
|
.Where(t => item.List.Select(k => k.Key).Contains(t.Key))
|
|
|
|
.GroupBy(t => t.Value)
|
|
|
|
.Select(grp => new
|
|
|
|
{
|
|
|
|
ContentKey = grp.Key,
|
|
|
|
Count = grp.Count(),
|
|
|
|
Keys = grp.Select(k => k.Key).ToList()
|
|
|
|
})
|
|
|
|
.Where(t => t.Count > 1)
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
if (!ruEquivalents.Any())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
duplicatesKeys.AddRange(
|
|
|
|
item.List.Where(item => ruEquivalents
|
|
|
|
.SelectMany(k => k.Keys)
|
|
|
|
.Any(k => k == item.Key)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
var duplicates = duplicatesKeys
|
|
|
|
.GroupBy(k => k.Value)
|
|
|
|
.Select(g => new { ContentKey = g.Key, Count = g.Count(), Keys = g.ToList() })
|
|
|
|
.ToList();
|
|
|
|
|
2021-05-27 13:37:38 +00:00
|
|
|
/*== Save dublicates.json ==*/
|
|
|
|
/* var listForSave = duplicates
|
|
|
|
.SelectMany(g => g.Keys)
|
|
|
|
.GroupBy(item => item.Key)
|
|
|
|
.Select(grp => new
|
|
|
|
{
|
|
|
|
Key = grp.Key,
|
|
|
|
Value = grp.FirstOrDefault().Value
|
|
|
|
})
|
|
|
|
.OrderByDescending(t => t.Value);
|
|
|
|
|
|
|
|
string json = JsonSerializer.Serialize(listForSave);
|
|
|
|
json = json.Replace("\"Key\":", "").Replace(",\"Value\"", "").Replace("},{", ",");
|
|
|
|
json = json.Substring(1, json.Length - 2);
|
|
|
|
File.WriteAllText(@"D:\dublicates.json", json);*/
|
2021-05-24 14:40:29 +00:00
|
|
|
|
2021-05-25 14:19:05 +00:00
|
|
|
Assert.AreEqual(0, duplicates.Count, string.Join(", ", duplicates.Select(d => JObject.FromObject(d).ToString())));
|
2021-05-20 15:11:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void NotAllLanguageTranslatedTest()
|
|
|
|
{
|
|
|
|
var groupedByLng = TranslationFiles
|
|
|
|
.GroupBy(t => t.Language)
|
2021-05-27 09:15:45 +00:00
|
|
|
.Select(grp => new { Lng = grp.Key, Count = grp.Count(), Files = grp.ToList() })
|
2021-05-20 15:11:43 +00:00
|
|
|
.ToList();
|
|
|
|
|
|
|
|
var enGroup = groupedByLng.Find(f => f.Lng == "en");
|
2021-05-21 12:27:56 +00:00
|
|
|
var expectedCount = enGroup.Count;
|
2021-05-20 15:11:43 +00:00
|
|
|
|
2021-05-21 12:27:56 +00:00
|
|
|
var otherLngs = groupedByLng.Where(g => g.Lng != "en");
|
|
|
|
|
|
|
|
var incompleteList = otherLngs
|
|
|
|
.Where(lng => lng.Count != expectedCount)
|
2021-05-27 09:15:45 +00:00
|
|
|
.Select(lng => new { Issue = $"Language '{lng.Lng}' (Count={lng.Count}). Not found files:\r\n", lng.Lng, lng.Files })
|
2021-05-21 12:27:56 +00:00
|
|
|
.ToList();
|
|
|
|
|
2021-05-27 09:15:45 +00:00
|
|
|
var message = $"Next languages are not equal 'en' (Count= {expectedCount}) by translated files count:\r\n\r\n";
|
|
|
|
|
|
|
|
if (incompleteList.Count > 0)
|
|
|
|
{
|
|
|
|
var enFilePaths = enGroup.Files.Select(f => f.Path);
|
|
|
|
|
|
|
|
for (int i = 0; i < incompleteList.Count; i++)
|
|
|
|
{
|
|
|
|
var lng = incompleteList[i];
|
|
|
|
|
2021-05-27 13:37:38 +00:00
|
|
|
message += $"{i}. {lng.Issue}\r\n";
|
2021-05-27 09:15:45 +00:00
|
|
|
|
|
|
|
var lngFilePaths = lng.Files.Select(f => f.Path).ToList();
|
|
|
|
|
|
|
|
var notFoundFilePaths = enFilePaths
|
|
|
|
.Select(p => p.Replace("\\en\\", $"\\{lng.Lng}\\"))
|
|
|
|
.Where(p => !lngFilePaths.Contains(p));
|
|
|
|
|
|
|
|
message += string.Join("\r\n", notFoundFilePaths);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Assert.AreEqual(0, incompleteList.Count, message);
|
2021-05-20 15:11:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void NotTranslatedKeysTest()
|
|
|
|
{
|
|
|
|
var groupedByLng = TranslationFiles
|
|
|
|
.GroupBy(t => t.Language)
|
|
|
|
.Select(grp => new
|
|
|
|
{
|
|
|
|
Lng = grp.Key,
|
|
|
|
Keys = grp
|
|
|
|
.SelectMany(k => k.Translations)
|
|
|
|
.OrderByDescending(itm => itm.Key)
|
2021-05-21 12:27:56 +00:00
|
|
|
.ToList()
|
2021-05-20 15:11:43 +00:00
|
|
|
})
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
var enGroup = groupedByLng.Find(f => f.Lng == "en");
|
|
|
|
|
2021-05-21 12:27:56 +00:00
|
|
|
var expectedCount = enGroup.Keys.Count;
|
2021-05-20 15:11:43 +00:00
|
|
|
|
2021-05-21 12:27:56 +00:00
|
|
|
var otherLngs = groupedByLng.Where(g => g.Lng != "en");
|
|
|
|
|
|
|
|
var incompleteList = otherLngs
|
|
|
|
.Where(lng => lng.Keys.Count != expectedCount)
|
2021-05-27 13:37:38 +00:00
|
|
|
.Select(lng => new { Issue = $"Language '{lng.Lng}' (Count={lng.Keys.Count}). Not found keys:\r\n", lng.Lng, lng.Keys })
|
2021-05-21 12:27:56 +00:00
|
|
|
.ToList();
|
|
|
|
|
2021-05-27 13:37:38 +00:00
|
|
|
var message = $"Next languages are not equal 'en' (Count= {expectedCount}) by translated keys count:\r\n\r\n";
|
|
|
|
|
|
|
|
if (incompleteList.Count > 0)
|
|
|
|
{
|
|
|
|
var enKeys = enGroup.Keys.Select(f => f.Key);
|
|
|
|
|
|
|
|
for (int i = 0; i < incompleteList.Count; i++)
|
|
|
|
{
|
|
|
|
var lng = incompleteList[i];
|
|
|
|
|
|
|
|
message += $"{i}. {lng.Issue}\r\n";
|
|
|
|
|
|
|
|
var lngKeys = lng.Keys.Select(f => f.Key).ToList();
|
|
|
|
|
|
|
|
var notFoundKeys = enKeys.Except(lngKeys);
|
|
|
|
|
|
|
|
message += string.Join("\r\n", notFoundKeys) + "\r\n\r\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Assert.AreEqual(0, incompleteList.Count, message);
|
2021-05-20 15:11:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void NotFoundKeysTest()
|
|
|
|
{
|
|
|
|
var allEnKeys = TranslationFiles
|
2021-05-25 14:19:05 +00:00
|
|
|
.Where(file => file.Language == "en")
|
2021-05-20 15:11:43 +00:00
|
|
|
.SelectMany(item => item.Translations)
|
|
|
|
.Select(item => item.Key);
|
|
|
|
|
|
|
|
var allJsTranslationKeys = JavaScriptFiles
|
|
|
|
.SelectMany(j => j.TranslationKeys)
|
2021-05-27 08:27:27 +00:00
|
|
|
.Select(k => k.Replace("Common:", "").Replace("Translations:", ""))
|
2021-05-20 15:11:43 +00:00
|
|
|
.Distinct();
|
|
|
|
|
|
|
|
var notFoundJsKeys = allJsTranslationKeys.Except(allEnKeys);
|
|
|
|
|
2021-05-21 12:27:56 +00:00
|
|
|
Assert.AreEqual(0, notFoundJsKeys.Count(),
|
|
|
|
"Some i18n-keys are not exist in translations in 'en' language: Keys: '{0}'",
|
2021-05-25 14:19:05 +00:00
|
|
|
string.Join("\r\n", notFoundJsKeys));
|
2021-05-20 15:11:43 +00:00
|
|
|
}
|
2021-05-21 10:34:35 +00:00
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void UselessTranslationKeysTest()
|
|
|
|
{
|
|
|
|
var allEnKeys = TranslationFiles
|
2021-05-25 14:19:05 +00:00
|
|
|
.Where(file => file.Language == "en")
|
2021-05-21 10:34:35 +00:00
|
|
|
.SelectMany(item => item.Translations)
|
|
|
|
.Select(item => item.Key)
|
|
|
|
.Where(k => !k.StartsWith("Culture_"));
|
|
|
|
|
|
|
|
var allJsTranslationKeys = JavaScriptFiles
|
|
|
|
.SelectMany(j => j.TranslationKeys)
|
2021-05-27 08:27:27 +00:00
|
|
|
.Select(k => k.Replace("Common:", "").Replace("Translations:", ""))
|
2021-05-21 10:34:35 +00:00
|
|
|
.Where(k => !k.StartsWith("Culture_"))
|
|
|
|
.Distinct();
|
|
|
|
|
|
|
|
var notFoundi18nKeys = allEnKeys.Except(allJsTranslationKeys);
|
|
|
|
|
2021-05-21 12:27:56 +00:00
|
|
|
Assert.AreEqual(0, notFoundi18nKeys.Count(),
|
2021-05-26 07:50:14 +00:00
|
|
|
"Some i18n-keys are not found in js: \r\nKeys: '\r\n{0}'",
|
2021-05-25 14:19:05 +00:00
|
|
|
string.Join("\r\n", notFoundi18nKeys));
|
2021-05-21 10:34:35 +00:00
|
|
|
}
|
2021-05-20 15:11:43 +00:00
|
|
|
}
|
|
|
|
}
|