Skip to content

Commit

Permalink
* w.r.t to issue #34
Browse files Browse the repository at this point in the history
* working
  • Loading branch information
festo-i40 committed Sep 5, 2024
1 parent 13d9a5e commit 8ba9420
Show file tree
Hide file tree
Showing 16 changed files with 161 additions and 99 deletions.
4 changes: 2 additions & 2 deletions src/AasxIntegrationBase/AasForms/FormInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -932,7 +932,7 @@ public static AnyUiGrid RenderAnyUiRefAttribs(
uitk.AddSmallComboBoxTo(g, row, 2, margin: new AnyUiThickness(1.0),
horizontalAlignment: AnyUiHorizontalAlignment.Stretch,
text: "" + ls.Language,
items: AasxLanguageHelper.GetLangCodes().ToArray()),
items: AasxLanguageHelper.Languages.GetAllLanguages().ToArray()),
(o) =>
{
if (o is string os)
Expand Down Expand Up @@ -1698,7 +1698,7 @@ public override void RenderAnyUi(AnyUiStackPanel view, AnyUiSmallWidgetToolkit u
uitk.AddSmallComboBoxTo(g, row, 1, margin: new AnyUiThickness(1.0),
horizontalAlignment: AnyUiHorizontalAlignment.Stretch,
text: "" + ls.Language,
items: AasxLanguageHelper.GetLangCodes().ToArray()),
items: AasxLanguageHelper.Languages.GetAllLanguages().ToArray()),
(o) =>
{
if (o is string os)
Expand Down
75 changes: 62 additions & 13 deletions src/AasxIntegrationBase/AasxLanguageHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ public bool IsAny()
return LangCode == "" && CountryCode == ""
|| LangCode == "All" && CountryCode == "All";
}

public static AasxLanguageTuple GetAny()
{
return new AasxLanguageTuple() { LangCode = "All", CountryCode = "All" };
}
}

/// <summary>
Expand All @@ -51,10 +56,13 @@ public bool IsAny()
/// </summary>
public class AasxLanguageTupleSet : MultiValueDictionary<string, AasxLanguageTuple>
{
public void Add(string lang, string country)
public void Add(string lang, string country, bool correctInput = true)
{
lang = lang?.ToLower().Trim();
country = country?.ToLower().Trim();
if (correctInput)
{
lang = lang?.ToLower().Trim();
country = country?.ToUpper().Trim();
}

if (lang != null && lang != ""
&& country != null && country != "")
Expand All @@ -63,13 +71,18 @@ public void Add(string lang, string country)
}
}

public AasxLanguageTupleSet()
{
Init();
}

/// <summary>
/// Rationale for default languages/ countries: member in IDTA or IEC TC65 WG24
/// </summary>
public void Init()
{
this.Clear();
Add("All", "All");
Add("All", "All", correctInput: false);
Add("en", "GB");
Add("en", "US");
Add("de", "DE");
Expand All @@ -91,43 +104,76 @@ public void Init()
public IEnumerable<AasxLanguageTuple> FindByLang(string lang)
{
lang = lang?.ToLower().Trim();
if (lang != null && lang != "" || this.ContainsKey(lang) == false)
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 != "")
country = country?.ToUpper().Trim();
if (country == null || country == "")
yield break;
foreach (var tp in this.Values)
if (country == tp.CountryCode)
if (country == tp.CountryCode.ToUpper())
yield return tp;
}

public IEnumerable<string> GetAllLanguages()
public IEnumerable<string> GetAllLanguages(bool nullForAny = false)
{
var temp = new List<string>();
foreach (var tp in this.Values)
if (!tp.IsAny())
yield return tp.LangCode;
temp.Add(tp.LangCode);
else
{
if (nullForAny)
temp.Add(null);
else
temp.Add(tp.LangCode);
}

if (temp.Count < 1)
yield break;

temp = temp.Distinct().ToList();
var first = temp[0];
temp.RemoveAt(0);
temp.Sort();
temp.Insert(0, first);

foreach (var t in temp)
yield return t;
}

public IEnumerable<string> GetAllCountries()
{
var temp = new List<string>();
foreach (var tp in this.Values)
if (!tp.IsAny())
yield return tp.CountryCode;
temp.Add(tp.CountryCode);

if (temp.Count < 1)
yield break;

temp = temp.Distinct().ToList();
var first = temp[0];
temp.RemoveAt(0);
temp.Sort();
temp.Insert(0, first);

foreach (var t in temp)
yield return t;
}

