简体   繁体   English

如何使用 appsettings 更改 api .net Core 中的语言资源?

[英]How change language Resources in api .net Core with appsettings?

I need to know how i can change language in my api .net core when start or deploy the same, defining in appsettings.json the language wanted.我需要知道如何在启动或部署相同的 api .net 核心时更改语言,在 appsettings.json 中定义所需的语言。 I saw about Localization and Globalization, but i dont need such a solution complicated.我看到了本地化和全球化,但我不需要这样复杂的解决方案。

For example... i have two resources files, Language (english) and Language.es (spanish):例如...我有两个资源文件,Language(英语)和 Language.es(西班牙语):

在此处输入图像描述

在此处输入图像描述

After in my appsettings.json i define the language wanted:在我的appsettings.json之后,我定义了想要的语言:

在此处输入图像描述

And i stablish the language change in my Startup.cs :我在我的Startup.cs中建立了语言更改:

在此处输入图像描述

I just need in the code the resource reference according to the configuration.我只需要根据配置在代码中引用资源。

在此处输入图像描述

But actually only obtain the responses in Spanish (server have language spanish default).但实际上只获取西班牙语的响应(服务器默认为西班牙语)。 How i can change the language resource?我如何更改语言资源?

With Thread.CurrentThread you are only setting the culture for the current thread at startup.使用Thread.CurrentThread ,您只需在启动时为当前线程设置区域性。 Try using these properties to set the culture for all threads in the application:尝试使用这些属性为应用程序中的所有线程设置区域性:

var cultureInfo = CultureInfo.CreateSpecificCulture(appSettings.Language)
CultureInfo.DefaultThreadCurrentCulture = cultureInfo;
CultureInfo.DefaultThreadCurrentUICulture = cultureInfo;

For more information have a look at the docs: https://docs.microsoft.com/en-us/dotnet/api/system.globalization.cultureinfo.defaultthreadcurrentculture?view=netcore-3.1有关更多信息,请查看文档: https://docs.microsoft.com/en-us/dotnet/api/system.globalization.cultureinfo.defaultthreadcurrentculture?view=netcore-3.1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM