确保您已经在中央位置定义了资源 您可以按照以下步骤操作: 在Blazor Server应用程序中, a. 添加“Resources”文件夹; b. 在“Resources”文件夹下添加文件“SharedResources.resx”; c. 在“SharedResources.resx”中添加名称/值对(例如,“Hello”/“你好”); d. 您也可以为其他本地化创建其他“resx”文件(例如,为日语添加“SharedResources.ja.jp.resx”)。
在Blazor Server程序中配置中央资源
a. 打开Startup.cs文件(位于Blazor Server应用程序的根目录下)。
b. 注册IStringLocalizer服务(请在“ConfigureServices”方法中):
services.AddLocalization(options => options.ResourcesPath = "Resources");
services.Configure
options.DefaultRequestCulture = new RequestCulture("en-US"); options.SupportedCultures = supportedCultures; options.SupportedUICultures = supportedCultures; });
在页面中显示本地化字符串 a. 添加@using Microsoft.Extensions.Localization指令。 b. 在页面中使用属性来检索本地化字符串(例如@Localizer["Hello"])。
请注意,在更改完毕后您需要重新启动应用程序以使更改生效。