codingseb / Localization

A suite to localize C# and WPF projects easily based on file format you choose.
MIT License
37 stars 6 forks source link

How to update language in real-time without change language in combobox #2

Closed linhmc closed 3 years ago

linhmc commented 3 years ago

Hi @codingseb , I am coding to help user can change language in real-time. That mean user change in .tm.json file and save in text editor, in UI when user refresh button it change language automatically image How can I reach it?

linhmc commented 3 years ago

Hi @codingseb

In Loc.cs class I suggest add method.

public void RaiseLanguageChange()
{
    TMLanguageChangingEventArgs changingArgs = new TMLanguageChangingEventArgs(currentLanguage, currentLanguage);
    TMLanguageChangedEventArgs changedArgs = new TMLanguageChangedEventArgs(currentLanguage, currentLanguage);
    CurrentLanguageChanging?.Invoke(this, changingArgs);
    if (!changingArgs.Cancel)
    {
        CurrentLanguageChanged?.Invoke(this, changedArgs);
    }
}

image

Developer can invoke to update language. Please consider.

codingseb commented 3 years ago

Hello again @linhmc Yes it's a good idea. Thanks for it.

I will include it in Loc.cs as soon as possible and publish a new CodingSeb.Localization version

codingseb commented 3 years ago

Ok I published :

To allow hot reloading of files and implement the method RaiseLanguageChangeEvents in class Loc. I also implemented an example of hot reloading in CodingSeb.Localization.Examples. See :

private void LanguageChangedRefreshButton_Click(object sender, RoutedEventArgs e)
{
    Languages.ReloadFiles();
    Loc.Instance.RaiseLanguageChangeEvents();
}
public static void ReloadFiles()
{
    string exampleFileFileName = Path.Combine(languagesFilesDirectory, "Example1.loc.json");
    LocalizationLoader.Instance.ClearAllTranslations();
    LocalizationLoader.Instance.AddFile(exampleFileFileName);
}
linhmc commented 3 years ago

Many thanks @codingseb.