Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
+ Added KWS Catalog project
  • Loading branch information
nd1012 committed Mar 13, 2024
1 parent 51e7cc6 commit 258c1fe
Show file tree
Hide file tree
Showing 8 changed files with 549 additions and 1 deletion.
88 changes: 88 additions & 0 deletions src/wan24-I8NKws/KwsCatalog.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
using System.ComponentModel.DataAnnotations;
using wan24.Core;
using wan24.ObjectValidation;

namespace wan24.I8NKws
{
/// <summary>
/// KWS catalog
/// </summary>
public sealed class KwsCatalog
{
/// <summary>
/// Constructor
/// </summary>
public KwsCatalog() { }

/// <summary>
/// Project name
/// </summary>
public string Project { get; set; } = string.Empty;

/// <summary>
/// Created time (UTC)
/// </summary>
public DateTime Created { get; set; } = DateTime.UtcNow;

/// <summary>
/// Modified time (UTC)
/// </summary>
public DateTime Modified { get; set; } = DateTime.UtcNow;

/// <summary>
/// Translator name
/// </summary>
public string Translator { get; set; } = string.Empty;

/// <summary>
/// Locale identifier
/// </summary>
[RegularExpression(RegularExpressions.LOCALE_WITH_DASH)]
public string Locale { get; set; } = "en-US";

/// <summary>
/// If the text is written right to left
/// </summary>
public bool RightToLeft { get; set; }

/// <summary>
/// Keywords
/// </summary>
public HashSet<KwsKeyword> Keywords { get; } = [];

/// <summary>
/// Validate the catalog
/// </summary>
/// <param name="throwOnError">Throw an exception on error?</param>
/// <param name="requireCompleteTranslations">Require all translations to be complete?</param>
/// <returns>If the catalog is valid</returns>
/// <exception cref="InvalidDataException">Catalog is invalid</exception>
public bool Validate(in bool throwOnError = true, in bool requireCompleteTranslations = false)
{
if (Keywords.Count == 0)
{
if (!throwOnError) return false;
throw new InvalidDataException("Missing keywords");
}
foreach (KwsKeyword keyword in Keywords)
{
if (string.IsNullOrEmpty(keyword.ID))
{
if (!throwOnError) return false;
throw new InvalidDataException("Found keyword with missing ID");
}
if (requireCompleteTranslations && keyword.TranslationMissing)
{
if (!throwOnError) return false;
throw new InvalidDataException($"Missing translation of ID \"{keyword.ID}\"");
}
}
if(!this.TryValidateObject(out List<ValidationResult> results, throwOnError: false))
{
if (!throwOnError) return false;
throw new InvalidDataException($"Found {results.Count} object errors - first error: {results.First().ErrorMessage}");
}
return true;
}
}
}
122 changes: 122 additions & 0 deletions src/wan24-I8NKws/KwsKeyword.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
using System.ComponentModel.DataAnnotations;
using System.Text.Json.Serialization;

namespace wan24.I8NKws
{
/// <summary>
/// KWS keyword record
/// </summary>
public sealed record class KwsKeyword
{
/// <summary>
/// Constructor
/// </summary>
public KwsKeyword() { }

/// <summary>
/// Constructor
/// </summary>
/// <param name="id">ID</param>
public KwsKeyword(in string id)
{
if (id.Length < 1) throw new ArgumentException("ID required", nameof(id));
ID = id;
}

/// <summary>
/// ID (keyword)
/// </summary>
[MinLength(1)]
public string ID { get; private set; } = null!;

/// <summary>
/// Previous IDs (extended when the ID is being updated; last entry was the latest ID)
/// </summary>
public HashSet<string> PreviousIds { get; private set; } = [];

/// <summary>
/// Extracted time (UTC)
/// </summary>
public DateTime Extracted { get; set; } = DateTime.UtcNow;

/// <summary>
/// Updated time (UTC)
/// </summary>
public DateTime Updated { get; set; } = DateTime.UtcNow;

/// <summary>
/// Latest translator name
/// </summary>
public string Translator { get; set; } = string.Empty;

/// <summary>
/// Developer comments
/// </summary>
public string DeveloperComments { get; set; } = string.Empty;

/// <summary>
/// Translator comments
/// </summary>
public string TranslatorComments { get; set; } = string.Empty;

/// <summary>
/// If this keyword is obsolete and should not be exported
/// </summary>
public bool Obsolete { get; set; }

/// <summary>
/// If the update of the ID has been done automatic using fuzzy logic search
/// </summary>
public bool Fuzzy { get; set; }

/// <summary>
/// If a translation is missing (no translation at all, or any empty translations)
/// </summary>
[JsonIgnore]
public bool TranslationMissing => Translations.Count == 0 || Translations.Any(t => t.Length == 0);

/// <summary>
/// Translations
/// </summary>
public List<string> Translations { get; } = [];

/// <summary>
/// Source references
/// </summary>
public HashSet<string> SourceReferences { get; } = [];

/// <summary>
/// Update the ID
/// </summary>
/// <param name="newId">New ID</param>
public void UpdateId(in string newId)
{
if (Obsolete) throw new InvalidOperationException();
if (newId.Length < 1) throw new ArgumentException("ID required", nameof(newId));
string oldId = ID;
if (newId == oldId) return;
ID = newId;
PreviousIds.Remove(oldId);
PreviousIds.Add(oldId);
}

/// <summary>
/// Undo an ID update
/// </summary>
/// <param name="id">Target ID to use</param>
public void UndoIdUpdate(string? id = null)
{
if (Obsolete || PreviousIds.Count == 0) throw new InvalidOperationException();
if (id is null)
{
id = PreviousIds.Last();
}
else if (!PreviousIds.Contains(id))
{
throw new ArgumentException("Unknown previous ID", nameof(id));
}
ID = id;
PreviousIds = [.. PreviousIds.SkipWhile(pid => pid != id).Skip(1)];
}
}
}
27 changes: 27 additions & 0 deletions src/wan24-I8NKws/KwsSourceReference.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System.ComponentModel.DataAnnotations;

namespace wan24.I8NKws
{
/// <summary>
/// KWS source reference
/// </summary>
public sealed record class KwsSourceReference
{
/// <summary>
/// Constructor
/// </summary>
public KwsSourceReference() { }

/// <summary>
/// Filename
/// </summary>
[MinLength(1)]
public required string FileName { get; init; }

/// <summary>
/// Line number (starts with <c>1</c>)
/// </summary>
[Range(1, int.MaxValue)]
public required int LineNumber { get; init; }
}
}
21 changes: 21 additions & 0 deletions src/wan24-I8NKws/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Andreas Zimmermann, wan24.de

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Loading

0 comments on commit 258c1fe

Please sign in to comment.