// 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; using NGettext; namespace Duplicati.Library.Localization { /// /// Class for reading embedded MO files /// public class MoLocalizationService : ILocalizationService { /// /// The catalog containing the translations /// private ICatalog catalog = new Catalog(); /// /// The environment variable used to locate MO files /// public const string LOCALIZATIONDIR_ENVNAME = "LOCALIZATION_FOLDER"; /// /// Path to search for extra .mo 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 .mo files in /// public static Assembly SearchAssembly = Assembly.GetExecutingAssembly(); /// /// Regular expression to match a locale from a filename /// private static readonly Regex CI_MATCHER = new Regex(@"localization-(?" + LocalizationService.CI_MATCHER + @")\.mo"); /// /// Initializes a new instance of the class. /// /// The culture to find. public MoLocalizationService(CultureInfo ci) { var filenames = new string[] { // Load the specialized version first string.Format("localization-{0}.mo", ci.Name), // Then try the generic language version string.Format("localization-{0}.mo", ci.TwoLetterISOLanguageName) }; foreach(var fn in filenames) { // search first in external files foreach (var sp in SearchPaths) { if (!string.IsNullOrWhiteSpace(sp) && File.Exists(Path.Combine(sp, fn))) { using (var moFileStream = File.OpenRead(Path.Combine(sp, fn))) catalog = new Catalog(moFileStream, ci); return; } } // search in embedded files 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) { catalog = new Catalog(s, ci); return; } } } } /// /// Localizes the string similar to how string.Format works /// /// The string to localize /// The localized string public string Localize(string message) { return catalog.GetString(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 catalog.GetString(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 catalog.GetString(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 catalog.GetString(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 catalog.GetString(message, args); } private static IEnumerable m_supportedcultures = null; /// /// Gets a list of all the supported cultures /// private static IEnumerable SupportedCultureInfos { 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-*.mo")); 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.Concat(new[] { new CultureInfo("en") }) .Distinct(); } return m_supportedcultures; } } /// /// Gets a list of all the supported cultures /// public static IEnumerable SupportedCultures { get { return SupportedCultureInfos .Select(x => x.Name).Distinct() .OrderBy(x => x) .ToList(); } } /// /// Returns true if the culture has localization support /// public static Boolean isCultureSupported(CultureInfo culture) { foreach (var supportedCulture in SupportedCultureInfos) { if (supportedCulture.TwoLetterISOLanguageName == culture.TwoLetterISOLanguageName) return true; } return false; } } }