jsakamoto / Toolbelt.Blazor.I18nText

The class library that provides the ability to localize texts on your Blazor app!
Mozilla Public License 2.0
246 stars 23 forks source link

How TO Set Initial Language in Startup.cs #14

Closed HassanBakri closed 4 years ago

HassanBakri commented 4 years ago

i found solution on the following issue #8 but i thought it might be better solution exist

jsakamoto commented 4 years ago

Sorry to late!

You can customize the way to determine initial language by setting GetInitialLanguageAsync property of I18nTextOptions class to your custom logic at ConfigureServices() in your Startup class, like this:

 public class Startup
 {
   public void ConfigureServices(IServiceCollection services)
   {
     services.AddI18nText<Startup>(options =>
     {
        options.GetInitialLanguageAsync = ...; // set your custom method or function.
     });
    ...

The signature of getting initial language method/function is:

async ValueTask<string> YourCustomMethodOrFunction(
  IServiceProvider serviceProvider,
  I18nTextOptions options)
{
  // ... do something for determine initial language...
  return "<language and country code, ex: "en-US", "ja-JP", etc.>";
}

If you want to hard coding the language code literal (for example, you want to set french language) in Startup.cs, you can do it like this:

 public class Startup
 {
   public void ConfigureServices(IServiceCollection services)
   {
     services.AddI18nText<Startup>(options =>
     {
        options.GetInitialLanguageAsync = (svc, opt) => new ValueTask<string>("fr-FR");
     });
    ...

See also: API Reference - I18nTextDependencyInjection class

HassanBakri commented 4 years ago

thank you , seems to be working