Git Product home page Git Product logo

.net-core-localization-and-enum's Introduction

.NET Core Localization and Enum

This is a demo project base on .NET core WebAPI template WeatherForecast, implement Localization on Text and Enum, read i18n text from resources.


  1. Add resource file(.resx)
    Usually I will put my resource file in [Resources] folder, remember put cultureInfo name on resx file like this {name}.{culture}.resx
    img
  2. Setup Startup.cs
    In public void ConfigureServices(IServiceCollection services) add following code
    //set Support Cultures and default
    services.Configure<RequestLocalizationOptions>(options =>
    {
        var supportedCultures = new[]{
            new CultureInfo("en-US"),
            new CultureInfo("zh-TW"),
            new CultureInfo("ja-JP")
        };
        options.DefaultRequestCulture = new RequestCulture("en-US");
        options.SupportedCultures = supportedCultures;
        options.SupportedUICultures = supportedCultures;
    });
    //set resource path
    services.AddLocalization(options => options.ResourcesPath = "Resources");
  3. Setup controller
    • DI
      private readonly IStringLocalizer _localizer;
      public YourController(IStringLocalizerFactory factory)
      {
         _localizer = factory.Create("Message", System.Reflection.Assembly.GetExecutingAssembly().GetName().Name);
      }
    • Get Value
      using _localizer[{name}] to get the value from resource file, for example, _localizer["cold"] can get key name [cold]'s culture string, if can't find the key in resource, it will just output the key name "cold" as string.
      You can check WeatherForecastController.cs for more usage detail.
  • Enum with i18N
    It was easy, just use _localizer[{enumName}.ToString()] to get culture string from resource, or you can check here for the usage.

That's all.

.net-core-localization-and-enum's People

Stargazers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.