2017. 5. 16. 10:22

공통

    public static class LangPack

    {

        static LangPack()

        {

            Resources = new Dictionary<Language, Dictionary<string, string>>();


            Dictionary<string, string> kr = new Dictionary<string, string>();

            kr.Add("language", "한국어");


            Dictionary<string, string> en = new Dictionary<string, string>();

            en.Add("language", "영어");


            Dictionary<string, string> ch = new Dictionary<string, string>();

            ch.Add("language", "중국어");


            Resources.Add(Language.Korean, kr);

            Resources.Add(Language.English, en);

            Resources.Add(Language.Chinese, ch);

        }


        private static Dictionary<Language, Dictionary<string, string>> Resources;


        public static string GetString(string key)

        {

            if (Resources.ContainsKey(App.SelectedLanguage))

            {

                if (Resources[App.SelectedLanguage].ContainsKey(key))

                    return Resources[App.SelectedLanguage][key];

            }


            return string.Empty;

        }

    }


컨버터

    public class LanguageConverter : IValueConverter

    {

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)

        {

            if (parameter == null)

                return string.Empty;


            if (parameter is string)

                return LangPack.GetString((string)parameter);

            else

                return string.Empty;

        }


        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)

        {

            throw new NotImplementedException();

        }

    }


Application Resource

   <Application.Resources>

        <ResourceDictionary>

            <converter:LanguageConverter x:Key="LangConverter" />

            <ObjectDataProvider x:Key="language"

                ObjectType="{x:Type lang:LangPack}"

                MethodName="GetString" >

                <ObjectDataProvider.MethodParameters>

                    <s:String>language</s:String>

                </ObjectDataProvider.MethodParameters>

            </ObjectDataProvider>

        </ResourceDictionary>

    </Application.Resources>


XAML

<Button Content="{Binding Source={StaticResource language}}" />

<Button Content="{Binding Converter={StaticResource LangConverter},  ConverterParameter=language}" />







Posted by CoolDragon