From ebc7a5708b8f4a91ba23e85e451f0e08f3b51802 Mon Sep 17 00:00:00 2001 From: Thorsten Sommer Date: Mon, 26 Sep 2022 22:05:10 +0200 Subject: [PATCH] Added progress calculation for sections & texts; covered corner cases --- .../Processor/TranslationProcessor.cs | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/I18N Commander/Processor/TranslationProcessor.cs b/I18N Commander/Processor/TranslationProcessor.cs index 7e771b6..9de7cb8 100644 --- a/I18N Commander/Processor/TranslationProcessor.cs +++ b/I18N Commander/Processor/TranslationProcessor.cs @@ -60,6 +60,54 @@ public static class TranslationProcessor var numTotalTranslations = numCultures * numTextElements; var numTextElementsTranslated = await db.Translations.CountAsync(n => !string.IsNullOrWhiteSpace(n.Text)); + if(numTextElementsTranslated == 0 && numTotalTranslations == 0) + return new ProcessorResult(100); + + if (numTotalTranslations == 0) + return new ProcessorResult(0); + + if(numTextElementsTranslated > numTotalTranslations) + return new ProcessorResult(100); + + return new ProcessorResult(numTextElementsTranslated * 100 / numTotalTranslations); + } + + public static async Task> CalculateTranslationProgress(Section section) + { + await using var db = ProcessorMeta.ServiceProvider.GetRequiredService(); + var numCultures = (await AppSettings.GetCultureIndices()).Count; + var numTextElements = await db.TextElements.CountAsync(n => n.Section == section); + var numTotalTranslations = numCultures * numTextElements; + var numTextElementsTranslated = await db.Translations.CountAsync(n => !string.IsNullOrWhiteSpace(n.Text) && n.TextElement.Section == section); + + if(numTextElementsTranslated == 0 && numTotalTranslations == 0) + return new ProcessorResult(100); + + if (numTotalTranslations == 0) + return new ProcessorResult(0); + + if(numTextElementsTranslated > numTotalTranslations) + return new ProcessorResult(100); + + return new ProcessorResult(numTextElementsTranslated * 100 / numTotalTranslations); + } + + public static async Task> CalculateTranslationProgress(TextElement textElement) + { + await using var db = ProcessorMeta.ServiceProvider.GetRequiredService(); + var numCultures = (await AppSettings.GetCultureIndices()).Count; + var numTotalTranslations = numCultures; + var numTextElementsTranslated = await db.Translations.CountAsync(n => !string.IsNullOrWhiteSpace(n.Text) && n.TextElement == textElement); + + if(numTextElementsTranslated == 0 && numTotalTranslations == 0) + return new ProcessorResult(100); + + if (numTotalTranslations == 0) + return new ProcessorResult(0); + + if(numTextElementsTranslated > numTotalTranslations) + return new ProcessorResult(100); + return new ProcessorResult(numTextElementsTranslated * 100 / numTotalTranslations); } } \ No newline at end of file