Skip to content

Commit

Permalink
Made *.xml reading work
Browse files Browse the repository at this point in the history
  • Loading branch information
Lakritzator committed Aug 4, 2015
1 parent 0300f80 commit 5eef2e9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
3 changes: 3 additions & 0 deletions Dapplo.Config.Test/Languages/language_de-DE.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@
<resource name="TestValue">Abbrechen</resource>
<resource name="OnlydeDE">BlubDE</resource>
</resources>
<resources prefix="Core">
<resource name="TestValue2">Another value</resource>
</resources>
</language>
7 changes: 5 additions & 2 deletions Dapplo.Config/Language/ILanguage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@ namespace Dapplo.Config.Language
{
/// <summary>
/// The base interface for all language objects.
/// It is important to have INotifyPropertyChanged, so language changes are directly shown in the UI.
/// My advice is that you extend your inteface with this, and the INotifyPropertyChanged,
/// so language changes are directly reflected in the UI.
///
/// This extends IDefaultValue, as this it is very common to start with english hard-coded
/// </summary>
public interface ILanguage : IDefaultValue, INotifyPropertyChanged
public interface ILanguage : IDefaultValue
{
}
}
20 changes: 13 additions & 7 deletions Dapplo.Config/Language/LanguageLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;

namespace Dapplo.Config.Language
Expand Down Expand Up @@ -62,7 +61,14 @@ public static LanguageLoader Get(string applicationName) {
return LoaderStore[applicationName];
}

public LanguageLoader(string applicationName, string defaultLanguage = "en-US", string filePatern = @"language_([a-zA-Z]+-[a-zA-Z]+)\.(ini|xml)")
/// <summary>
/// Create a LanguageLoader, this is your container for all the ILanguage implementing interfaces.
/// You can supply a default language right away.
/// </summary>
/// <param name="applicationName"></param>
/// <param name="defaultLanguage"></param>
/// <param name="filePatern">Pattern for the filename, the ietf group needs to be in there!</param>
public LanguageLoader(string applicationName, string defaultLanguage = "en-US", string filePatern = @"language_(?<IETF>[a-zA-Z]+-[a-zA-Z]+)\.(ini|xml)")
{
CurrentLanguage = defaultLanguage;
_filePattern = filePatern;
Expand All @@ -71,7 +77,7 @@ public LanguageLoader(string applicationName, string defaultLanguage = "en-US",
_availableLanguages = (
from filename
in _files
select Regex.Replace(Path.GetFileName(filename), _filePattern, "$1")).Distinct().ToDictionary(x => x, x => CultureInfo.GetCultureInfo(x).NativeName
select Regex.Match(Path.GetFileName(filename), _filePattern).Groups["IETF"].Value).Distinct().ToDictionary(x => x, x => CultureInfo.GetCultureInfo(x).NativeName
);
}

Expand Down Expand Up @@ -253,10 +259,10 @@ where file.Contains(CurrentLanguage)
newIni = await IniFile.ReadAsync(languageFile, Encoding.UTF8, token).ConfigureAwait(false);
} else if (languageFile.EndsWith(".xml")) {
newIni =
(from resourcesItem in XDocument.Load(languageFile).Descendants("resources")
group resourcesItem by resourcesItem.Attribute("prefix").Value into resourceItem
from resource in resourceItem.Descendants("resource")
select resourceItem).ToDictionary(group => group.Key, group => (IDictionary<string,string>)group.ToDictionary(x => x.Attribute("name").Value, x => x.Value));
(from resourcesElement in XDocument.Load(languageFile).Root.Elements("resources")
from resourceElement in resourcesElement.Elements("resource")
group resourceElement by resourcesElement.Attribute("prefix").Value into resourceElementGroup
select resourceElementGroup).ToDictionary(group => group.Key, group => (IDictionary<string,string>)group.ToDictionary(x => x.Attribute("name").Value, x => x.Value));
} else {
throw new NotSupportedException(string.Format("Can't read the file format for {0}", languageFile));
}
Expand Down

0 comments on commit 5eef2e9

Please sign in to comment.