protected virtual string DebugText(string ns, string key, LanguageInfo language, ParameterSet dict) { var text = GetTextEntry(ns, key, language, true); bool fallback = text != null && text.Text.Language != language.Key; var sb = new StringBuilder(); sb.Append("Namespace: ").Append(ns).Append("\n") .Append("Key: ").Append(key).Append("\n") .Append("Language: ").Append(language.Key).Append("\n") .Append("Pattern: "); if (text != null) { sb.Append("" + text.Text.Pattern).Append("\n") .Append("Pattern Dialect: ").Append(text.Text.PatternDialect).Append("\n"); if (fallback) { sb.Append("Fallback to: ").Append(text.Text.Language).Append("\n"); } } else { sb.Append("(undefined for language)\n"); } sb.Append("Values = {\n"); bool first = true; foreach (var dictKey in dict.Keys) { if (first) { first = false; } else { sb.Append(", \n"); } sb.Append(" ").Append(dictKey).Append(": ").Append(dict[dictKey]); } sb.Append("\n}\n"); return(sb.ToString()); }
public LocalizedTextCacheEntry GetTextEntry(string ns, string key, LanguageInfo language, bool considerLanguageFallbacks, bool considerNamespaceFallbacks = true) { EnsureLoaded(); var entry = GetTextEntryInternal(ns, key, language, considerLanguageFallbacks); if (entry == null && FallbackNamespaces != null && considerNamespaceFallbacks) { foreach (var fallbackNs in FallbackNamespaces.Reverse()) { if ((entry = GetTextEntryInternal(fallbackNs, key, language, considerLanguageFallbacks)) != null) { break; } } } return(entry); }
/// <summary> /// Gets the text with the specified namespace and key for the specified language. /// If namespace is null the assembly asm is passed to the implementation of GetCurrentNamespace /// </summary> /// <param name="ns">If null the current namespace</param> /// <param name="key">The key of the text to get</param> /// <param name="language">If null the current language</param> /// <param name="callingAssembly">The calling assembly. If helper methods invoke this method they should pass Assembly.GetCallingAssembly() to ensure that namespaces are handled correctly</param> /// <param name="debug">Show debug output instead of evaluating the text's pattern</param> /// <param name="returnNullOnMissing">if set to <c>true</c> null is returned on missing texts. This has no effect if no key is specified as the string is the default text</param> /// <param name="encode">if set to <c>true</c> the text is encoded using the current text managers encoder.</param> /// <param name="fallback">A fallback value if no localized value can be found.</param> /// <returns>The translated string</returns> public string Get(string key, object values = null, LanguageInfo language = null, string ns = null, Assembly callingAssembly = null, bool?debug = null, bool returnNullOnMissing = false, bool encode = true, string fallback = null) { EnsureLoaded(); //Sad: Assembly.GetCallingAssembly isn't reliable. MVC views, compiled Linq expressions etc. return rubbish bool debugMode = debug ?? IsInDebugMode(); ns = ns ?? GetCurrentNamespace(TrackCallingAssembly ? callingAssembly : null); ns = ns ?? (DefaultNamespace ?? ""); language = language ?? GetCurrentLanguage(); var dict = ObjectHelper.ParamsToParameterSet(values, addWithIndex: true); if (debugMode) { return(DebugText(ns, key, language, dict)); } var entry = GetTextEntry(ns, key, language, true); if (entry != null) { return(entry.Evaluator.Evaluate(new EvaluationContext { Parameters = dict, Language = language, TimeZoneInfo = GetCurrentTimeZoneInfo(), Namespace = ns, StringEncoder = encode && StringEncoder != null && entry.PatternDialect.Encode ? StringEncoder : ((x) => x) //Use identity transform if no transformer is specified })); } if (!returnNullOnMissing && MissingTextHandler != null) { return(MissingTextHandler(ns, key, language, fallback)); } return(null); }
/// <summary> /// Localizes the string. /// If a key is specified the string the method is called on is considered the default value. Otherwise; the key /// </summary> /// <param name="s">The string with the default value.</param> /// <param name="typeRef">An object reference to a class in the namespace to get texts from.</param> /// <param name="parameters">The parameters to the text.</param> /// <param name="key">The key for text. If this is specified the string the method is called on is considered the default value. Otherwise; the key</param> /// <param name="language">The language. Default is current language</param> /// <param name="ns">The namespace. Specify to override the namespace from the type</param> /// <param name="debug">Show debug output as configured in the current text manager.</param> /// <param name="returnNullOnMissing">if set to <c>true</c> null is returned on missing texts. This has no effect if no key is specified as the string is the default text</param> /// <param name="encode">if set to <c>true</c> the text is encoded using the current text managers encoder.</param> /// <param name="fallback">A fallback value if no localized value can be found.</param> /// <returns> /// A localized text using the current text manager /// </returns> public static string Localize(this string s, object typeRef = null, object parameters = null, string key = null, LanguageInfo language = null, string ns = null, bool?debug = null, bool returnNullOnMissing = false, bool encode = true, string fallback = null) { Assembly asm = null; if (typeRef != null) { if (typeRef is string) { throw new LocalizedArgumentException("StringHelpers.TypeRefIsString"); } asm = typeRef.GetType().Assembly; } else { asm = Assembly.Load("Rebel.Cms.Web"); //TODO: Need to remove hard coded reference to web project } return(Localize(s, asm, parameters, key, language, ns, debug, returnNullOnMissing, encode, fallback)); }
public override bool PrepareTextSources(string ns = null, string key = null, LanguageInfo language = null) { if (ns != null && NamespaceTextResolver != null) { lock (this) { if (probedNamespaces.Contains(ns)) { return(false); } var asm = TypeFinder.GetFilteredLocalAssemblies(exclusionFilter: KnownAssemblyExclusionFilter).FirstOrDefault(x => { try { //This will fail for certain assemblies in medium trust (e.g. mscorlib) return(GetNamespace(x) == ns); } catch { return(false); } }); if (asm != null) { return(PrepareTextSources(asm)); } else { probedNamespaces.Add(ns); } } } return(false); }
/// <summary> /// Implementing classes may override this method to add text sources on demand. /// </summary> /// <param name="ns">The ns.</param> /// <returns>true if the namespace was loaded</returns> public virtual bool PrepareTextSources(string ns = null, string key = null, LanguageInfo language = null) { return(false); }
public static string Get(string key, object parameters = null, LanguageInfo language = null, string ns = null, bool?debug = null, bool returnNullOnMissing = false, bool encode = true) { return(LocalizationConfig.TextManager.Get <TNamespaceRef>(key, parameters, language, ns, debug, returnNullOnMissing, encode)); }
public void FromModel_LanguageInfo_ToRdbms() { // Arrange var model = new LanguageInfo() { Key = "en-gb", Name = "myname" }; // Act var rdbms = _rdbmsTypeMapper.Map<Locale>(model); // Assert Assert_CompareLocale(rdbms, model); }
private static void Assert_CompareLocale(Locale rdbms, LanguageInfo model) { Assert.That(model.Alias, Is.EqualTo(rdbms.Alias)); Assert.That(model.Key, Is.EqualTo(rdbms.LanguageIso)); Assert.That((string)model.Name, Is.EqualTo(rdbms.Name)); Assert.That(model.Culture, Is.EqualTo(LanguageInfo.GetCultureFromKey("en-gb"))); }
public override bool PrepareTextSources(string ns = null, string key = null, LanguageInfo language = null) { if (ns!= null && NamespaceTextResolver != null) { lock (this) { if (probedNamespaces.Contains(ns)) { return false; } var asm = TypeFinder.GetFilteredLocalAssemblies(exclusionFilter:KnownAssemblyExclusionFilter).FirstOrDefault(x => { try { //This will fail for certain assemblies in medium trust (e.g. mscorlib) return GetNamespace(x) == ns; } catch { return false; } }); if (asm != null) { return PrepareTextSources(asm); } else { probedNamespaces.Add(ns); } } } return false; }
protected override string DebugText(string ns, string key, LanguageInfo language, ParameterSet dict) { if (DebugFormatter != null) { var text = GetTextEntry(ns, key, language, true); return DebugFormatter(ns, key, language, text, dict); } return base.DebugText(ns, key, language, dict); }