Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

MVC 6 Localization

$
0
0

Dear All,

I'm working in a projects with MVC6 that needs English-Arabic support.

I created my project and implemented the IStringLocalizer, everything works perfect for the default culture (en-US) (in both Controllers & Views)

Note: My resources files are inside the same application with naming considerations

When I change the culture to Arabic, same string values are being retrieved by the resource manager, although the culture has been set to "ar-QA".

Here is my code:

LanguageActionFilter :

public class LanguageActionFilter : ActionFilterAttribute
{

public LanguageActionFilter(ILoggerFactory loggerFactory)
{

}

public override void OnActionExecuting(ActionExecutingContext context)
{

string culture = (string)context.RouteData.Values["culture"] ?? "en-US";


System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo(culture);
System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture);

CultureInfo.CurrentCulture = new CultureInfo(culture);
CultureInfo.CurrentUICulture = new CultureInfo(culture);

base.OnActionExecuting(context);
}
}

TestStringLocalizer:

public class TestStringLocalizer : IStringLocalizer
{
private readonly IResourceNamesCache _resourceNamesCache;
private ResourceManager _resourceManager;
private readonly AssemblyWrapper _resourceAssemblyWrapper;
private readonly string _resourceBaseName;
private string _applicationBasePath;

public TestStringLocalizer(ResourceManager resourceManager,
AssemblyWrapper resourceAssembly,
string baseName,
IResourceNamesCache resourceNamesCache,
string applicationBasePath)
{
_resourceAssemblyWrapper = resourceAssembly;
_resourceManager = resourceManager;
_resourceBaseName = baseName;
_resourceNamesCache = resourceNamesCache;
_applicationBasePath = applicationBasePath;
}

public virtual LocalizedString this[string name]
{
get
{
var value = GetStringSafely(name, null);
return new LocalizedString(name, value ?? name, resourceNotFound: value == null);
}
}

public virtual LocalizedString this[string name, params object[] arguments]
{
get
{
var format = GetStringSafely(name, null);
var value = string.Format(format ?? name, arguments);
return new LocalizedString(name, value, resourceNotFound: format == null);
}
}

public IStringLocalizer WithCulture(CultureInfo culture)
{
return new TestStringLocalizer(_resourceManager,
_resourceAssemblyWrapper,
_resourceBaseName,
_resourceNamesCache,
_applicationBasePath);
}

public virtual IEnumerable<LocalizedString> GetAllStrings(bool includeAncestorCultures) =>
GetAllStrings(includeAncestorCultures, CultureInfo.CurrentUICulture);

protected IEnumerable<LocalizedString> GetAllStrings(bool includeAncestorCultures, CultureInfo culture)
{
throw new NotImplementedException();
}

protected string GetStringSafely(string name, CultureInfo culture)
{

var resourceValue = string.Empty;

var cultureName = (culture ?? System.Threading.Thread.CurrentThread.CurrentCulture).Name;
CultureInfo currentCulture = System.Threading.Thread.CurrentThread.CurrentCulture;

var baseName = "Application.FullNameSpace.MainResource";

try
{

_resourceManager = new ResourceManager(baseName, Assembly.GetExecutingAssembly());
resourceValue = _resourceManager.GetString(baseName, currentCulture);

}
catch (Exception ex)
{
string error = ex.Message;
return name;
}


return resourceValue;
}
}

Please advise.

Thank You,

Rania


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>