Beispiel #1
0
 /// <summary>
 /// Load the resources from the language file
 /// </summary>
 /// <param name="languageFile">File to load from</param>
 private static void LoadResources(LanguageFile languageFile)
 {
     LOG.InfoFormat("Loading language file {0}", languageFile.Filepath);
     try {
         XmlDocument xmlDocument = new XmlDocument();
         xmlDocument.Load(languageFile.Filepath);
         XmlNodeList resourceNodes = xmlDocument.GetElementsByTagName("resource");
         foreach (XmlNode resourceNode in resourceNodes)
         {
             string key = resourceNode.Attributes["name"].Value;
             if (!string.IsNullOrEmpty(languageFile.Prefix))
             {
                 key = languageFile.Prefix + "." + key;
             }
             string text = resourceNode.InnerText;
             if (!string.IsNullOrEmpty(text))
             {
                 text = text.Trim();
             }
             if (!resources.ContainsKey(key))
             {
                 resources.Add(key, text);
             }
             else
             {
                 resources[key] = text;
             }
         }
     } catch (Exception e) {
         LOG.Error("Could not load language file " + languageFile.Filepath, e);
     }
 }
Beispiel #2
0
 /// <summary>
 /// Load the language file information
 /// </summary>
 /// <param name="languageFilePath"></param>
 /// <returns></returns>
 private static LanguageFile LoadFileInfo(string languageFilePath)
 {
     try
     {
         XmlDocument xmlDocument = new XmlDocument();
         xmlDocument.Load(languageFilePath);
         XmlNodeList nodes = xmlDocument.GetElementsByTagName("language");
         if (nodes.Count > 0)
         {
             LanguageFile languageFile = new LanguageFile();
             languageFile.Filepath = languageFilePath;
             XmlNode node = nodes.Item(0);
             languageFile.Description = node.Attributes["description"].Value;
             if (node.Attributes["ietf"] != null)
             {
                 languageFile.Ietf = ReformatIETF(node.Attributes["ietf"].Value);
             }
             if (node.Attributes["version"] != null)
             {
                 languageFile.Version = new Version(node.Attributes["version"].Value);
             }
             if (node.Attributes["prefix"] != null)
             {
                 languageFile.Prefix = node.Attributes["prefix"].Value.ToLower();
             }
             if (node.Attributes["languagegroup"] != null)
             {
                 string languageGroup = node.Attributes["languagegroup"].Value;
                 languageFile.LanguageGroup = languageGroup.ToLower();
             }
             return(languageFile);
         }
         else
         {
             throw new XmlException("Root element <language> is missing");
         }
     }
     catch (Exception e)
     {
         LOG.Error("Could not load language file " + languageFilePath, e);
     }
     return(null);
 }
