I am trying to implement a language switching method, created a dll in which there are two classes enEN.cs and ruRU.cs

public const string NAME_ELEMENT = "НАЗВАНИЕ ЭЛЕМЕНТА"; 

I can not figure out how to create a class that would change the values ​​of the rows of my form, based on the language that I choose? If you save the default value in a file, then initially it needs to be created when the form is loaded. You also need to save the value of the language that the user selects.

Connect string to element:

 label.Content = EN.String; label.Content = RU.String; 

How can I make it easier? The form must load (for example) RU.String; first, and when choosing another option, change all such values ​​in the form to EN.String; ..

  • four
    DynamicResource + ResourceDictionary. Somewhere there was already such a question. - VladD
  • To be honest, I asked this question because I can’t think of how to save the language setting so that the program, after reading the file, realizes that it needs to load the library with the class (eg enEN) and when choosing the language to load another library class. I have no ideas, I decided to consult - Elizaveta
  • Saving a setting is a separate issue. Dynamic switching in runtime is more difficult. - VladD
  • one
    Here is a good approach to switching languages ​​in runtime in WPF. - Surfin Bird
  • 2

1 answer 1

If you want to do localization through files (which I do not advise doing), then you can:

  public interface ILocalization { string NameLabelText {get;} string LastNameLabelText {get;} } public class RuLocalization : ILocalization { public string NameLabelText {get;} = "Имя"; public string LastNameLabelText {get;} = "Фамилия" } public class EnLocalization : ILocalization { public string NameLabelText {get;} = "Name"; public string LastNameLabelText {get;} = "LastName" } public class View { public View(ILocalization Localization) { labelName.Content = Localization.NameLabelText; labelLastName.Content = Localization.LastNameLabelText; } } 

I would recommend using a ready-made framework. https://www.codeproject.com/kb/wpf/wpf_resx_localization.aspx