ant-design-blazor / ant-design-blazor

🌈A set of enterprise-class UI components based on Ant Design and Blazor WebAssembly.

Home Page:https://antblazor.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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 LocalizationServiceandLocalizationService.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

image
The Trace.WriteLine output en-US,so I think the SetLanguage below has also been execute properly