Skip to content

Instantly share code, notes, and snippets.

@bariloce
Last active August 29, 2015 14:04
Show Gist options
  • Save bariloce/2a22604d7755681fc928 to your computer and use it in GitHub Desktop.
Save bariloce/2a22604d7755681fc928 to your computer and use it in GitHub Desktop.
Localization extension on Views - not buildable, just to see example of implementation
public class LocalizationService : ILocalizationService
{
//some injections
//...
//ctor
//...
public virtual string GetResource(string resourceKey)
{
if (_workContext.WorkingLanguage != null)
return GetResource(resourceKey, _workContext.WorkingLanguage.Id);
return "";
}
public virtual string GetResource(string resourceKey, int languageId,
bool logIfNotFound = true, string defaultValue = "", bool returnEmptyIfNotFound = false)
{
string result = string.Empty;
if (resourceKey == null)
resourceKey = string.Empty;
resourceKey = resourceKey.Trim().ToLowerInvariant();
if (_localizationSettings.LoadAllLocaleRecordsOnStartup)
{
//load all records (we know they are cached)
var resources = GetAllResourceValues(languageId);
if (resources.ContainsKey(resourceKey))
{
result = resources[resourceKey].Value;
}
}
else
{
//gradual loading
string key = string.Format(LOCALSTRINGRESOURCES_BY_RESOURCENAME_KEY, languageId, resourceKey);
string lsr = _cacheManager.Get(key, () =>
{
var query = from l in _lsrRepository.Table
where l.ResourceName == resourceKey
&& l.LanguageId == languageId
select l.ResourceValue;
return query.FirstOrDefault();
});
if (lsr != null)
result = lsr;
}
if (String.IsNullOrEmpty(result))
{
if (logIfNotFound)
_logger.Warning(string.Format("Resource string ({0}) is not found. Language ID = {1}", resourceKey, languageId));
if (!String.IsNullOrEmpty(defaultValue))
{
result = defaultValue;
}
else
{
if (!returnEmptyIfNotFound)
result = resourceKey;
}
}
return result;
}
}
using System;
using System.Web;
public class LocalizedString : MarshalByRefObject, IHtmlString
{
private readonly string _localized;
private readonly string _scope;
private readonly string _textHint;
private readonly object[] _args;
public LocalizedString(string localized)
{
_localized = localized;
}
public LocalizedString(string localized, string scope, string textHint, object[] args)
{
_localized = localized;
_scope = scope;
_textHint = textHint;
_args = args;
}
public static LocalizedString TextOrDefault(string text, LocalizedString defaultValue)
{
if (string.IsNullOrEmpty(text))
return defaultValue;
return new LocalizedString(text);
}
public string Scope
{
get { return _scope; }
}
public string TextHint
{
get { return _textHint; }
}
public object[] Args
{
get { return _args; }
}
public string Text
{
get { return _localized; }
}
public override string ToString()
{
return _localized;
}
public string ToHtmlString()
{
return _localized;
}
public override int GetHashCode()
{
var hashCode = 0;
if (_localized != null)
hashCode ^= _localized.GetHashCode();
return hashCode;
}
public override bool Equals(object obj)
{
if (obj == null || obj.GetType() != GetType())
return false;
var that = (LocalizedString)obj;
return string.Equals(_localized, that._localized);
}
}
public delegate LocalizedString Localizer(string text, params object[] args);
<div class="product-tags-box">
<div class="title">
<strong>@T("Products.Tags")</strong>
</div>
</div>
public abstract class WebViewPage<TModel> : System.Web.Mvc.WebViewPage<TModel>
{
private ILocalizationService _localizationService;
private Localizer _localizer;
private IWorkContext _workContext;
/// <summary>
/// Get a localized resources
/// </summary>
public Localizer T
{
get
{
if (_localizer == null)
{
//null localizer
//_localizer = (format, args) => new LocalizedString((args == null || args.Length == 0) ? format : string.Format(format, args));
//default localizer
_localizer = (format, args) =>
{
var resFormat = _localizationService.GetResource(format);
if (string.IsNullOrEmpty(resFormat))
{
return new LocalizedString(format);
}
return
new LocalizedString((args == null || args.Length == 0)
? resFormat
: string.Format(resFormat, args));
};
}
return _localizer;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment