Monday, May 1, 2017

How to : Asp.Net Core Resource Files in Separate Project / DLL

About the Post:
In this post, we will show how to consume Asp.Net Core Resource Files from a Separate Project / DLL, We will use Web API Core as an example.

Why :
 I love software design "Separation Of Concerns" concept. and recently we develop a web API contains many resources so I tried to decouple the resources files into a separate project.
Unfortunately, I can't found much resources talking about this matter at the moment, I guess because the .Net Core still in earlier stages. However, I'm writing this post to let you know how to do it.

Solution Steps:
1- Create New .Net Core Class Library project Call it "MyApp.Resources.cs".
2- From Nuget console install package: > Install-Package Microsoft.Extensions.Localization
3- Add New Folder named "Resources".
4- Inside "Resources" Folder add the following resources files from VS Add new item resources file:
     - Add All supported Language you need, here we will support Arabic & English,
                - SharedResources.resx
                - SharedResources.ar-SA.resx
                - SharedResources.en-US.resx


5- Create New Interface named "AppResource.cs": 

namespace MyApp.Resources
{
    public interface IAppResource
    {
        string GetResource(string key);
    }
}
--------------------------------------------------------------------

6- Create New Class library file, named "SharedResources.cs" that implement  our IAppResource interface:

namespace MyApp.Resources
{
    public class SharedResources : IAppResource
    {
        private IStringLocalizer<SharedResources> _localizer;
        public SharedResources(IStringLocalizer<SharedResources> localizer)
        {
            _localizer = localizer;
        }

        public string GetResource(string key)
        {
            return _localizer[key];
        }
    }
}
--------------------------------------------------------------------

7- Create New Web API Core Project From VS.

8- In the Project Startup.cs file inside  -> ConfigureServices :

// Firstly: Configure our Localisation middleware
services.AddLocalization(
                opts =>
                {
                    opts.ResourcesPath = "Resources";
                }
                );

            services.Configure<RequestLocalizationOptions>(
           opts =>
           {
               var supportedCultures = new List<CultureInfo>
               {
                new CultureInfo("en-US"),
                new CultureInfo("en"),
                new CultureInfo("ar-SA"),
                new CultureInfo("ar")                
               };
               opts.DefaultRequestCulture = new RequestCulture("ar-SA");             
               opts.SupportedCultures = supportedCultures;              
               opts.SupportedUICultures = supportedCultures;
           });
            // --------------
// Secondly: register our shared resources service for DI 
            services.AddSingleton<IAppResource, SharedResources>();
--------------------------------------------------------------------

9- Inside web API project Add new Controller named "LocalizationTestController":

       private readonly IAppResource _sharedLocalizer;
        public LocalizationTestController(IAppResource sharedLocalizer)
        {
            _sharedLocalizer = sharedLocalizer;
        }
       
        [HttpGet]
        public string Get()
        {
            string result = _sharedLocalizer.GetResource("Hi");
            return result;
        }


--------------------------------------------------------------------

10-  now try your API request:
api/LocalizationTest 
api/LocalizationTest/?culture=ar-SA




No comments: