예제 #1
0
        private bool MatchLanguageByCurrentCulture(InstallationLanguage language)
        {
            var curCulture = CultureInfo.GetCultureInfoByIetfLanguageTag("tr-TR");             // Thread.CurrentThread.CurrentCulture;

            if (language.Code.IsCaseInsensitiveEqual(curCulture.IetfLanguageTag))
            {
                return(true);
            }

            curCulture = curCulture.Parent;
            if (curCulture != null)
            {
                return(language.Code.IsCaseInsensitiveEqual(curCulture.IetfLanguageTag));
            }

            return(false);
        }
예제 #2
0
        public virtual IList <InstallationLanguage> GetAvailableLanguages()
        {
            if (_availableLanguages == null)
            {
                _availableLanguages = new List <InstallationLanguage>();
                var webHelper = EngineContext.Current.Resolve <IWebHelper>();
                foreach (var filePath in Directory.EnumerateFiles(webHelper.MapPath("~/App_Data/Localization/Installation/"), "*.xml"))
                {
                    var xmlDocument = new XmlDocument();
                    xmlDocument.Load(filePath);


                    //get language code
                    var languageCode = "";
                    //we file name format: installation.{languagecode}.xml
                    var r       = new Regex(Regex.Escape("installation.") + "(.*?)" + Regex.Escape(".xml"));
                    var matches = r.Matches(Path.GetFileName(filePath));
                    foreach (Match match in matches)
                    {
                        languageCode = match.Groups[1].Value;
                    }

                    //get language friendly name
                    var languageName = xmlDocument.SelectSingleNode(@"//Language").Attributes["Name"].InnerText.Trim();

                    //is default
                    var isDefaultAttribute = xmlDocument.SelectSingleNode(@"//Language").Attributes["IsDefault"];
                    var isDefault          = isDefaultAttribute != null?Convert.ToBoolean(isDefaultAttribute.InnerText.Trim()) : false;

                    //is default
                    var isRightToLeftAttribute = xmlDocument.SelectSingleNode(@"//Language").Attributes["IsRightToLeft"];
                    var isRightToLeft          = isRightToLeftAttribute != null?Convert.ToBoolean(isRightToLeftAttribute.InnerText.Trim()) : false;

                    //create language
                    var language = new InstallationLanguage
                    {
                        Code          = languageCode,
                        Name          = languageName,
                        IsDefault     = isDefault,
                        IsRightToLeft = isRightToLeft,
                    };
                    //load resources
                    foreach (XmlNode resNode in xmlDocument.SelectNodes(@"//Language/LocaleResource"))
                    {
                        var resNameAttribute = resNode.Attributes["Name"];
                        var resValueNode     = resNode.SelectSingleNode("Value");

                        if (resNameAttribute == null)
                        {
                            throw new WorkException("安装所有资源必须有一个属性名=\"Value\".");
                        }
                        var resourceName = resNameAttribute.Value.Trim();
                        if (string.IsNullOrEmpty(resourceName))
                        {
                            throw new WorkException("所有安装的资源属性名称必须有一个值.'");
                        }

                        if (resValueNode == null)
                        {
                            throw new WorkException("安装所有资源必须有一个元素 \"Value\".");
                        }
                        var resourceValue = resValueNode.InnerText.Trim();

                        language.Resources.Add(new InstallationLocaleResource()
                        {
                            Name  = resourceName,
                            Value = resourceValue
                        });
                    }

                    _availableLanguages.Add(language);
                    _availableLanguages = _availableLanguages.OrderBy(l => l.Name).ToList();
                }
            }
            return(_availableLanguages);
        }