From 6c679539536fc9df2a64f15279d35fc6c1398711 Mon Sep 17 00:00:00 2001 From: Victor Tran Date: Wed, 25 Sep 2024 12:10:08 +0000 Subject: [PATCH] Add comment functionality --- Parlance.ClientApp/src/interfaces/projects.ts | 1 + .../TranslationEditor/TranslationArea.tsx | 10 ++++++++ .../AppleStringsTranslationFile.cs | 1 + .../AppleStringsTranslationFileEntry.cs | 9 +++---- .../DotNetResourcesTranslationFile.cs | 3 ++- .../DotNetResourcesTranslationFileEntry.cs | 9 +++---- .../Gettext/GettextTranslationFileEntry.cs | 1 + .../I18NextJsonTranslationFileEntry.cs | 1 + .../IParlanceTranslationFileEntry.cs | 1 + .../QtLinguist/QtLinguistTranslationFile.cs | 24 +++++++++---------- .../QtLinguistTranslationFileEntry.cs | 15 ++++++++---- .../VueI18n/VueI18nTranslationFile.cs | 1 + .../VueI18n/VueI18nTranslationFileEntry.cs | 9 +++---- .../WebextensionJsonTranslationFile.cs | 1 + .../WebextensionJsonTranslationFileEntry.cs | 9 +++---- Parlance/Controllers/ProjectsController.cs | 2 +- 16 files changed, 62 insertions(+), 35 deletions(-) diff --git a/Parlance.ClientApp/src/interfaces/projects.ts b/Parlance.ClientApp/src/interfaces/projects.ts index dcc680d7..749b3723 100644 --- a/Parlance.ClientApp/src/interfaces/projects.ts +++ b/Parlance.ClientApp/src/interfaces/projects.ts @@ -49,6 +49,7 @@ export interface BaseEntry { source: string; oldSourceString?: string; translation: TranslationEntry; + comment?: string; } export interface BaseTranslationEntry { diff --git a/Parlance.ClientApp/src/pages/Projects/Subprojects/Languages/Translation/TranslationEditor/TranslationArea.tsx b/Parlance.ClientApp/src/pages/Projects/Subprojects/Languages/Translation/TranslationEditor/TranslationArea.tsx index 968fa1d6..01b917d9 100644 --- a/Parlance.ClientApp/src/pages/Projects/Subprojects/Languages/Translation/TranslationEditor/TranslationArea.tsx +++ b/Parlance.ClientApp/src/pages/Projects/Subprojects/Languages/Translation/TranslationEditor/TranslationArea.tsx @@ -456,6 +456,16 @@ export default function TranslationArea({ {entry.key} + {entry.comment && + +
+
+ {t("Comment from the developer")} +
+ {entry.comment} +
+
+ }
{ new() diff --git a/Parlance.Project/TranslationFiles/AppleStrings/AppleStringsTranslationFileEntry.cs b/Parlance.Project/TranslationFiles/AppleStrings/AppleStringsTranslationFileEntry.cs index b04f7191..999ce14b 100644 --- a/Parlance.Project/TranslationFiles/AppleStrings/AppleStringsTranslationFileEntry.cs +++ b/Parlance.Project/TranslationFiles/AppleStrings/AppleStringsTranslationFileEntry.cs @@ -2,9 +2,10 @@ namespace Parlance.Project.TranslationFiles.AppleStrings; public class AppleStringsTranslationFileEntry : IParlanceTranslationFileEntry { - public string Key { get; init; } - public string Source { get; init; } - public IList Translation { get; set; } - public string Context { get; init; } + public required string Key { get; init; } + public required string Source { get; init; } + public required IList Translation { get; set; } + public required string Context { get; init; } public bool RequiresPluralisation { get; set; } + public required string? Comment { get; init; } } \ No newline at end of file diff --git a/Parlance.Project/TranslationFiles/DotNetResources/DotNetResourcesTranslationFile.cs b/Parlance.Project/TranslationFiles/DotNetResources/DotNetResourcesTranslationFile.cs index 264fb838..64fef5da 100644 --- a/Parlance.Project/TranslationFiles/DotNetResources/DotNetResourcesTranslationFile.cs +++ b/Parlance.Project/TranslationFiles/DotNetResources/DotNetResourcesTranslationFile.cs @@ -100,7 +100,8 @@ private async Task LoadFile(string file, Locale locale, string baseFile, Locale Source = bases[key], Context = Path.GetFileName(file), RequiresPluralisation = false, - Translation = translationEntry + Translation = translationEntry, + Comment = null, }; }).Cast().ToList(); } diff --git a/Parlance.Project/TranslationFiles/DotNetResources/DotNetResourcesTranslationFileEntry.cs b/Parlance.Project/TranslationFiles/DotNetResources/DotNetResourcesTranslationFileEntry.cs index 2f8d683b..8f749f56 100644 --- a/Parlance.Project/TranslationFiles/DotNetResources/DotNetResourcesTranslationFileEntry.cs +++ b/Parlance.Project/TranslationFiles/DotNetResources/DotNetResourcesTranslationFileEntry.cs @@ -2,9 +2,10 @@ namespace Parlance.Project.TranslationFiles.DotNetResources; public class DotNetResourcesTranslationFileEntry : IParlanceTranslationFileEntry { - public string Key { get; set; } - public string Source { get; set; } - public IList Translation { get; set; } - public string Context { get; set; } + public required string Key { get; set; } + public required string Source { get; set; } + public required IList Translation { get; set; } + public required string Context { get; set; } public bool RequiresPluralisation { get; set; } + public required string? Comment { get; init; } } \ No newline at end of file diff --git a/Parlance.Project/TranslationFiles/Gettext/GettextTranslationFileEntry.cs b/Parlance.Project/TranslationFiles/Gettext/GettextTranslationFileEntry.cs index a4a46e6b..6082c297 100644 --- a/Parlance.Project/TranslationFiles/Gettext/GettextTranslationFileEntry.cs +++ b/Parlance.Project/TranslationFiles/Gettext/GettextTranslationFileEntry.cs @@ -14,4 +14,5 @@ public class GettextTranslationFileEntry : IParlanceTranslationFileEntry public string? RealContext { get; set; } public IList PreLines { get; set; } = new List(); public bool RequiresPluralisation { get; set; } + public string? Comment { get; set; } = ""; } diff --git a/Parlance.Project/TranslationFiles/I18nextJson/I18NextJsonTranslationFileEntry.cs b/Parlance.Project/TranslationFiles/I18nextJson/I18NextJsonTranslationFileEntry.cs index 1ff719a5..ffc9f6bb 100644 --- a/Parlance.Project/TranslationFiles/I18nextJson/I18NextJsonTranslationFileEntry.cs +++ b/Parlance.Project/TranslationFiles/I18nextJson/I18NextJsonTranslationFileEntry.cs @@ -12,4 +12,5 @@ public class I18NextJsonTranslationFileEntry : IParlanceTranslationFileEntry public IList Translation { get; set; } = null!; public string Context { get; set; } = null!; public bool RequiresPluralisation { get; set; } + public string? Comment { get; init; } } \ No newline at end of file diff --git a/Parlance.Project/TranslationFiles/IParlanceTranslationFileEntry.cs b/Parlance.Project/TranslationFiles/IParlanceTranslationFileEntry.cs index 0d07e2a5..4efbfcac 100644 --- a/Parlance.Project/TranslationFiles/IParlanceTranslationFileEntry.cs +++ b/Parlance.Project/TranslationFiles/IParlanceTranslationFileEntry.cs @@ -13,4 +13,5 @@ public interface IParlanceTranslationFileEntry public IList Translation { get; set; } public string Context { get; } public bool RequiresPluralisation { get; set; } + public string? Comment { get; } } \ No newline at end of file diff --git a/Parlance.Project/TranslationFiles/QtLinguist/QtLinguistTranslationFile.cs b/Parlance.Project/TranslationFiles/QtLinguist/QtLinguistTranslationFile.cs index 58204114..70fce59c 100644 --- a/Parlance.Project/TranslationFiles/QtLinguist/QtLinguistTranslationFile.cs +++ b/Parlance.Project/TranslationFiles/QtLinguist/QtLinguistTranslationFile.cs @@ -46,25 +46,23 @@ private async Task LoadFile(string file, Locale locale) RealKey = (string)msg.Element("source")! + "-" + (string)msg.Parent!.Element("name")!, Context = ((string)msg.Parent!.Element("name"))!, Source = (string)msg.Element("source")!, + TsComment = (string?)msg.Element("comment"), + TsExtraComment = (string?)msg.Element("extracomment"), Translation = msg.Attribute("numerus")?.Value == "yes" - // ? msg.Descendants("numerusform").Select((content, idx2) => new TranslationWithPluralType - // { - // PluralType = pluralRules[idx2].Category, - // TranslationContent = (string)content - // }).ToList() ? pluralRules.Select((rule, idx2) => new TranslationWithPluralType { PluralType = rule.Category, TranslationContent = pluralDescendants.Count > idx2 ? (string)pluralDescendants[idx2] : "" }).ToList() - : new List - { + : + [ new() { PluralType = "singular", TranslationContent = (string)msg.Element("translation")! } - }, + ], + Type = (string?)msg.Element("translation")!.Attribute("type") ?? "finished", RequiresPluralisation = msg.Attribute("numerus")?.Value == "yes", Locations = msg.Elements("location") .Where(loc => loc.Attribute("filename") is not null && loc.Attribute("line") is not null).Select( @@ -72,7 +70,7 @@ private async Task LoadFile(string file, Locale locale) new QtLinguistTranslationFileEntry.Location((string)loc.Attribute("filename")!, (string)loc.Attribute("line")!)) }; - }).Cast().ToList(); + }).Where(entry => entry.Type != "vanished").Cast().ToList(); } private protected override Task UseAsBaseImpl(string filename, Locale locale) @@ -102,7 +100,7 @@ public override async Task Save() var doc = new XDocument( new XElement("TS", new XAttribute("version", "2.1"), new XAttribute("language", _locale.ToUnderscored()), - Entries.GroupBy(entry => entry.Context).Select(context => + Entries.Cast().GroupBy(entry => entry.Context).Select(context => new XElement("context", new XElement("name", new XText(context.Key)), context.Select(entry => new XElement("message", @@ -120,10 +118,12 @@ public override async Task Save() : new XElement("translation", new XText(entry.Translation.Single(x => x.PluralType == "singular") .TranslationContent)), - ((QtLinguistTranslationFileEntry)entry).Locations.Select(location => + entry.Locations.Select(location => new XElement("location", new XAttribute("filename", location.Filename), new XAttribute("line", location.Line))), - new XElement("source", new XText(((QtLinguistTranslationFileEntry)entry).Source)) + new XElement("source", new XText(entry.Source)), + new XElement("comment", entry.TsComment), + new XElement("extracomment", entry.TsExtraComment) )) ) ) diff --git a/Parlance.Project/TranslationFiles/QtLinguist/QtLinguistTranslationFileEntry.cs b/Parlance.Project/TranslationFiles/QtLinguist/QtLinguistTranslationFileEntry.cs index 18ef1dcc..6ffe2323 100644 --- a/Parlance.Project/TranslationFiles/QtLinguist/QtLinguistTranslationFileEntry.cs +++ b/Parlance.Project/TranslationFiles/QtLinguist/QtLinguistTranslationFileEntry.cs @@ -1,5 +1,6 @@ using System.Security.Cryptography; using System.Text; +using System.Xml.Linq; namespace Parlance.Project.TranslationFiles.QtLinguist; @@ -8,10 +9,14 @@ public class QtLinguistTranslationFileEntry : IParlanceTranslationFileEntry public record Location(string Filename, string Line); public string Key => Convert.ToHexString(SHA256.HashData(Encoding.UTF8.GetBytes(RealKey))); - public string RealKey { get; init; } = null!; - public string Source { get; init; } = null!; - public IList Translation { get; set; } = null!; - public string Context { get; init; } = null!; + public required string RealKey { get; init; } + public required string Source { get; init; } + public required IList Translation { get; set; } + public required string Context { get; init; } public bool RequiresPluralisation { get; set; } - public IEnumerable Locations { get; init; } = null!; + public required string? TsComment { get; init; } + public required string? TsExtraComment { get; init; } + public string? Comment => TsComment ?? TsExtraComment; + public required IEnumerable Locations { get; init; } + public required string Type { get; set; } } \ No newline at end of file diff --git a/Parlance.Project/TranslationFiles/VueI18n/VueI18nTranslationFile.cs b/Parlance.Project/TranslationFiles/VueI18n/VueI18nTranslationFile.cs index 666fae1b..733f671e 100644 --- a/Parlance.Project/TranslationFiles/VueI18n/VueI18nTranslationFile.cs +++ b/Parlance.Project/TranslationFiles/VueI18n/VueI18nTranslationFile.cs @@ -122,6 +122,7 @@ private async Task LoadFile(string file, Locale locale, string baseFile, Locale { Key = path.ToString(), Source = isPlural ? source.Split(pluralSeparator).Last() : source, + Comment = null, Translation = isPlural ? translation?.Split(pluralSeparator).Where((x, i) => pluralRules.Length > i).Select( (x, i) => new TranslationWithPluralType diff --git a/Parlance.Project/TranslationFiles/VueI18n/VueI18nTranslationFileEntry.cs b/Parlance.Project/TranslationFiles/VueI18n/VueI18nTranslationFileEntry.cs index d2295308..0a70b68b 100644 --- a/Parlance.Project/TranslationFiles/VueI18n/VueI18nTranslationFileEntry.cs +++ b/Parlance.Project/TranslationFiles/VueI18n/VueI18nTranslationFileEntry.cs @@ -3,9 +3,10 @@ namespace Parlance.Project.TranslationFiles.VueI18n; // ReSharper disable once InconsistentNaming public class VueI18nTranslationFileEntry : IParlanceTranslationFileEntry { - public required string Key { get; set; } = null!; - public required string Source { get; set; } = null!; - public required IList Translation { get; set; } = null!; - public required string Context { get; set; } = null!; + public required string Key { get; set; } + public required string Source { get; set; } + public required IList Translation { get; set; } + public required string Context { get; set; } public required bool RequiresPluralisation { get; set; } + public required string? Comment { get; set; } } \ No newline at end of file diff --git a/Parlance.Project/TranslationFiles/WebextensionJson/WebextensionJsonTranslationFile.cs b/Parlance.Project/TranslationFiles/WebextensionJson/WebextensionJsonTranslationFile.cs index a2afce7e..0c190b64 100644 --- a/Parlance.Project/TranslationFiles/WebextensionJson/WebextensionJsonTranslationFile.cs +++ b/Parlance.Project/TranslationFiles/WebextensionJson/WebextensionJsonTranslationFile.cs @@ -65,6 +65,7 @@ private async Task LoadFile(string file, Locale locale, string baseFile, Locale Description = tItem?.description, Placeholders = tItem?.placeholders, RequiresPluralisation = false, + Comment = null, Translation = new List { new() diff --git a/Parlance.Project/TranslationFiles/WebextensionJson/WebextensionJsonTranslationFileEntry.cs b/Parlance.Project/TranslationFiles/WebextensionJson/WebextensionJsonTranslationFileEntry.cs index fdf261b9..b7243e62 100644 --- a/Parlance.Project/TranslationFiles/WebextensionJson/WebextensionJsonTranslationFileEntry.cs +++ b/Parlance.Project/TranslationFiles/WebextensionJson/WebextensionJsonTranslationFileEntry.cs @@ -4,11 +4,12 @@ namespace Parlance.Project.TranslationFiles.WebextensionJson; public class WebextensionJsonTranslationFileEntry : IParlanceTranslationFileEntry { - public string Key { get; set; } = null!; - public string Source { get; set; } = null!; - public IList Translation { get; set; } = null!; - public string Context { get; set; } = null!; + public required string Key { get; set; } + public required string Source { get; set; } + public required IList Translation { get; set; } + public required string Context { get; set; } public bool RequiresPluralisation { get; set; } + public required string? Comment { get; set; } public string? Description { get; set; } public JsonObject? Placeholders { get; set; } } \ No newline at end of file diff --git a/Parlance/Controllers/ProjectsController.cs b/Parlance/Controllers/ProjectsController.cs index b2c900ae..37c2445a 100644 --- a/Parlance/Controllers/ProjectsController.cs +++ b/Parlance/Controllers/ProjectsController.cs @@ -505,7 +505,7 @@ public async Task GetProjectEntries(string project, string subpro return Json(await Task.WhenAll(translationFile.Entries.Select(async entry => new { - entry.Key, entry.Context, entry.Source, entry.Translation, entry.RequiresPluralisation, + entry.Key, entry.Context, entry.Source, entry.Translation, entry.RequiresPluralisation, entry.Comment, OldSourceString = await sourceStringsService.GetSourceStringChange(subprojectLanguage, entry) }))); }