diff --git a/Claunia.Localization.Core/Localization.cs b/Claunia.Localization.Core/Localization.cs index 3b49dc5..0bd3b79 100644 --- a/Claunia.Localization.Core/Localization.cs +++ b/Claunia.Localization.Core/Localization.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; @@ -9,8 +10,9 @@ namespace Claunia.Localization.Core /// public class Localization { - readonly ObservableCollection messages; - readonly ObservableCollection translators; + readonly Dictionary> indexes; + readonly ObservableCollection messages; + readonly ObservableCollection translators; public Localization() { @@ -24,6 +26,7 @@ namespace Claunia.Localization.Core messages = new ObservableCollection(); Messages = new ReadOnlyObservableCollection(messages); messages.CollectionChanged += OnModified; + indexes = new Dictionary>(); } /// Date this localization has been created @@ -97,5 +100,31 @@ namespace Claunia.Localization.Core Message message = messages.FirstOrDefault(t => t.Id == id); return !(message is null) && messages.Remove(message); } + + public Dictionary GetIndex(string locale) + { + // TODO: Plurals + if(indexes.TryGetValue(locale, out Dictionary existingIndex)) return existingIndex; + + Dictionary index = new Dictionary(); + foreach(Message message in messages) + { + LocalizedString translated = message.Translations.FirstOrDefault(l => l.Locale == locale); + + if(translated is null) + { + // TODO: Requested "es" but only "es_ES" exists + int underscore = locale.IndexOf('_'); + if(underscore > 0) + translated = message.Translations.FirstOrDefault(l => l.Locale == locale.Substring(underscore)); + } + + index.Add(message.Id, translated is null ? message.Source.Singular : translated.Singular); + } + + indexes.Add(locale, index); + + return index; + } } } \ No newline at end of file diff --git a/Claunia.Localization.Test/gettext/Test.cs b/Claunia.Localization.Test/gettext/Test.cs index 0fe304e..7a59717 100644 --- a/Claunia.Localization.Test/gettext/Test.cs +++ b/Claunia.Localization.Test/gettext/Test.cs @@ -1,5 +1,8 @@ +using System; +using System.Collections.Generic; using System.IO; using System.Text; +using Claunia.Localization.Parsers; using NUnit.Framework; namespace Claunia.Localization.Test.gettext @@ -10,9 +13,14 @@ namespace Claunia.Localization.Test.gettext [Test] public void Parse() { - var testPath = Path.Combine(".", "gettext","es_ES.po"); + string testPath = Path.Combine(".", "gettext", "es_ES.po"); - var localization = Claunia.Localization.Parsers.GetText.Parse(testPath, Encoding.GetEncoding("iso8859-1")); + Core.Localization localization = GetText.Parse(testPath, Encoding.GetEncoding("iso8859-1")); + + DateTime start = DateTime.UtcNow; + Dictionary indexed = localization.GetIndex("es_ES"); + DateTime end = DateTime.UtcNow; + TimeSpan elapsed = end - start; } } } \ No newline at end of file