Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 492
Stefano Avatar asked Jan 05 '23 17:01

Stefano


2 Answers

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.

like image 100
kdaveid Avatar answered Jan 12 '23 00:01

kdaveid


If you are using Areas, just put you controllername.culture.resx in the Controllers directory (exactly where controllername.cs is, don't create a 'Resources' directory anywhere).

I suggest you to test with a simple resx (controllername.resx). If it works, you can extend it with controllername.culture.resx

like image 22
AlbertSY Avatar answered Jan 12 '23 00:01

AlbertSY