You can create and access culture specific strings using a resource file.
First create a resource file and name it according to the culture code. So for the default you'll have Names.resx
and for french you'll have Names.fr-FR.resx
. From here you should open up the properties of each resx file and give it a similar custom tool namespace such as ViewRes
. Now when you access the resx file to grab a string like so: ViewRes.Names.MyString
you'll get the string according to the current culture defined in Thread.CurrentThread.CurrentCulture
which you can set. You can set this using the accept-language in your Global.asax.cs file like so:
protected void Application_AcquireRequestState(object sender, EventArgs e)
{
string culture = HttpContext.Request.ServerVariables.Get("HTTP_ACCEPT_LANGUAGE");
CultureInfo ci = culture as CultureInfo;
if (ci == null)
ci = new CultureInfo("en");
Thread.CurrentThread.CurrentUICulture = ci;
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ci.Name);
}
Now the next time you access ViewRes.Names
in your controller it will be with the culture set by accept-language.
You can also set the culture when accessing your resx strings like so:
[ResponseType(typeof(Product))]
public IHttpActionResult GetProduct(int id, string culture)
{
ViewRes.Names.Culture = new CultureInfo(culture);
return Ok(new Product(){Name = ViewRes.Names.MyString, Price = 10});
}