public string GetLocalizedResource(string textKey, string defaultValue = null, params object[] parameters) { if (string.IsNullOrEmpty(textKey)) { throw new ArgumentNullException("textKey", "text key cannot be null"); } var langKey = WorkContext.CurrentCuture; var key = DictionaryHelper.BuildKey(langKey, textKey); var dictionary = WorkContext.LocalizedResourceDictionary; if (dictionary == null || !dictionary.Any(l => l.Key.Equals(key) && l.Language.Equals(langKey))) { return(GetDefaultValue(langKey, textKey, defaultValue, parameters)); } var localizeResource = dictionary.FirstOrDefault(d => d.Key.Equals(key)); if (localizeResource != null) { if (parameters != null && parameters.Any()) { return(string.Format(localizeResource.Value, parameters)); } return(localizeResource.Value); } return(string.Empty); }
/// <summary> /// Refresh localize resource dictionaries /// </summary> public void RefreshDictionary() { var data = GetAll().Select(l => new LocalizedResourceModel { TextKey = l.TextKey, DefaultValue = l.DefaultValue, LanguageId = l.LanguageId, TranslatedValue = l.TranslatedValue }).ToList(); WorkContext.LocalizedResourceDictionary = data.Select( l => new LocalizeDictionaryItem { Language = l.LanguageId, Key = DictionaryHelper.BuildKey(l.LanguageId, l.TextKey), Value = l.TranslatedValue }).ToList(); }