// Copyright (C) 2016, The Duplicati Team // http://www.duplicati.com, info@duplicati.com // // This library is free software; you can redistribute it and/or modify // it under the terms of the GNU Lesser General Public License as // published by the Free Software Foundation; either version 2.1 of the // License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, but // WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA using System; using System.IO; using System.Linq; using System.Collections.Generic; using System.Globalization; using System.Reflection; using System.Text.RegularExpressions; namespace Duplicati.Library.Localization { /// /// Class for reading embedded PO files /// public class PoLocalizationService : ILocalizationService { /// /// The environment variable used to locate PO files /// public const string LOCALIZATIONDIR_ENVNAME = "LOCALIZATION_FOLDER"; /// /// A cached copy of all strings /// private Dictionary m_messages = new Dictionary(); /// /// Path to search for extra .po files in /// public static string[] SearchPaths = string.IsNullOrWhiteSpace(AppDomain.CurrentDomain.GetData(LOCALIZATIONDIR_ENVNAME) as string) ? new string[] { Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) } : (AppDomain.CurrentDomain.GetData(LOCALIZATIONDIR_ENVNAME) as string).Split(new char[] { Path.PathSeparator }, StringSplitOptions.RemoveEmptyEntries); /// /// Assembly to look for .po files in /// public static Assembly SearchAssembly = Assembly.GetExecutingAssembly(); /// /// Regular expression to match a PO line entry /// private static readonly Regex PO_MATCHER = new Regex(@"(?\w+(\[(?[0-9]+)\])?)\s+""(?(?:\\.|""""|[^""\\])*)"""); /// /// Regular expression to match a locale from a filename /// private static readonly Regex CI_MATCHER = new Regex(@"localization-(?" + LocalizationService.CI_MATCHER + @")\.po"); /// /// Initializes a new instance of the class. /// /// The culture to find. public PoLocalizationService(CultureInfo ci) { var filenames = new string[] { // Load the generic country version first string.Format("localization-{0}.po", ci.Name), // Then the specialized version with overrides string.Format("localization-{0}.po", ci.TwoLetterISOLanguageName) }; foreach(var fn in filenames) { // Use embedded version first if (SearchAssembly != null) { // Find the localization streams inside the assembly var names = from name in SearchAssembly.GetManifestResourceNames() let m = CI_MATCHER.Match(name) let c = m.Success && string.Equals(m.Value, fn, StringComparison.InvariantCultureIgnoreCase) ? LocalizationService.ParseCulture(m.Groups["culture"].Value) : null where c != null select name; foreach (var sn in names) using (var s = SearchAssembly.GetManifestResourceStream(sn)) if (s != null) using (var sr = new StreamReader(s, System.Text.Encoding.UTF8, true)) ParseStream(sr); } // Then override with external foreach(var sp in SearchPaths) if (!string.IsNullOrWhiteSpace(sp) && File.Exists(Path.Combine(sp, fn))) using (var fs = new StreamReader(Path.Combine(sp, fn), System.Text.Encoding.UTF8, true)) ParseStream(fs); } } /// /// Parses a PO file by reading each line /// /// The reader to extract data from. private void ParseStream(TextReader rd) { string ln; string msgid = null; while ((ln = rd.ReadLine()) != null) { var m = PO_MATCHER.Match(ln); if (m.Success) { var key = m.Groups["key"].Value; var value = m.Groups["value"].Value; if (!string.IsNullOrEmpty(value)) value = value.Replace("\"\"", "\"").Replace("\\\"", "\"").Replace("\\t", "\t").Replace("\\r", "\r").Replace("\\n", "\n"); if (string.Equals(key, "msgid", StringComparison.OrdinalIgnoreCase)) { msgid = value; } else if (string.Equals(key, "msgstr", StringComparison.OrdinalIgnoreCase)) { m_messages[msgid] = value; } } } } /// /// Performs the actual translation /// /// The message to translate. private string Transform(string msg) { string res; if (string.IsNullOrWhiteSpace(msg) || !m_messages.TryGetValue(msg, out res)) return msg; return res; } /// /// Localizes the string similar to how string.Format works /// /// The string to localize /// The localized string public string Localize(string message) { return string.Format(Transform(message)); } /// /// Localizes the string similar to how string.Format works /// /// The string to localize /// The first argument /// The localized string public string Localize(string message, object arg0) { return string.Format(Transform(message), arg0); } /// /// Localizes the string similar to how string.Format works /// /// The string to localize /// The first argument /// The second argument /// The localized string public string Localize(string message, object arg0, object arg1) { return string.Format(Transform(message), arg0, arg1); } /// /// Localizes the string similar to how string.Format works /// /// The string to localize /// The first argument /// The second argument /// The third argument /// The localized string public string Localize(string message, object arg0, object arg1, object arg2) { return string.Format(Transform(message), arg0, arg1, arg2); } /// /// Localizes the string similar to how string.Format works /// /// The string to localize /// The arguments public string Localize(string message, params object[] args) { return string.Format(Transform(message), args); } /// /// Gets all strings. /// public IDictionary AllStrings { get { return m_messages; } } private static string[] m_supportedcultures = null; /// /// Gets a list of all the supported cultures /// public static IEnumerable SupportedCultures { get { if (m_supportedcultures == null) { var lst = new string[0].AsEnumerable(); if (SearchAssembly != null) lst = lst.Union(SearchAssembly.GetManifestResourceNames()); foreach (var sp in SearchPaths) if (Directory.Exists(sp)) lst = lst.Union(Directory.GetFiles(sp, "localization-*.po")); var allcultures = from name in lst let m = CI_MATCHER.Match(name) let ci = m.Success ? LocalizationService.ParseCulture(m.Groups["culture"].Value) : null where ci != null select ci; m_supportedcultures = allcultures .Select(x => x.TwoLetterISOLanguageName).Distinct().OrderBy(x => x) .Union( allcultures.Select(x => x.Name).Distinct().OrderBy(x => x) ).ToArray(); } return m_supportedcultures; } } } }