jsakamoto / Toolbelt.Blazor.I18nText

The class library that provides the ability to localize texts on your Blazor app!
Mozilla Public License 2.0
246 stars 23 forks source link

Change Language, selectlsit #56

Closed peterhym21 closed 1 year ago

peterhym21 commented 1 year ago

How can i get i to change language like the demo with the selectlist @jsakamoto?

https://jsakamoto.github.io/Toolbelt.Blazor.I18nText/

i have try to follow what you did in here:

https://github.com/jsakamoto/Toolbelt.Blazor.I18nText/blob/master/SampleSites/NETCore31App/Components/Shared/MainLayout.razor

but i cant seam to get i to work, any help.


@inject Toolbelt.Blazor.I18nText.I18nText I18nText 

<div class="form-row align-items-center text-muted px-4">
                    <div class="col"></div>
                    <div class="col-auto">
                        <span class="oi oi-globe" aria-hidden="true"></span>
                    </div>
                    <div class="col-auto">
                        <select class="form-control form-control-sm" @onchange="OnChangeCurrentLang">
                            <option value="en" selected="@(CurrentLang == "en")">English</option>
                            <option value="da" selected="@(CurrentLang == "da")">Dansk</option>
                        </select>
                    </div>
                </div>

@code {
    private string CurrentLang;

    I18nText.LanguageTable languageTable = new I18nText.LanguageTable();

    protected override async Task OnInitializedAsync()
    {
        var lang = await I18nText.GetCurrentLanguageAsync();
        if (new[] { "en", "da" }.Contains(lang.Split('-')[0]))
        {
            CurrentLang = lang;
        }
        else
        {
            CurrentLang = "en";
        }

        languageTable = await I18nText.GetTextTableAsync<I18nText.LanguageTable>(this);
        I18nText.ChangeLanguage += (s, a) =>
        {
            Console.WriteLine($"Language Changed: {a.LanguageCode}");
        };
    }

    private async Task OnChangeCurrentLang(ChangeEventArgs args)
    {
        Console.WriteLine($"OnChange: {args.Value}");
        CurrentLang = args.Value as string;
        await I18nText.SetCurrentLanguageAsync(CurrentLang);
    }
}
@inherits LayoutComponentBase
@using Blazored.Toast.Configuration

<PageTitle>MCC</PageTitle>

<div class="page">
    <RadzenContextMenu />
    <RadzenTooltip />
    <RadzenNotification />
    <RadzenDialog />
    <div class="sidebar">
        <NavMenu />
    </div>

    <main>

        <div class="top-row px-4 auth">
            <PageHeader/>
            <LoginDisplay />
            <LanguageSwithcer/>
        </div>
        <BlazoredToasts Position="ToastPosition.BottomCenter"
                        Timeout="10"
                        IconType="IconType.FontAwesome"
                        ErrorIcon="fas fa-times-circle"
                        InfoIcon="fas fa-info"
                        SuccessIcon="fas fa-check"
                        WarningIcon="fas fa-exclamation-triangle"
                        MaxToastCount="3" />

        <article class="content px-4">
            @Body
        </article>
    </main>
</div>

@page "/"
@inject IHttpContextAccessor httpContextAccessor
@inject IToastService ToastService
@inject HotKeys HotKeys
@inject Toolbelt.Blazor.I18nText.I18nText I18nText
@implements IDisposable

<PageTitle>Home</PageTitle>

<h1>@languageTable["HelloWorld"]</h1>

<div class="text-center">
    <img src="/Images/MCC.png" alt="MCC logo">
</div>

@code {
    HotKeysContext? HotKeysContext;
    I18nText.LanguageTable languageTable = new I18nText.LanguageTable();

    protected override async Task OnInitializedAsync()
    {
        languageTable = await I18nText.GetTextTableAsync<I18nText.LanguageTable>(this);
        this.HotKeysContext = this.HotKeys.CreateContext()
            .Add(ModKeys.None, Keys.F8 , Toaster);
    }

    void Toaster() => ToastService.ShowInfo("Congtats ypu just used a Hotkey: F8", "HotKey");

    public void Dispose() => this.HotKeysContext.Dispose();

}

language

jsakamoto commented 1 year ago

@peterhym21

Your code expects to be able to load "da" language localized text json,

image

But, there is no localized text json which matched "da" language code. (Instead, there is the "dk" language localized text json.)

image

That is the reason why the program didn't work well, isn't it?

peterhym21 commented 1 year ago

Yeah i see, i am totally blind,

Thanke you soo much for all the help. Much appriciated :)