コード例 #1
0
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string key             = KeyProvider.ProvideKey(new[] { value });
            var    cultureInfo     = value as CultureInfo;
            object localizedObject = CommonResourceManager.Instance.GetResourceObject(key, cultureInfo);

            return(localizedObject ?? ResXExtension.GetDefaultValue(key));
        }
コード例 #2
0
        /// <summary>
        /// Returns localized string (object) by composite key.
        /// Key parts are in <paramref name="values"/> except last member of array.
        /// Last member is <see cref="CultureInfo"/>
        /// Firstly <see cref="KeyProvider"/> makes key from it's BaseKey and key parts from <paramref name="values"/>.
        /// Then localized object returned from resources depending on this key
        /// </summary>
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            if (Parameters.Count + 1 != values.Length)
            {
                throw new ArgumentException(string.Format("ResConverter - parameters count error. Parameters.Count = {0}, values.Count = {1}", Parameters.Count, values.Length));
            }
            // values are the mix from ResKeyParts and ResParams. And culture in last parameter.
            // Separate them as Cinderella to different heaps.
            var resKeyParts = new List <object>();
            var resParams   = new List <object>();

            for (int i = 0; i < Parameters.Count; i++)
            {
                object      value = values[i];
                BindingBase param = Parameters[i];
                if (param is ResKeyPart)
                {
                    if (value == DependencyProperty.UnsetValue && param.FallbackValue != null)
                    {
                        value = param.FallbackValue;
                    }
                    if (value != null)
                    {
                        resKeyParts.Add(value);
                    }
                }
                else if (param is ResParam)
                {
                    resParams.Add(value);
                }
            }
            CultureInfo cultureInfo = values[values.Length - 1] as CultureInfo;

            string key             = KeyProvider.ProvideKey(resKeyParts);
            object localizedObject = ResManager.Instance.GetResourceObject(key, cultureInfo) ??
                                     ResManager.Instance.GetResourceObject(ResExtension.Key, cultureInfo);

            if (localizedObject == null)
            {
                localizedObject = ResExtension.GetDefaultValue(key);
            }
            else
            {
                try
                {
                    if (resParams.Count > 0)
                    {
                        localizedObject = string.Format(localizedObject.ToString(), resParams.Cast <object>().ToArray());
                    }
                }
                catch (FormatException)
                {
                }
            }
            return(localizedObject);
        }
コード例 #3
0
ファイル: ResXConverter.cs プロジェクト: Michael-Z/DriveHud
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            Check.Require(Parameters.Count + 1 == values.Length, string.Format("DH: ResXConverter - parameters count error. Parameters.Count = {0}, values.Count = {1}", Parameters.Count, values.Length));

            var resXKeyParts = new List <object>();
            var resXParams   = new List <object>();

            for (int i = 0; i < Parameters.Count; i++)
            {
                object value = values[i];

                BindingBase param = Parameters[i];

                if (param is ResXKeyPart)
                {
                    if (value == DependencyProperty.UnsetValue && param.FallbackValue != null)
                    {
                        value = param.FallbackValue;
                    }
                    if (value != null)
                    {
                        resXKeyParts.Add(value);
                    }
                }
                else if (param is ResXParam)
                {
                    resXParams.Add(value);
                }
            }

            CultureInfo cultureInfo = values[values.Length - 1] as CultureInfo;

            string key = KeyProvider.ProvideKey(resXKeyParts);

            object localizedObject = CommonResourceManager.Instance.GetResourceObject(key, cultureInfo) ??
                                     CommonResourceManager.Instance.GetResourceObject(ResXExtension.Key, cultureInfo);

            if (localizedObject == null)
            {
                localizedObject = ResXExtension.GetDefaultValue(key);
            }
            else
            {
                try
                {
                    localizedObject = string.Format(localizedObject.ToString(), resXParams.Cast <object>().ToArray());
                }
                catch
                {
                }
            }

            return(localizedObject);
        }