Introduction
This article show the register and unregister custom culture on the local machine. In a second time, it show an use case of custom culture in an ASP.Net MVC project (using VS2013).
1 - New custom cultures
In the first step we create a new Console project, it allowed the custom culture's registering on the local machine.
- once created, add a reference to C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\sysglobl.dll
- in Program.cs file add "using System.Globalization;"
Next the instructions to register a culture :
static void AddCulture(string region, string baseCulture, string newCulture)
{
try {
var cultureInfos = new CultureInfo(baseCulture);
var regionInfos = new RegionInfo(region);
var builder = new CultureAndRegionInfoBuilder(newCulture, CultureAndRegionModifiers.None);
builder.LoadDataFromCultureInfo(cultureInfos);
builder.LoadDataFromRegionInfo(regionInfos);
builder.Register();
Console.WriteLine("SUCCES ADD [" + newCulture + "]");
}
catch (Exception e)
{
Console.WriteLine("ERREUR ADD [" + newCulture + "] : " + e.Message);
}
}
And to unregister it :
static void DeleteCulture(string newCulture)
{
try {
CultureAndRegionInfoBuilder.Unregister(newCulture);
Console.WriteLine("SUCCES DELETE [" + newCulture + "]");
}
catch (Exception e)
{
Console.WriteLine("ERREUR DELETE [" + newCulture + "] : " + e.Message);
}
}
In the Main sub we can write this code to add two cultures "fr-FR-Canin" and "fr-FR-Felin" :
string region = "FR";
string baseCulture = "fr-FR";
string[] tbCulture = new string[2] { "fr-FR-Felin", "fr-FR-Canin" };
Console.WriteLine("Taper 1 pour ajouter les cultures ou 2 pour les enlever :");
string key = Console.ReadLine();
if (key == "1") {
foreach (var item in tbCulture) AddCulture(region, baseCulture, item);
}
if (key == "2") {
foreach (var item in tbCulture) DeleteCulture(item);
}
Console.ReadLine();
To go more deep in Culture you have this article here.
Now we can build the project to create the executable file and launch it on the local machine with administrator privileges.
2 - How to use it
For that we are going to use an ASP.Net MVC application, and add/update files.
2.1 - Resource files
Here we create a new folder "Ressources" and add 3 resource files :
- the base one "Caracteristique.resx" which contains (property/value) :
- "Bruit" : "fait du bruit"
- "MetsFavoris" : "nourriture"
- "Ennemi" : "puce"
- the one for Felin "Caracteristique.resx" which contains (property/value) :
- "Bruit" : "miauler"
- "MetsFavoris" : "poisson"
- the one for Canin "Caracteristique.resx" which contains (property/value) :
- "Bruit" : "aboyer"
- "MetsFavoris" : "os"
2.2 - Model "Animal.cs"
We create it in the Models folder and add using instruction inside :
Then we add properties and set the Display attribut :
public string Nom { get; set; }
[Display(ResourceType = typeof(resx.Caracteristique), Name = "Bruit")]
public bool FaitDuBruit { get; set; }
[Display(ResourceType = typeof(resx.Caracteristique), Name = "MetsFavoris")]
public bool MetsFavoris { get; set; }
[Display(ResourceType = typeof(resx.Caracteristique), Name = "Ennemi")]
public bool Infecter { get; set; }
2.3 - Custom "ActionFilterAttribute"
Next we create a custom ActionFilterAttribute (in a new folder "Attributes") to define the culture to use when an action is called. The culture is passed as a parameter to the constructor. This custom action filter will be used like a class attribut in controllers.
using...
using System.Web.Mvc;
using System.Globalization;
using System.Threading;
namespace WebAppCulture.Attributes
{
[AttributeUsage(AttributeTargets.Class)]
public class LocalisationAttribute : ActionFilterAttribute
{
public readonly string CultureName;
public LocalisationAttribute(string cultureName)
{
CultureName = cultureName;
}
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo(CultureName);
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(CultureName);
}
}
}
2.4 - Controllers and View
In the Controllers folder we first create "BaseAnimalController.cs" and add one using instruction :
We arrange the Index action like that : (here is the default animal, not a dog, not a cat)
public ActionResult Index(Animal animal = null)
{
if (animal == null) { animal = new Animal() { Nom="Toutou" }; }
if (string.IsNullOrEmpty(animal.Nom)) { animal.Nom = "Toutou"; }
return View("Animal", animal);
}
In the second step we create the view "Animal.cshtml" in the Views\Shared folder :
- add "@model WebAppCulture.Models.Animal"
- and for all model properties :
- @Html.DisplayNameFor(model => model.property) when executed, the information is reliated to the culture
- @Html.DisplayFor(model => model.property)
In the third step we create two areas "Canin" and "Felin". For each of them we add a controller named "AnimalController.cs" who inherits from BaseAnimalController, so :
- in the two controllers we add
-
using WebAppCulture.Controllers;
-
using WebAppCulture.Attributes;
-
using WebAppCulture.Models;
- for the controller attribute we use the rigth culture (depends on the area) :
[LocalisationAttribute("fr-FR-Canin")]
public class AnimalController : BaseAnimalController
[LocalisationAttribute("fr-FR-Felin")]
public class AnimalController : BaseAnimalController
- and then the action for each :
public ActionResult Animal()
{
Animal animal = new Animal() {
Nom = "MiaouMiaou",
FaitDuBruit = false,
Infecter = true,
MetsFavoris = true };
return base.Index(animal);
}
|
public ActionResult Animal()
{
Animal animal = new Animal() {
Nom = "WouafWouaf",
FaitDuBruit = true,
Infecter = false,
MetsFavoris = false };
return base.Index(animal);
}
|
2.5 - Home page
In order to test all this is an example of content for the view "Views\Home\Index.cshtml"
<div class="row">
<div class="col-md-4">
<h2>CANIN</h2>
<p><a href="@Url.Action("Animal", "Animal", new { Area = "Canin" })"
class="btn btn-primary">Test canin</a></p>
</div>
<div class="col-md-4">
<h2>FELIN</h2>
<p><a href="@Url.Action("Animal", "Animal", new { Area = "Felin" })"
class="btn btn-primary">Test félin</a></p>
</div>
<div class="col-md-4">
<h2>AUTRE</h2>
<p><a href="@Url.Action("Index", "BaseAnimal")"
class="btn btn-primary">Test autre</a></p>
</div>
</div>
Here we go ! You can test it to see the custom culture in action.
Conclusion
The explanation is simple enough for the creation of custom culture. Regarding its use, that is how I put it into practice. But this is not the only way !!! Besides, I await your remarks and in what circumstances did you use custom cultures.
History