diff --git a/src/i18n.Domain/Concrete/POTranslationRepository.cs b/src/i18n.Domain/Concrete/POTranslationRepository.cs index 609cf0a..67e3c90 100644 --- a/src/i18n.Domain/Concrete/POTranslationRepository.cs +++ b/src/i18n.Domain/Concrete/POTranslationRepository.cs @@ -142,7 +142,7 @@ namespace i18n.Domain.Concrete /// The translation you wish to save. Must have Language shortag filled out. public void SaveTranslation(Translation translation) { - var templateFilePath = GetAbsoluteLocaleDir() + "/messages.pot"; + var templateFilePath = GetAbsoluteLocaleDir() + "/" + _settings.LocaleFilename + ".pot"; var POTDate = DateTime.Now; if (File.Exists(templateFilePath)) { POTDate = File.GetLastWriteTime(templateFilePath); } @@ -258,7 +258,7 @@ namespace i18n.Domain.Concrete /// A list of template items to save. The list should be all template items for the entire project. public void SaveTemplate(IDictionary items) { - string filePath = GetAbsoluteLocaleDir() + "/messages.pot"; + string filePath = GetAbsoluteLocaleDir() + "/" + _settings.LocaleFilename + ".pot"; string backupPath = filePath + ".backup"; if (File.Exists(filePath)) //we backup one version. more advanced backup solutions could be added here. @@ -350,9 +350,11 @@ namespace i18n.Domain.Concrete return _settings.LocaleDirectory; } - private string GetPathForLanguage(string langtag) + private string GetPathForLanguage(string langtag, string filename = null) { - return Path.Combine(GetAbsoluteLocaleDir(), langtag, "messages.po"); + if (!filename.IsSet()) + filename = _settings.LocaleFilename; + return Path.Combine(GetAbsoluteLocaleDir(), langtag, filename + ".po"); } /// @@ -370,83 +372,97 @@ namespace i18n.Domain.Concrete translation.LanguageInformation = language; var items = new ConcurrentDictionary(); - string path = GetPathForLanguage(langtag); - - if (File.Exists(path)) { - DebugHelpers.WriteLine("Reading file: {0}", path); - - using (var fs = File.OpenText(path)) - { - // http://www.gnu.org/s/hello/manual/gettext/PO-Files.html - - string line; - bool itemStarted = false; - while ((line = fs.ReadLine()) != null) - { - var extractedComments = new List(); - var translatorComments = new List(); - var flags = new List(); - var references = new List(); - - //read all comments, flags and other descriptive items for this string - //if we have #~ its a historical/log entry but it is the messageID/message so we skip this do/while - if (line.StartsWith("#") && !line.StartsWith("#~")) - { - do - { - itemStarted = true; - switch (line[1]) - { - case '.': //Extracted comments - extractedComments.Add(line.Substring(2).Trim()); - break; - case ':': //references - references.Add(ReferenceContext.Parse(line.Substring(2).Trim())); - break; - case ',': //flags - flags.Add(line.Substring(2).Trim()); - break; - case '|': //msgid previous-untranslated-string - NOT used by us - break; - default: //translator comments - translatorComments.Add(line.Substring(1).Trim()); - break; - } - - } while ((line = fs.ReadLine()) != null && line.StartsWith("#")); - } - - if (line != null && (itemStarted || line.StartsWith("#~"))) - { - TranslationItem item = ParseBody(fs, line, extractedComments); - if (item != null) { - // - item.TranslatorComments = translatorComments; - item.ExtractedComments = extractedComments; - item.Flags = flags; - item.References = references; - // - items.AddOrUpdate( - item.MsgKey, - // Add routine. - k => { - return item; - }, - // Update routine. - (k, v) => { - v.References = v.References.Append(item.References); - var referencesAsComments = item.References.Select(r => r.ToComment()).ToList(); - v.ExtractedComments = v.ExtractedComments.Append(referencesAsComments); - v.TranslatorComments = v.TranslatorComments.Append(referencesAsComments); - v.Flags = v.Flags.Append(referencesAsComments); - return v; - }); + List paths = new List(); + + paths.Add(GetPathForLanguage(langtag)); + + foreach (var file in _settings.LocaleOtherFiles) + { + paths.Add(GetPathForLanguage(langtag, file)); + } + + foreach (var path in paths) + { + if (File.Exists(path)) + { + DebugHelpers.WriteLine("Reading file: {0}", path); + + using (var fs = File.OpenText(path)) + { + // http://www.gnu.org/s/hello/manual/gettext/PO-Files.html + + string line; + bool itemStarted = false; + while ((line = fs.ReadLine()) != null) + { + var extractedComments = new List(); + var translatorComments = new List(); + var flags = new List(); + var references = new List(); + + //read all comments, flags and other descriptive items for this string + //if we have #~ its a historical/log entry but it is the messageID/message so we skip this do/while + if (line.StartsWith("#") && !line.StartsWith("#~")) + { + do + { + itemStarted = true; + switch (line[1]) + { + case '.': //Extracted comments + extractedComments.Add(line.Substring(2).Trim()); + break; + case ':': //references + references.Add(ReferenceContext.Parse(line.Substring(2).Trim())); + break; + case ',': //flags + flags.Add(line.Substring(2).Trim()); + break; + case '|': //msgid previous-untranslated-string - NOT used by us + break; + default: //translator comments + translatorComments.Add(line.Substring(1).Trim()); + break; + } + + } while ((line = fs.ReadLine()) != null && line.StartsWith("#")); + } + + if (line != null && (itemStarted || line.StartsWith("#~"))) + { + TranslationItem item = ParseBody(fs, line, extractedComments); + if (item != null) + { + // + item.TranslatorComments = translatorComments; + item.ExtractedComments = extractedComments; + item.Flags = flags; + item.References = references; + // + items.AddOrUpdate( + item.MsgKey, + // Add routine. + k => { + return item; + }, + // Update routine. + (k, v) => + { + v.References = v.References.Append(item.References); + var referencesAsComments = + item.References.Select(r => r.ToComment()).ToList(); + v.ExtractedComments = v.ExtractedComments.Append(referencesAsComments); + v.TranslatorComments = v.TranslatorComments.Append(referencesAsComments); + v.Flags = v.Flags.Append(referencesAsComments); + return v; + }); + } } - } - itemStarted = false; - } - } + itemStarted = false; + } + } + } } translation.Items = items; return translation; diff --git a/src/i18n.Domain/Concrete/i18nSettings.cs b/src/i18n.Domain/Concrete/i18nSettings.cs index 249b769..4c5481f 100644 --- a/src/i18n.Domain/Concrete/i18nSettings.cs +++ b/src/i18n.Domain/Concrete/i18nSettings.cs @@ -88,35 +88,81 @@ namespace i18n.Domain.Concrete return paths; } - #region Locale directory + #region Locale directory - private const string _localeDirectoryDefault = "locale"; - public virtual string LocaleDirectory - { - get - { - string prefixedString = GetPrefixedString("LocaleDirectory"); - string setting = _settingService.GetSetting(prefixedString); - string path; - if (setting != null) - { - path = setting; - } - else - { - path = _localeDirectoryDefault; - } + private const string _localeDirectoryDefault = "locale"; + public virtual string LocaleDirectory + { + get + { + string prefixedString = GetPrefixedString("LocaleDirectory"); + string setting = _settingService.GetSetting(prefixedString); + string path; + if (setting != null) + { + path = setting; + } + else + { + path = _localeDirectoryDefault; + } - return MakePathAbsoluteAndFromConfigFile(path); - } - set - { - string prefixedString = GetPrefixedString("LocaleDirectory"); - _settingService.SetSetting(prefixedString, value); - } - } + return MakePathAbsoluteAndFromConfigFile(path); + } + set + { + string prefixedString = GetPrefixedString("LocaleDirectory"); + _settingService.SetSetting(prefixedString, value); + } + } - #endregion + #endregion + + #region Locale filename + + private const string _localeFilenameDefault = "messages"; + public virtual string LocaleFilename + { + get + { + string prefixedString = GetPrefixedString("LocaleFilename"); + string setting = _settingService.GetSetting(prefixedString); + if (setting.IsSet()) + { + return setting; + } + + return _localeFilenameDefault; + } + set + { + string prefixedString = GetPrefixedString("LocaleFilename"); + _settingService.SetSetting(prefixedString, value); + } + } + + private const string _localeOtherFilesDefault = ""; + public virtual IEnumerable LocaleOtherFiles + { + get + { + string prefixedString = GetPrefixedString("LocaleOtherFiles"); + string setting = _settingService.GetSetting(prefixedString); + if (!setting.IsSet()) + { + setting = _localeOtherFilesDefault; + } + + return setting.Split(';'); + } + set + { + string prefixedString = GetPrefixedString("LocaleOtherFiles"); + _settingService.SetSetting(prefixedString, string.Join(";", value)); + } + } + + #endregion #region White list