How to use wpflocalizeextension in Code-Behind?

How can I use wpflocalizeextension in c # code? In xaml, to get a localized string, I can use it like this:

<Window x:Class="SomeClass"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:lex="http://wpflocalizeextension.codeplex.com"    
    lex:LocalizeDictionary.DesignCulture="uk-UA"    
   lex:ResxLocalizationProvider.DefaultAssembly="DesktopApp"    
   lex:ResxLocalizationProvider.DefaultDictionary="Resources">
   <Button Content="{lex:Loc SignInBtn}"/>

How can I get a localized string in code, for example MessageBox.Show("SignInBtn");?

+6
source share
4 answers

It is pretty simple. Localization keys are saved as AssemblyName: Resources : KeyName, where Resources is the name of the class Resources, as a rule, you will not change it to something else.

You can create a simple wrapper to get localized values:

using WPFLocalizeExtension.Extensions;

public static class LocalizationProvider
{
    public static T GetLocalizedValue<T>(string key)
    {
        return LocExtension.GetLocalizedValue<T>(Assembly.GetCallingAssembly().GetName().Name + ":Resources:" + key);
    }
}

, , "SignInBtn", :

MessageBox.Show(LocalizationProvider.GetLocalizedValue<string>("SignInBtn"));
+11

:

LocalizeDictionary.Instance.GetLocalizedObject("keyComesHere", null, LocalizeDictionary.Instance.Culture).ToString()

, .

+2

mcy - , .

3- , , :

LocalizeDictionary.Instance.GetLocalizedObject("AssemblyName", "DictionaryName", "Key", LocalizeDictionary.Instance.Culture);
0

Properties.Resources.yourKey

0

All Articles