Esempio n. 1
0
        private List <LanguageConfiguration> LoadFiles(string languageFilePattern)
        {
            List <LanguageConfiguration> loadedLanguages = new List <LanguageConfiguration>();
            List <string> languageDirectories            = new List <string>();

            if (IniConfig.IsPortable)
            {
                languageDirectories.Add(PAF_LANGUAGE_PATH);
            }
            else
            {
                languageDirectories.Add(APPLICATIONDATA_LANGUAGE_PATH);
            }
            languageDirectories.Add(STARTUP_LANGUAGE_PATH);
            foreach (string path in languageDirectories)
            {
                // Search in executable directory
                LOG.DebugFormat("Searching language directory '{0}' for language files with pattern '{1}'", path, languageFilePattern);
                try {
                    foreach (string languageFile in Directory.GetFiles(path, languageFilePattern, SearchOption.AllDirectories))
                    {
                        LOG.DebugFormat("Found language file: {0}", languageFile);
                        LanguageConfiguration languageConfig = LanguageConfiguration.Load(languageFile);
                        if (languageConfig != null)
                        {
                            if (string.IsNullOrEmpty(languageConfig.LanguageGroup) || supportedLanguageGroups.Contains(languageConfig.LanguageGroup))
                            {
                                LOG.InfoFormat("Loaded language: {0}", languageConfig.Description);
                                loadedLanguages.Add(languageConfig);
                            }
                            else
                            {
                                LOG.InfoFormat("Skipping unsupported language: {0}", languageConfig.Description);
                            }
                        }
                    }
                } catch (DirectoryNotFoundException) {
                    LOG.InfoFormat("Non existing language directory: {0}", path);
                } catch (Exception e) {
                    LOG.Error("Error trying for read directory " + path, e);
                }
            }
            return(loadedLanguages);
        }
Esempio n. 2
0
        /// <summary>
        /// loads a language configuration from a file path
        /// </summary>
        public static LanguageConfiguration Load(string path)
        {
            LanguageConfiguration ret = null;

            try {
                XmlDocument doc = new XmlDocument();
                doc.Load(path);
                XmlNodeList nodes = doc.GetElementsByTagName("language");
                if (nodes.Count > 0)
                {
                    ret      = new LanguageConfiguration();
                    ret.File = path;
                    XmlNode node = nodes.Item(0);
                    ret.Description = node.Attributes["description"].Value;
                    ret.Ietf        = node.Attributes["ietf"].Value;
                    ret.Version     = node.Attributes["version"].Value;
                    if (node.Attributes["languagegroup"] != null)
                    {
                        string languageGroup = node.Attributes["languagegroup"].Value;
                        ret.LanguageGroup = languageGroup.ToLower();
                    }

                    XmlNodeList resourceNodes = doc.GetElementsByTagName("resource");
                    ret.Resources = new List <Resource>(resourceNodes.Count);
                    foreach (XmlNode resourceNode in resourceNodes)
                    {
                        Resource res = new Resource();
                        res.Name = resourceNode.Attributes["name"].Value;
                        res.Text = resourceNode.InnerText;
                        ret.Resources.Add(res);
                    }
                }
                else
                {
                    throw new XmlException("Root element <language> is missing");
                }
            } catch (Exception e) {
                LOG.Error("Could not load language file " + path, e);
            }
            return(ret);
        }
Esempio n. 3
0
        private void AdoptMissingResourcesFromDefaultLanguage()
        {
            LanguageConfiguration defaultLanguageConfiguration = GetDefaultLanguageConfiguration();

            if (defaultLanguageConfiguration != null)
            {
                foreach (Resource resource in defaultLanguageConfiguration.Resources)
                {
                    if (resource != null && !strings.ContainsKey(resource.Name))
                    {
                        AddResource(resource);
                        if (LOG.IsWarnEnabled)
                        {
                            LOG.Warn("Adopted missing string resource from default language: " + resource.Name);
                        }
                    }
                }
            }
            else
            {
                LOG.Warn("Default language file is missing! The default language file is: " + DEFAULT_LANGUAGE);
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Set language
        /// </summary>
        /// <param name="wantedIETF">wanted IETF</param>
        /// <returns>Actuall IETF </returns>
        public string SetLanguage(string wantedIETF)
        {
            LOG.Debug("SetLanguage called for : " + wantedIETF);
            Dictionary <string, LanguageConfiguration> identifiedLanguages = new Dictionary <string, LanguageConfiguration>();

            if (languages == null || languages.Count == 0)
            {
                throw new FileNotFoundException("No language files found!");
            }

            // Find selected languages in available languages
            foreach (LanguageConfiguration language in languages)
            {
                LOG.Debug("Found language: " + language.Ietf);
                if (!identifiedLanguages.ContainsKey(language.Ietf))
                {
                    identifiedLanguages.Add(language.Ietf, language);
                }
                else
                {
                    LOG.WarnFormat("Found double language file: {0}", language.File);
                }
            }

            LanguageConfiguration selectedLanguage = null;

            try {
                selectedLanguage = identifiedLanguages[wantedIETF];
            } catch (KeyNotFoundException) {
                LOG.Warn("Selecteded language " + wantedIETF + " not found.");
            }

            // Make best match for language (e.g. en -> "en-US")
            if (selectedLanguage == null)
            {
                foreach (string ietf in identifiedLanguages.Keys)
                {
                    if (ietf.StartsWith(wantedIETF))
                    {
                        try {
                            selectedLanguage = identifiedLanguages[ietf];
                            LOG.Info("Selecteded language " + ietf + " by near match for: " + wantedIETF);
                            wantedIETF = ietf;
                            break;
                        } catch (KeyNotFoundException) {
                            LOG.Warn("Selecteded language " + wantedIETF + " not found.");
                        }
                    }
                }
            }

            if (selectedLanguage == null && !DEFAULT_LANGUAGE.Equals(wantedIETF))
            {
                try {
                    selectedLanguage = identifiedLanguages[DEFAULT_LANGUAGE];
                } catch (KeyNotFoundException) {
                    LOG.Warn("No english language file found!!");
                }
            }
            if (selectedLanguage == null)
            {
                // Select first (maybe only) language!
                selectedLanguage = languages[0];
                LOG.Warn("Selected " + selectedLanguage.Ietf + " as fallback language!");
            }

            // build directionary for the strings
            strings.Clear();
            foreach (Resource resource in selectedLanguage.Resources)
            {
                AddResource(resource);
            }

            currentIETF = selectedLanguage.Ietf;
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(currentIETF);

            return(currentIETF);
        }
Esempio n. 5
0
        /// <summary>
        /// loads a language configuration from a file path
        /// </summary>
        public static LanguageConfiguration Load(string path)
        {
            LanguageConfiguration ret = null;
            try {
                XmlDocument doc = new XmlDocument();
                doc.Load(path);
                XmlNodeList nodes = doc.GetElementsByTagName("language");
                if(nodes.Count > 0) {
                    ret = new LanguageConfiguration();
                    ret.File = path;
                    XmlNode node = nodes.Item(0);
                    ret.Description = node.Attributes["description"].Value;
                    ret.Ietf = node.Attributes["ietf"].Value;
                    ret.Version = node.Attributes["version"].Value;
                    if (node.Attributes["languagegroup"] != null) {
                        string languageGroup = node.Attributes["languagegroup"].Value;
                        ret.LanguageGroup = languageGroup.ToLower();
                    }

                    XmlNodeList resourceNodes = doc.GetElementsByTagName("resource");
                    ret.Resources = new List<Resource>(resourceNodes.Count);
                    foreach(XmlNode resourceNode in resourceNodes) {
                        Resource res = new Resource();
                        res.Name = resourceNode.Attributes["name"].Value;
                        res.Text = resourceNode.InnerText;
                        ret.Resources.Add(res);
                    }
                } else {
                    throw new XmlException("Root element <language> is missing");
                }
            } catch(Exception e) {
                LOG.Error("Could not load language file "+path, e);
            }
            return ret;
        }