public void InitByCustomString(string input)
{
this.Clear();
Add("All", "All", correctInput: false);
var pairs = input.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
foreach (var pair in pairs)
{
var m = Regex.Match(pair, @"(\w+)\s*/\s*(\w+)");
var m = Regex.Match(pair, @"(\w+)\s*-\s*(\w+)");
if (m.Success)
Add(m.Groups[1].ToString(), m.Groups[2].ToString());
}
Expand All @@ -136,7 +182,9 @@ public void InitByCustomString(string input)

public static class AasxLanguageHelper
{
public static AasxLanguageTupleSet Languages = new AasxLanguageTupleSet();

#if __old
public enum LangEnum { Any = 0, EN, DE, ZH, JA, KO, FR, ES };

public static string[] LangEnumToISO639String = {
Expand Down Expand Up @@ -184,6 +232,7 @@ public static IEnumerable<string> GetLangCodes()
for (int i = 1; i < LangEnumToISO639String.Length; i++)
yield return LangEnumToISO639String[i];
}
#endif

public static string GetFirstLangCode(string codes)
{
Expand Down
6 changes: 5 additions & 1 deletion src/AasxPackageExplorer/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ private void Application_Startup(object sender, StartupEventArgs e)
Options.ReplaceCurr(InferOptions(exePath, e.Args));

// commit some options to other global locations
AdminShellUtil.DefaultLngIso639 = AasxLanguageHelper.GetFirstLangCode(Options.Curr.DefaultLang) ?? "en?";
AdminShellUtil.DefaultLngIso639 = AasxLanguageHelper.GetFirstLangCode(Options.Curr.DefaultLangs) ?? "en?";

// search for plugins?
if (Options.Curr.PluginDir != null)
Expand Down Expand Up @@ -173,6 +173,10 @@ private void Application_Startup(object sender, StartupEventArgs e)
}
}

// languages
if (Options.Curr.OfferedLangs?.HasContent() == true)
AasxIntegrationBase.AasxLanguageHelper.Languages.InitByCustomString(Options.Curr.OfferedLangs);

// preferences
Pref pref = Pref.Read();

Expand Down
2 changes: 1 addition & 1 deletion src/AasxPackageExplorer/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1243,7 +1243,7 @@ private async void Window_Loaded(object sender, RoutedEventArgs e)
;
}

AasxIntegrationBaseWpf.CountryFlagWpf.LoadImage();
// AasxIntegrationBaseWpf.CountryFlagWpf.LoadImage();
}

private void ToolFindReplace_ResultSelected(AasxSearchUtil.SearchResultItem resultItem)
Expand Down
6 changes: 5 additions & 1 deletion src/AasxPackageExplorer/options-debug.MIHO.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,11 @@
"TemplateIdSubmodelTemplate": "www.example.com/ids/smt/DDDD_DDDD_DDDD_DDDD",
"TemplateIdConceptDescription": "www.example.com/ids/cd/DDDD_DDDD_DDDD_DDDD",
"EclassDir": ".\\eclass\\",
"DefaultLang": "en,de",
"DefaultLangs": "en,de",
// the default codes, as also in the source code
// "OfferedLangs": "en-GB,en-US,de-DE,de-CH,de-AT,es-ES,fi-FI,fr-FR,it-IT,ja-JP,ko-KR,nl-NL,no-NO,pt-PT,sv-SE,zh-CN",
// see: https://stackoverflow.com/questions/3191664/list-of-all-locales-and-their-short-codes
"OfferedLangs": "af-ZA,am-ET,ar-AE,ar-BH,ar-DZ,ar-EG,ar-IQ,ar-JO,ar-KW,ar-LB,ar-LY,ar-MA,ar-OM,ar-QA,ar-SA,ar-SD,ar-SY,ar-TN,ar-YE,as-IN,az-az,ba-RU,be-BY,bg-BG,bn-BD,bn-IN,bo-CN,br-FR,ca-ES,co-FR,cs-CZ,cy-GB,da-DK,de-DE,de-AT,de-CH,de-LI,de-LU,dv-MV,el-CY,el-GR,en-GB,en-AU,en-BZ,en-CA,en-cb,en-IE,en-IN,en-JM,en-MT,en-MY,en-NZ,en-PH,en-SG,en-TT,en-US,en-ZA,en-ZW,es-ES,es-AR,es-BO,es-CL,es-CO,es-CR,es-DO,es-EC,es-GT,es-HN,es-MX,es-NI,es-PA,es-PE,es-PR,es-PY,es-SV,es-US,es-UY,es-VE,et-EE,eu-ES,fa-IR,fi-FI,fo-FO,fr-FR,fr-BE,fr-CA,fr-CH,fr-LU,fr-MC,fy-NL,ga-IE,gd-GB,gd-ie,gl-ES,gu-IN,he-IL,hi-IN,hr-BA,hr-HR,hu-HU,hy-AM,id-ID,ig-NG,ii-CN,in-ID,is-IS,it-CH,it-IT,iw-IL,ja-JP,ka-GE,kk-KZ,kl-GL,km-KH,kn-IN,ko-KR,ky-KG,lb-LU,lo-LA,lt-LT,lv-LV,mi-NZ,mk-MK,ml-IN,mn-MN,mr-IN,ms-BN,ms-MY,mt-MT,nb-NO,ne-NP,nl-BE,nl-NL,nn-NO,no-no,oc-FR,or-IN,pa-IN,pl-PL,ps-AF,pt-BR,pt-PT,rm-CH,ro-mo,ro-RO,ru-mo,ru-RU,rw-RW,sa-IN,se-FI,se-NO,se-SE,si-LK,sk-SK,sl-SI,sq-AL,sr-BA,sr-CS,sr-ME,sr-RS,sr-sp,sv-FI,sv-SE,sw-KE,ta-IN,te-IN,th-TH,tk-TM,tn-ZA,tr-TR,tt-RU,ug-CN,uk-UA,ur-PK,uz-uz,vi-VN,wo-SN,xh-ZA,yo-NG,zh-CN,zh-HK,zh-MO,zh-SG,zh-TW,zu-ZA",
"DefaultEmptyLangText": "", /* change for having a preset when creating new language lists */
"DefaultEmptyReferenceKey": "", /* change for having a preset when creating new References */
/* "LogoFile": "SpecPI40_t.png", */
Expand Down
4 changes: 1 addition & 3 deletions src/AasxPackageLogic/DispEditHelperBasics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,6 @@ public class DispEditHelperBasics : AnyUiSmallWidgetToolkit
// Members
//

private string[] defaultLanguages = new[] { "en", "de", "fr", "es", "it", "zh", "kr", "jp" };

public PackageCentral.PackageCentral packages = null;
public IPushApplicationEvent appEventsProvider = null;

