示例#1
0
        }                                                                           // debug

        // You can call this multiple times if required for debugging purposes

        public void LoadTranslation(string language, CultureInfo uicurrent,
                                    string[] txfolders, int includesearchupdepth,
                                    string logdir,
                                    string includefolderreject = "\\bin",       // use to reject include files in specific locations - for debugging
                                    bool loadorgenglish        = false,         // optional load original english and store
                                    bool loadfile = false                       // remember file where it came from
                                    )
        {
#if DEBUG
            if (logger != null)
            {
                logger.Dispose();
            }

            logger = new LogToFile();
            logger.SetFile(logdir, "translator-ids.log", false);
#endif
            translations    = null;     // forget any
            originalenglish = null;
            originalfile    = null;

            List <Tuple <string, string> > languages = EnumerateLanguages(txfolders);

            //  uicurrent = CultureInfo.CreateSpecificCulture("it"); // debug

            Tuple <string, string> langsel = null;

            if (language == "Auto")
            {
                langsel = FindISOLanguage(languages, uicurrent.Name);

                if (langsel == null)
                {
                    langsel = FindISOLanguage(languages, uicurrent.TwoLetterISOLanguageName);
                }
            }
            else
            {
                langsel = languages.Find(x => Path.GetFileNameWithoutExtension(x.Item2).Equals(language, StringComparison.InvariantCultureIgnoreCase));
            }

            if (langsel == null)
            {
                return;
            }

            System.Diagnostics.Debug.WriteLine("Load Language " + langsel.Item2);
            logger?.WriteLine("Read " + langsel.Item2 + " from " + langsel.Item1);

            using (LineReader lr = new LineReader())
            {
                string tlffile = Path.Combine(langsel.Item1, langsel.Item2);

                if (lr.Open(tlffile))
                {
                    translations    = new Dictionary <string, string>();
                    originalenglish = new Dictionary <string, string>();
                    originalfile    = new Dictionary <string, string>();

                    string prefix = "";

                    string line = null;
                    while ((line = lr.ReadLine()) != null)
                    {
                        line = line.Trim();
                        if (line.StartsWith("Include", StringComparison.InvariantCultureIgnoreCase))
                        {
                            line = line.Mid(7).Trim();

                            DirectoryInfo di = new DirectoryInfo(Path.GetDirectoryName(tlffile));

                            string filename = null;

                            string fileinroot = Path.Combine(di.FullName, line);

                            if (File.Exists(fileinroot))   // first we prefer files in the same folder..
                            {
                                filename = fileinroot;
                            }
                            else
                            {
                                di = di.GetDirectoryAbove(includesearchupdepth);        // then search the tree, first jump up search depth amount

                                try
                                {
                                    FileInfo[] allFiles = Directory.EnumerateFiles(di.FullName, line, SearchOption.AllDirectories).Select(f => new FileInfo(f)).OrderBy(p => p.LastWriteTime).ToArray();

                                    if (allFiles.Length > 0)
                                    {
                                        var selected = allFiles.Where((x) => !x.DirectoryName.Contains(includefolderreject));       // reject folders with this pattern..files
                                        if (selected.Count() > 0)
                                        {
                                            filename = selected.First().FullName;
                                        }
                                    }
                                }
                                catch { }
                            }

                            if (filename == null || !lr.Open(filename))     // if no file found, or can't open..
                            {
                                logger?.WriteLine(string.Format("*** Cannot include {0}", line));
                            }
                            else
                            {
                                logger?.WriteLine("Read " + filename);
                            }
                        }
                        else if (line.Length > 0 && !line.StartsWith("//"))
                        {
                            StringParser s = new StringParser(line);

                            string id = s.NextWord(" :");

                            if (id.Equals("SECTION"))
                            {
                                prefix = s.NextQuotedWord(" /");
                            }
                            else
                            {
                                if (id.StartsWith(".") && prefix.HasChars())
                                {
                                    id = prefix + id;
                                }
                                else
                                {
                                    prefix = id.Word(new char[] { '.' });
                                }

                                if (s.IsCharMoveOn(':'))
                                {
                                    string orgenglish = s.NextQuotedWord(replaceescape: true);  // first is the original english version

                                    string foreign = null;
                                    bool   err     = false;

                                    if (s.IsStringMoveOn("=>"))
                                    {
                                        foreign = s.NextQuotedWord(replaceescape: true);
                                        err     = foreign == null;
                                    }
                                    else if (s.IsCharMoveOn('@'))
                                    {
                                        foreign = null;
                                    }
                                    else
                                    {
                                        err = false;
                                    }

                                    if (err == true)
                                    {
                                        logger?.WriteLine(string.Format("*** Translator ID but no translation {0}", id));
                                        System.Diagnostics.Debug.WriteLine("*** Translator ID but no translation {0}", id);
                                    }
                                    else
                                    {
                                        if (!translations.ContainsKey(id))
                                        {
                                            //logger?.WriteLine(string.Format("New {0}: \"{1}\" => \"{2}\"", id, english, foreign));
                                            translations[id] = foreign;
                                            if (loadorgenglish)
                                            {
                                                originalenglish[id] = orgenglish;
                                            }
                                            if (loadfile)
                                            {
                                                originalfile[id] = lr.CurrentFile;
                                            }
                                        }
                                        else
                                        {
                                            logger?.WriteLine(string.Format("*** Translator Repeat {0}", id));
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
示例#2
0
        // You can call this multiple times if required for debugging purposes

        public void LoadTranslation(string language, CultureInfo uicurrent, string txfolder1, string txfolder2, int includesearchupdepth, string logdir)
        {
#if DEBUG
            if (logger != null)
            {
                logger.Dispose();
            }

            logger = new LogToFile();
            logger.SetFile(logdir, "translator-ids.log", false);
#endif
            translations = null;                                          // forget any

            List <string> languagesfolder1 = Languages(txfolder1, false); // full paths
            List <string> languagesfolder2 = Languages(txfolder2, false);

            // uicurrent = CultureInfo.CreateSpecificCulture("es"); // debug

            string langfile = null;

            if (language == "Auto")
            {
                langfile = FindLanguage(languagesfolder1, uicurrent.Name);

                if (langfile == null)
                {
                    langfile = FindLanguage(languagesfolder2, uicurrent.Name);
                }

                if (language == null)
                {
                    langfile = FindLanguage(languagesfolder1, uicurrent.TwoLetterISOLanguageName);
                }

                if (language == null)
                {
                    langfile = FindLanguage(languagesfolder2, uicurrent.TwoLetterISOLanguageName);
                }

                if (langfile == null)
                {
                    return;
                }
            }
            else
            {
                langfile = Path.Combine(txfolder1, language + ".tlf");

                if (!File.Exists(langfile))
                {
                    langfile = Path.Combine(txfolder2, language + ".tlf");

                    if (!File.Exists(langfile))
                    {
                        return;
                    }
                }
            }

            System.Diagnostics.Debug.WriteLine("Load Language " + langfile);
            logger?.WriteLine("Read " + langfile);

            using (LineReader lr = new LineReader())
            {
                if (lr.Open(langfile))
                {
                    translations = new Dictionary <string, string>();

                    string prefix = "";

                    string line = null;
                    while ((line = lr.ReadLine()) != null)
                    {
                        line = line.Trim();
                        if (line.StartsWith("Include", StringComparison.InvariantCultureIgnoreCase))
                        {
                            line = line.Mid(7).Trim();

                            DirectoryInfo di = new DirectoryInfo(Path.GetDirectoryName(langfile));

                            string filename = null;

                            if (File.Exists(Path.Combine(di.FullName, line)))   // first we prefer files in the same folder..
                            {
                                filename = Path.Combine(di.FullName, line);
                            }
                            else
                            {
                                di = di.GetDirectoryAbove(includesearchupdepth);        // then search the tree, first jump up search depth amount

                                try
                                {
                                    FileInfo[] allFiles = Directory.EnumerateFiles(di.FullName, line, SearchOption.AllDirectories).Select(f => new FileInfo(f)).OrderBy(p => p.LastWriteTime).ToArray();
                                    if (allFiles.Length == 1)
                                    {
                                        filename = allFiles[0].FullName;
                                    }
                                }
                                catch { }
                            }

                            if (filename == null || !lr.Open(filename))     // if no file found, or can't open..
                            {
                                logger?.WriteLine(string.Format("*** Cannot include {0}", line));
                            }
                            else
                            {
                                logger?.WriteLine("Read " + filename);
                            }
                        }
                        else if (line.Length > 0 && !line.StartsWith("//"))
                        {
                            StringParser s = new StringParser(line);

                            string id = s.NextWord(" :");

                            if (id.StartsWith(".") && prefix.HasChars())
                            {
                                id = prefix + id;
                            }
                            else
                            {
                                prefix = id.Word(new char[] { '.' });
                            }

                            if (s.IsCharMoveOn(':'))
                            {
                                s.NextQuotedWord(replaceescape: true);  // ignore the english for ref purposes only

                                string foreign = null;
                                bool   err     = false;

                                if (s.IsStringMoveOn("=>"))
                                {
                                    foreign = s.NextQuotedWord(replaceescape: true);
                                    err     = foreign == null;
                                }
                                else if (s.IsCharMoveOn('@'))
                                {
                                    foreign = null;
                                }
                                else
                                {
                                    err = false;
                                }

                                if (err == true)
                                {
                                    logger?.WriteLine(string.Format("*** Translator ID but no translation {0}", id));
                                    System.Diagnostics.Debug.WriteLine("*** Translator ID but no translation {0}", id);
                                }
                                else
                                {
                                    if (!translations.ContainsKey(id))
                                    {
                                        //logger?.WriteLine(string.Format("New {0}: \"{1}\" => \"{2}\"", id, english, foreign));
                                        translations[id] = foreign;
                                    }
                                    else
                                    {
                                        logger?.WriteLine(string.Format("*** Translator Repeat {0}", id));
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }