From 13d9a5e551c6f6f00ad5d8556a5352c118ad6a3e Mon Sep 17 00:00:00 2001 From: Michael Hoffmeister Date: Tue, 3 Sep 2024 17:35:37 +0200 Subject: [PATCH] * prepare refacture of language defaults --- src/AasxIntegrationBase/AasxLanguageHelper.cs | 120 ++++++++++++++++++ 1 file changed, 120 insertions(+) diff --git a/src/AasxIntegrationBase/AasxLanguageHelper.cs b/src/AasxIntegrationBase/AasxLanguageHelper.cs index d1c1ac72..a2d4a0b0 100644 --- a/src/AasxIntegrationBase/AasxLanguageHelper.cs +++ b/src/AasxIntegrationBase/AasxLanguageHelper.cs @@ -7,16 +7,136 @@ This source code is licensed under the Apache License 2.0 (see LICENSE.txt). This source code may use other Open Source software components (see LICENSE.txt). */ +using AdminShellNS; using System; using System.Collections.Generic; +using System.Collections.Immutable; using System.Linq; using System.Text; +using System.Text.RegularExpressions; using System.Threading.Tasks; namespace AasxIntegrationBase { + /// + /// This class lists valid combinations of ISO 639 (2 digit language codes) + /// and ISO 3166 (2 digit country codes). Only 1:1 relations are modelled. + /// + public class AasxLanguageTuple + { + /// + /// According ISO 639-2, 2 digit language code + /// + public string LangCode; + + /// + /// According ISO 3166, 2 digit country code + /// + public string CountryCode; + + /// + /// Marks the wildcard element + /// + public bool IsAny() + { + return LangCode == "" && CountryCode == "" + || LangCode == "All" && CountryCode == "All"; + } + } + + /// + /// Maintains a list of language tuples. + /// Note: Is always defaulted with a list of language tuples. + /// Could be cleard/ added. + /// + public class AasxLanguageTupleSet : MultiValueDictionary + { + public void Add(string lang, string country) + { + lang = lang?.ToLower().Trim(); + country = country?.ToLower().Trim(); + + if (lang != null && lang != "" + && country != null && country != "") + { + this.Add(lang, new AasxLanguageTuple() { LangCode = lang, CountryCode = country }); + } + } + + /// + /// Rationale for default languages/ countries: member in IDTA or IEC TC65 WG24 + /// + public void Init() + { + this.Clear(); + Add("All", "All"); + Add("en", "GB"); + Add("en", "US"); + Add("de", "DE"); + Add("de", "CH"); + Add("de", "AT"); + Add("es", "ES"); + Add("fi", "FI"); + Add("fr", "FR"); + Add("it", "IT"); + Add("ja", "JP"); + Add("ko", "KR"); + Add("nl", "NL"); + Add("no", "NO"); + Add("pt", "PT"); + Add("sv", "SE"); + Add("zh", "CN"); + } + + public IEnumerable FindByLang(string lang) + { + lang = lang?.ToLower().Trim(); + if (lang != null && lang != "" || this.ContainsKey(lang) == false) + yield break; + foreach (var x in this[lang]) + yield return x; + } + + public IEnumerable FindByCountry(string country) + { + country = country?.ToLower().Trim(); + if (country != null && country != "") + yield break; + foreach (var tp in this.Values) + if (country == tp.CountryCode) + yield return tp; + } + + public IEnumerable GetAllLanguages() + { + foreach (var tp in this.Values) + if (!tp.IsAny()) + yield return tp.LangCode; + } + + public IEnumerable GetAllCountries() + { + foreach (var tp in this.Values) + if (!tp.IsAny()) + yield return tp.CountryCode; + } + + public void InitByCustomString(string input) + { + this.Clear(); + var pairs = input.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); + foreach (var pair in pairs) + { + var m = Regex.Match(pair, @"(\w+)\s*/\s*(\w+)"); + if (m.Success) + Add(m.Groups[1].ToString(), m.Groups[2].ToString()); + } + } + } + public static class AasxLanguageHelper { + public enum LangEnum { Any = 0, EN, DE, ZH, JA, KO, FR, ES }; public static string[] LangEnumToISO639String = {