Expand Down Expand Up @@ -939,7 +937,7 @@ public void AddKeyListLangStr<T>(
AnyUiContextCapability.Blazor, new AnyUiThickness(0, 4, 0, 4)),
text: "" + langStr[currentI].Language,
minWidth: 60,
items: defaultLanguages,
items: AasxLanguageHelper.Languages.GetAllLanguages(nullForAny: true).ToArray(),
isEditable: true),
verticalAlignment: AnyUiVerticalAlignment.Top,
verticalContentAlignment: AnyUiVerticalAlignment.Center
Expand Down
4 changes: 2 additions & 2 deletions src/AasxPackageLogic/DispEditHelperMiniModules.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1955,8 +1955,8 @@ public void DispSmeListAddNewHelper<T>(
mlp.Value = new List<ILangStringTextType>();
// add defaults?
if (Options.Curr.DefaultLang.HasContent())
foreach (var lng in Options.Curr.DefaultLang.Split(',',
if (Options.Curr.DefaultLangs.HasContent())
foreach (var lng in Options.Curr.DefaultLangs.Split(',',
StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries))
mlp.Value.Add(new LangStringTextType("" + lng, ""));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public static AnyUiDialogueDataModalPanel CreateModelDialogue(
helper.Set(
helper.AddSmallComboBoxTo(g, 0, 3,
text: innerDiaData.AttributeLang,
items: AasxLanguageHelper.GetLangCodes().ToArray(),
items: AasxLanguageHelper.Languages.GetAllLanguages().ToArray(),
isEditable: true),
minWidth: 200),
(s) => { innerDiaData.AttributeLang = s; });
Expand Down
14 changes: 10 additions & 4 deletions src/AasxPackageLogic/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -476,10 +476,16 @@ public class OptionsInformation
public bool AllowLocalFiles = true;

[OptionDescription(Description =
"Designates the default language code in ISO639-1. This will be used for new language strings " +
"of uncertain language",
Cmd = "-default-lang")]
public string DefaultLang = "en";
"Designates the default language codes in ISO639-1. This will be used for new language strings " +
"of uncertain information. Each language separated by comma. First language code is the default one.",
Cmd = "-default-langs")]
public string DefaultLangs = "en";

[OptionDescription(Description =
"List of language/ country codes offered by the UI. Comma-separated tags of two-digit ISO 639-2 and ISO 3166-1 codes, " +
"also known (but not equal) to RFC 5464 locales. ",
Cmd = "-offered-langs")]
public string OfferedLangs = "en";

[OptionDescription(Description =
"Designates the default value of the first text value of a newly created multi language list. " +
Expand Down
2 changes: 1 addition & 1 deletion src/AasxPluginContactInformation/ContactEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public static ListOfContactEntity ParseSubmodelForV10(
AdminShellPackageEnv thePackage,
Aas.Submodel subModel, AasxPluginContactInformation.ContactInformationOptions options,
string defaultLang,
int selectedDocClass, AasxLanguageHelper.LangEnum selectedLanguage)
int selectedDocClass, AasxLanguageTuple selectedLanguage)
{
// set a new list
var its = new ListOfContactEntity();
Expand Down
45 changes: 22 additions & 23 deletions src/AasxPluginContactInformation/ContactListAnyUiControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class ShelfAnyUiControl

protected AnyUiSmallWidgetToolkit _uitk = new AnyUiSmallWidgetToolkit();

protected AasxLanguageHelper.LangEnum _selectedLang = AasxLanguageHelper.LangEnum.Any;
protected AasxLanguageTuple _selectedLang = AasxLanguageTuple.GetAny();

protected string _selectedRole = null;

Expand Down Expand Up @@ -224,10 +224,7 @@ protected void RenderPanelOutside(
//

// countries
var allLangs = new List<object>();
foreach (var dc in (AasxLanguageHelper.LangEnum[])Enum.GetValues(
typeof(AasxLanguageHelper.LangEnum)))
allLangs.Add("Nation - " + AasxLanguageHelper.LangEnumToISO3166String[(int)dc]);
var countries = AasxLanguageHelper.Languages.GetAllCountries().ToList();

// controls
var controls = uitk.AddSmallWrapPanelTo(outer, 1, 0,
Expand All @@ -238,21 +235,23 @@ protected void RenderPanelOutside(
//

AnyUiComboBox cbCountries = null;
cbCountries = AnyUiUIElement.RegisterControl(controls.Add(new AnyUiComboBox()
{
Margin = new AnyUiThickness(6, 4, 4, 4),
MinWidth = 120,
Items = allLangs,
SelectedIndex = (int)_selectedLang
}), (o) =>
{
// ReSharper disable PossibleInvalidOperationException
if (cbCountries != null)
_selectedLang = (AasxLanguageHelper.LangEnum)cbCountries.SelectedIndex;
// ReSharper enable PossibleInvalidOperationException
PushUpdateEvent();
return new AnyUiLambdaActionNone();
});
cbCountries = AnyUiUIElement.RegisterControl(
controls.Add(new AnyUiComboBox()
{
Margin = new AnyUiThickness(6, 4, 4, 4),
MinWidth = 120,
Items = countries.Select((s) => "Nation - " + s).Cast<object>().ToList(),
SelectedIndex = _selectedLang == null ? 0 : (countries.IndexOf(_selectedLang.CountryCode))
}),
(o) => {
// ReSharper disable PossibleInvalidOperationException
if (cbCountries != null)
_selectedLang = AasxLanguageHelper.Languages.FindByCountry(
countries.ElementAt(cbCountries.SelectedIndex.Value)).FirstOrDefault();
// ReSharper enable PossibleInvalidOperationException
PushUpdateEvent();
return new AnyUiLambdaActionNone();
});

//
// Role
Expand Down Expand Up @@ -365,9 +364,9 @@ protected void RenderPanelOutside(
foreach (var ce in its)
{
// Country?
if (_selectedLang != AasxLanguageHelper.LangEnum.Any
&& !AasxLanguageHelper.LangEnumToISO3166String[(int)_selectedLang]
.Equals(ce.CountryCode, StringComparison.InvariantCultureIgnoreCase))
if (_selectedLang != null
&& (!_selectedLang.IsAny()
&& _selectedLang.CountryCode != ce.CountryCode?.ToUpper().Trim()))
continue;

// Role
Expand Down
Loading

0 comments on commit 8ba9420

Please sign in to comment.