Skip to content

Commit

Permalink
* prepare refacture of
Browse files Browse the repository at this point in the history
   language defaults
  • Loading branch information
festo-i40 committed Sep 3, 2024
1 parent 3c0aec1 commit 13d9a5e
Showing 1 changed file with 120 additions and 0 deletions.
120 changes: 120 additions & 0 deletions src/AasxIntegrationBase/AasxLanguageHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
/// <summary>
/// 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.
/// </summary>
public class AasxLanguageTuple
{
/// <summary>
/// According ISO 639-2, 2 digit language code
/// </summary>
public string LangCode;

/// <summary>
/// According ISO 3166, 2 digit country code
/// </summary>
public string CountryCode;

/// <summary>
/// Marks the wildcard element
/// </summary>
public bool IsAny()
{
return LangCode == "" && CountryCode == ""
|| LangCode == "All" && CountryCode == "All";
}
}

/// <summary>
/// Maintains a list of language tuples.
/// Note: Is always defaulted with a list of language tuples.
/// Could be cleard/ added.
/// </summary>
public class AasxLanguageTupleSet : MultiValueDictionary<string, AasxLanguageTuple>
{
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 });
}
}

/// <summary>
/// Rationale for default languages/ countries: member in IDTA or IEC TC65 WG24
/// </summary>
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<AasxLanguageTuple> 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<AasxLanguageTuple> 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<string> GetAllLanguages()
{
foreach (var tp in this.Values)
if (!tp.IsAny())
yield return tp.LangCode;
}

public IEnumerable<string> 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 = {
Expand Down

0 comments on commit 13d9a5e

Please sign in to comment.