Beispiel #3
0
        /// <summary>
        /// Scan the files in all directories
        /// </summary>
        private static void ScanFiles()
        {
            languageFiles.Clear();
            helpFiles.Clear();
            foreach (string languagePath in languagePaths)
            {
                if (!Directory.Exists(languagePath))
                {
                    LOG.InfoFormat("Skipping non existing language path {0}", languagePath);
                    continue;
                }
                LOG.InfoFormat("Searching language directory '{0}' for language files with pattern '{1}'", languagePath, LANGUAGE_FILENAME_PATTERN);
                try {
                    foreach (string languageFilepath in Directory.GetFiles(languagePath, LANGUAGE_FILENAME_PATTERN, SearchOption.AllDirectories))
                    {
                        //LOG.DebugFormat("Found language file: {0}", languageFilepath);
                        LanguageFile languageFile = LoadFileInfo(languageFilepath);
                        if (languageFile == null)
                        {
                            continue;
                        }
                        if (string.IsNullOrEmpty(languageFile.Ietf))
                        {
                            LOG.WarnFormat("Fixing missing ietf in language-file {0}", languageFilepath);
                            string languageFilename = Path.GetFileName(languageFilepath);
                            if (IETF_REGEXP.IsMatch(languageFilename))
                            {
                                string replacementIETF = IETF_REGEXP.Replace(languageFilename, "$1");
                                languageFile.Ietf = ReformatIETF(replacementIETF);
                                LOG.InfoFormat("Fixed IETF to {0}", languageFile.Ietf);
                            }
                            else
                            {
                                LOG.ErrorFormat("Missing ietf , no recover possible... skipping language-file {0}!", languageFilepath);
                                continue;
                            }
                        }

                        // Check if we can display the file
                        if (!string.IsNullOrEmpty(languageFile.LanguageGroup) && unsupportedLanguageGroups.Contains(languageFile.LanguageGroup))
                        {
                            LOG.InfoFormat("Skipping unsuported (not able to display) language {0} from file {1}", languageFile.Description, languageFilepath);
                            continue;
                        }

                        // build prefix, based on the filename, but only if it's not set in the file itself.
                        if (string.IsNullOrEmpty(languageFile.Prefix))
                        {
                            string languageFilename = Path.GetFileNameWithoutExtension(languageFilepath);
                            if (PREFIX_REGEXP.IsMatch(languageFilename))
                            {
                                languageFile.Prefix = PREFIX_REGEXP.Replace(languageFilename, "$1");
                                if (!string.IsNullOrEmpty(languageFile.Prefix))
                                {
                                    languageFile.Prefix = languageFile.Prefix.Replace("plugin", "").ToLower();
                                }
                            }
                        }
                        List <LanguageFile> currentFiles = null;
                        if (languageFiles.ContainsKey(languageFile.Ietf))
                        {
                            currentFiles = languageFiles[languageFile.Ietf];
                            bool needToAdd = true;
                            List <LanguageFile> deleteList = new List <LanguageFile>();
                            foreach (LanguageFile compareWithLangfile in currentFiles)
                            {
                                if ((languageFile.Prefix == null && compareWithLangfile.Prefix == null) || (languageFile.Prefix != null && languageFile.Prefix.Equals(compareWithLangfile.Prefix)))
                                {
                                    if (compareWithLangfile.Version > languageFile.Version)
                                    {
                                        LOG.WarnFormat("Skipping {0}:{1}:{2} as {3}:{4}:{5} is newer", languageFile.Filepath, languageFile.Prefix, languageFile.Version, compareWithLangfile.Filepath, compareWithLangfile.Prefix, compareWithLangfile.Version);
                                        needToAdd = false;
                                        break;
                                    }
                                    else
                                    {
                                        LOG.WarnFormat("Found {0}:{1}:{2} and deleting {3}:{4}:{5}", languageFile.Filepath, languageFile.Prefix, languageFile.Version, compareWithLangfile.Filepath, compareWithLangfile.Prefix, compareWithLangfile.Version);
                                        deleteList.Add(compareWithLangfile);
                                    }
                                }
                            }
                            if (needToAdd)
                            {
                                foreach (LanguageFile deleteFile in deleteList)
                                {
                                    currentFiles.Remove(deleteFile);
                                }
                                LOG.InfoFormat("Added language {0} from: {1}", languageFile.Description, languageFile.Filepath);
                                currentFiles.Add(languageFile);
                            }
                        }
                        else
                        {
                            currentFiles = new List <LanguageFile>();
                            currentFiles.Add(languageFile);
                            languageFiles.Add(languageFile.Ietf, currentFiles);
                            LOG.InfoFormat("Added language {0} from: {1}", languageFile.Description, languageFile.Filepath);
                        }
                    }
                } catch (DirectoryNotFoundException) {
                    LOG.InfoFormat("Non existing language directory: {0}", languagePath);
                } catch (Exception e) {
                    LOG.Error("Error trying for read directory " + languagePath, e);
                }

                // Now find the help files
                LOG.InfoFormat("Searching language directory '{0}' for help files with pattern '{1}'", languagePath, HELP_FILENAME_PATTERN);
                try {
                    foreach (string helpFilepath in Directory.GetFiles(languagePath, HELP_FILENAME_PATTERN, SearchOption.AllDirectories))
                    {
                        LOG.DebugFormat("Found help file: {0}", helpFilepath);
                        string helpFilename = Path.GetFileName(helpFilepath);
                        string ietf         = ReformatIETF(helpFilename.Replace(".html", "").Replace("help-", ""));
                        if (!helpFiles.ContainsKey(ietf))
                        {
                            helpFiles.Add(ietf, helpFilepath);
                        }
                        else
                        {
                            LOG.WarnFormat("skipping help file {0}, already a file with the same IETF {1} found!", helpFilepath, ietf);
                        }
                    }
                } catch (DirectoryNotFoundException) {
                    LOG.InfoFormat("Non existing language directory: {0}", languagePath);
                } catch (Exception e) {
                    LOG.Error("Error trying for read directory " + languagePath, e);
                }
            }
        }