1

I have a web-api project that has several controllers, I implemented localization (English and Korean) for it but want one or two controllers return messages in English even when the language is Korean (don't want use simple English message and want to read from English resource)

Is it possible?

I setup localization like below:

 services.Configure<RequestLocalizationOptions>(options =>
            {
                var cultures = new List<CultureInfo> {
                    new CultureInfo("en"),
                    new CultureInfo("kr")
                };
                options.DefaultRequestCulture = new Microsoft.AspNetCore.Localization.RequestCulture("en");
                options.SupportedCultures = cultures;
                options.SupportedUICultures = cultures;
            });

            services.AddMvc().AddDataAnnotationsLocalization(options =>
            {
                options.DataAnnotationLocalizerProvider = (type, factory) =>factory.Create(typeof(SharedTranslate));
            });
            services.AddLocalization(o =>
            {
                o.ResourcesPath = "Resources";
            });

and in constructor :

public AuthAdminController(IStringLocalizer<SharedTranslate> localizer,...

I use below code in constructor of controller but didn't work

//First
            System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
            //Second
            System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture("en");
SajjadZare
  • 2,487
  • 4
  • 38
  • 68
  • Yes. You can set: `Thread.CurrentThread.CurrentCulture = ...` and `Thread.CurrentThread.CurrentUICulture = ...` by yourself. Something like that. – Ergis Jan 17 '21 at 09:14
  • @Ergis I add it in constructor of controller but not work – SajjadZare Jan 17 '21 at 09:28
  • System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US"); – SajjadZare Jan 17 '21 at 09:29
  • Please share [mcve]. – mjwills Jan 17 '21 at 09:30
  • @SajjadZare it has to work. I've just recently done it. Put a debugger point and check what is the value of `Thread.CurrentThread.CurrentCulture` right before you return the View – Ergis Jan 17 '21 at 09:33
  • How did you set up the localization? can you post that too? If you are injecting `IStringLocalizerFactory` or a wrapper over that then from your controller, you can enforce to send English as language code. – user1672994 Jan 17 '21 at 09:33
  • @user1672994 I edited the question and add some code – SajjadZare Jan 17 '21 at 09:38
  • @Ergis I edited the question and add some code – SajjadZare Jan 17 '21 at 09:38
  • You have to set the `CurrentUICulture`. Fully read the word, it has a `UI` in it :D Check [this](https://stackoverflow.com/questions/329033/what-is-the-difference-between-currentculture-and-currentuiculture-properties-of) important piece of information – Ergis Jan 17 '21 at 09:42

1 Answers1

5

Use the following code while retrieving the localized string in AuthAdminController

 CultureInfo.CurrentUICulture = new CultureInfo("<<Your language Code>>");
 var localizedString = localizer[resourceKey].Value;
 CultureInfo.CurrentUICulture = new CultureInfo("<<Reset to default one>>");
 return localizedString;

The first line setting the CurrentUICulture and 3rd line resetting to default one.

user1672994
  • 10,509
  • 1
  • 19
  • 32