Search code examples
c#asp.netlocalization

Asp.net core areas localization


Hi all I'm involved in a localization project. We are working with areas and we need to create the resource file for the languages supported. The default structure ( Resources/Controllers/controllername.culture.resx) work correctly like Microsoft docs. we would like know the directory structure for the rest areas. We have looked for some docs but none we have found. Thx to all


Solution

  • Resource files for C# objects (not views) are being resolved by the namespace they are in. They are relative to the ResourcePath set during the initialization of MVC localization.

    Let's say you have set the ResourcesPath in your to "Resources"

    services.AddLocalization(s => s.ResourcesPath = "Resources");

    and you have a HomeController without area like this:

    namespace WebApp.Controllers
    {
       public class HomeController : Controller
       {
         ...
       }
    }
    

    then your resource files in the folder Resources>Controllers>HomeController.{culture}.resx will be picked up.

    In case of a controller in a area you might change to a different namespace like this:

    namespace WebApp.Admin.Controllers
    {
       [Area("admin")]
       public class HomeController : Controller
       {
         ...
       }
    }
    

    Note, this is the HomeController of the admin area.

    In this case your resource files for this particular controller can live in the folder:

    Resources>Admin>Controllers>HomeController.{culture}.resx

    OR can have this naming:
    Admin.Controllers.HomeController.{culture}.resx

    This works in ASP.NET Core 1.1.0. I have updated the AspNetCore.Identity.Localization project with an area.