// Copyright (C) 2015, 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.Linq; using System.Globalization; using System.Collections.Generic; using System.Text.RegularExpressions; using Duplicati.Library.Localization.Short; namespace Duplicati.Library.Localization { /// /// Provides an entry point for localization, regardless of the underlying translation engine /// public static class LocalizationService { /// /// The cache of services /// private static Dictionary Services = new Dictionary(); /// /// The key for accessing logical context /// internal const string LOGICAL_CONTEXT_KEY = "DUPLICATI_LOCALIZATION_CULTURE_CONTEXT"; /// /// Regular expression to match a locale /// public static readonly Regex CI_MATCHER = new Regex(@"[A-z]{2}(-[A-z]{4})?(-[A-z]{2})?"); /// /// Returns a temporary disposable localization context /// /// The context that must be disposed. /// The locale to use. public static IDisposable TemporaryContext(CultureInfo ci) { if (ci == null) return null; LC.setCulture(ci); return new LocalizationContext(ci); } /// /// A non-translating service /// private static ILocalizationService InvariantService = new MockLocalizationService(); /// /// Gets a localization provider with the default language /// public static ILocalizationService Invariant { get { return Get(CultureInfo.InvariantCulture); } } /// /// Parses the culture string into a cultureinfo instance. /// /// The parsed culture. /// The culture string. /// Set to true to return the invariant culture if the string is not valid, otherwise null is returned. public static CultureInfo ParseCulture(string culture, bool returninvariant = false) { var ci = returninvariant ? CultureInfo.InvariantCulture : null; if (CI_MATCHER.Match(culture).Success) try { ci = new CultureInfo(culture); } catch { } return ci; } /// /// Gets a localization provider with the current language /// public static ILocalizationService Current { get { var lc = System.Runtime.Remoting.Messaging.CallContext.LogicalGetData(LOGICAL_CONTEXT_KEY) as string; if (!string.IsNullOrWhiteSpace(lc)) return Get(new CultureInfo(lc)); return Get(CultureInfo.CurrentCulture); } } /// /// Gets a localization provider with the OS install language /// public static ILocalizationService Installed { get { return Get(CultureInfo.InstalledUICulture); } } /// /// Gets a localization provider with the specified language /// public static ILocalizationService Get(CultureInfo ci) { if (ci == CultureInfo.InvariantCulture) return InvariantService; ILocalizationService service; if (!Services.TryGetValue(ci, out service)) service = Services[ci] = new PoLocalizationService(ci); return service; } /// /// Gets a list of all locales known by the CLR /// /// All locales. public static IEnumerable AllLocales { get { return CultureInfo.GetCultures(CultureTypes.AllCultures).Select(x => x.Name).Where(x => !string.IsNullOrWhiteSpace(x)).Distinct().OrderBy(x => x); } } /// /// Gets all cultures with localization support /// public static IEnumerable SupportedCultures { get { return PoLocalizationService.SupportedCultures; } } } }