Can't use localization
sciencekiller opened this issue · comments
While I was trying to add localization resource files, I meet a problem. I completely follow the guide in the official document but after I add Dashboard.en-US.resx and Dashboard.zn-CN.resx, I can't use IStringLocalizer, for there's no csharp class and nothing can be plece in the generic argument.
Any help are appreciated
Thanks for contacting us @sciencekiller , you can just add a empty Class for your self. Because the IStringLocalizer just use the type to locate the resource.
OK, that works. However, when I use @inject ILocalizationService LocalizationService
andLocalizationService.SetLanguage(new CultureInfo("en-US"));
,the UI culture doesn't change. It's useless to refresh the page either.
You also need to add a event handler for ILocalizationService .LanguageChanged
and call StateHasChanged to refresh the component.
protected override async Task OnInitializedAsync()
{
if (RequestLocalizationOptions != null && RequestLocalizationOptions.Value.SupportedCultures != null)
{
supportedCultures = RequestLocalizationOptions.Value.SupportedCultures.ToList();
}
LocalizationService.LanguageChanged += OnLanguageChanged;
}
private void OnLanguageChanged(object sender, CultureInfo e)
{
// 处理语言更改事件的逻辑
StateHasChanged(); // 如果需要更新UI,调用StateHasChanged()
}
// 记得在组件或页面销毁时取消订阅
public void Dispose()
{
LocalizationService.LanguageChanged -= OnLanguageChanged;
}
But that makes no use