//------------------------------------------------------------------------------
// <auto-generated>
//     此代码由工具生成。
//     运行时版本:2.0.50727.3082
//
//     对此文件的更改可能会导致不正确的行为，并且如果
//     重新生成代码，这些更改将会丢失。
// </auto-generated>
//------------------------------------------------------------------------------

using System.Text.RegularExpressions;
using System.Resources;
namespace Sheng.Winform.Controls.Localisation {
    
    /// <summary>
    /// 
    /// </summary>
    public class Language {
        
        private static ILanguage _Language;
        
        private Language() {
        }
        
        /// <summary>
        ///  Call GetLanguages() to retrieve a list of possible languages that can be used to set this property.
        ///  The default value is the default language.
        /// </summary>
        public static ILanguage Current {
            get {
                if ((_Language == null)) {
                    //System.Collections.Generic.List<ILanguage> list = Language.GetLanguages();
                    //for (int i = 0; (i < list.Count); i = (i + 1)) {
                    //    if (list[i].IsDefault) {
                    //        _Language = list[i];
                    //        return _Language;
                    //    }
                    //}

                    _Language = new Chinese__Simplified_();
                }
                return _Language;
            }
            set {
                _Language = value;
            }
        }
        
        /// <summary>
        ///  Gets a list of available languages defined in this assembly.
        /// </summary>
        public static System.Collections.Generic.List<ILanguage> GetLanguages() {
            System.Collections.Generic.List<ILanguage> items = new System.Collections.Generic.List<ILanguage>();
            System.Type[] exportedTypes = System.Reflection.Assembly.GetExecutingAssembly().GetExportedTypes();
            for (int i = 0; (i < exportedTypes.Length); i = (i + 1)) {
                if (exportedTypes[i].IsClass) {
                    if ((exportedTypes[i].GetInterface("ILanguage") != null)) {
                        try {
                            object obj = System.Activator.CreateInstance(exportedTypes[i]);
                            ILanguage interfaceReference = ((ILanguage)(obj));
                            if ((interfaceReference != null)) {
                                items.Add(interfaceReference);
                            }
                        }
                        catch (System.Exception ) {
                        }
                    }
                }
            }
            return items;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static string GetString(string name)
        {
            return Current.ResourceManager.GetString(name);
        }

        //取${}花括号中间的字符串
        readonly static Regex patternInner = new Regex(@"(?<=\${)([^\}]*)?(?=\})",
            RegexOptions.Compiled | RegexOptions.CultureInvariant);

        //取${}形式的完整字符串
        readonly static Regex pattern = new Regex(@"\$\{[^\}]+\}",
           RegexOptions.Compiled | RegexOptions.CultureInvariant);
        /// <summary>
        /// 如果输入的字符串包含 "${...}" 这样的格式,则认为是指代资源文件中的一个字符串资源
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string Parse(string input)
        {
            if (input == null)
            {
                return null;
            }

            string result = input;
            string resString;
            MatchCollection matchs = pattern.Matches(input);
            foreach (Match match in matchs)
            {
                resString = GetString(patternInner.Match(match.Value).Value);
                resString = resString.Replace("$NewLine$", System.Environment.NewLine);
                result = result.Replace(match.Value, resString);
            }

            return result;
        }
    }
}
