From 46e6e1e93039629a31fec48cbc861b65debb3202 Mon Sep 17 00:00:00 2001 From: Suha Can Date: Wed, 2 Jun 2010 16:24:05 -0700 Subject: [PATCH] - Resource manager processes po files, scope fallback. --HG-- branch : dev --- .../Services/DefaultResourceManager.cs | 83 ++++++++++++++++--- 1 file changed, 72 insertions(+), 11 deletions(-) diff --git a/src/Orchard/Localization/Services/DefaultResourceManager.cs b/src/Orchard/Localization/Services/DefaultResourceManager.cs index 938780ea1..313de4212 100644 --- a/src/Orchard/Localization/Services/DefaultResourceManager.cs +++ b/src/Orchard/Localization/Services/DefaultResourceManager.cs @@ -1,4 +1,6 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; +using System.IO; using Orchard.FileSystems.WebSite; namespace Orchard.Localization.Services { @@ -19,11 +21,21 @@ namespace Orchard.Localization.Services { LoadCultures(); } - if (cultureName.Equals("en-US")) { - return text; + foreach (var culture in _cultures) { + if (String.Equals(cultureName, culture.CultureName, StringComparison.OrdinalIgnoreCase)) { + string scopedKey = scope + "|" + text; + string genericKey = "|" + text; + if (culture.Translations.ContainsKey(scopedKey)) { + return culture.Translations[scopedKey]; + } + if (culture.Translations.ContainsKey(genericKey)) { + return culture.Translations[genericKey]; + } + return text; + } } - return string.Empty; + return text; } private void LoadCultures() { @@ -38,16 +50,65 @@ namespace Orchard.Localization.Services { private IDictionary LoadTranslationsForCulture(string culture) { string path = string.Format(CoreLocalizationFilePathFormat, culture); string text = _webSiteFolder.ReadFile(path); - return ParseLocalizationStream(text); + if (text != null) { + return ParseLocalizationStream(text); + } + return new Dictionary(); } private static IDictionary ParseLocalizationStream(string text) { - return new Dictionary(); + Dictionary translations = new Dictionary(); + StringReader reader = new StringReader(text); + string poLine, id, scope; + id = scope = String.Empty; + while ((poLine = reader.ReadLine()) != null) { + if (poLine.StartsWith("#:")) { + scope = ParseScope(poLine); + continue; + } + + if (poLine.StartsWith("msgid")) { + id = ParseId(poLine); + continue; + } + + if (poLine.StartsWith("msgstr")) { + string translation = ParseTranslation(poLine); + if (!String.IsNullOrEmpty(id)) { + if (!String.IsNullOrEmpty(scope)) { + string scopedKey = scope + "|" + id; + if (!translations.ContainsKey(scopedKey)) { + translations.Add(scopedKey, translation); + } + } + string genericKey = "|" + id; + if (!translations.ContainsKey(genericKey)) { + translations.Add(genericKey, translation); + } + } + id = scope = String.Empty; + } + + } + + return translations; + } + + private static string ParseTranslation(string poLine) { + return poLine.Substring(6).Trim().Trim('"'); + } + + private static string ParseId(string poLine) { + return poLine.Substring(5).Trim().Trim('"'); + } + + private static string ParseScope(string poLine) { + return poLine.Substring(2).Trim().Trim('"'); + } + + class CultureDictionary { + public string CultureName { get; set; } + public IDictionary Translations { get; set; } } } - - class CultureDictionary { - public string CultureName { get; set; } - public IDictionary Translations { get; set; } - } }