示例#1
0
        /// <summary>
        /// Translate a reference text in a repository context
        /// </summary>
        public string RepositoryTranslate(string culture, string context, string instance, string reference)
        {
            if (string.IsNullOrEmpty(reference))
            {
                return("");
            }

            string result = reference;

            try
            {
                var key = context + "\r" + reference + "\r" + instance;
                RepositoryTranslation myTranslation = RepositoryTranslations.ContainsKey(key) ? RepositoryTranslations[key] : null;
                if (myTranslation != null)
                {
                    if (!string.IsNullOrEmpty(culture) && myTranslation.Translations.ContainsKey(culture))
                    {
                        result = myTranslation.Translations[culture];
                        if (string.IsNullOrEmpty(result))
                        {
                            result = reference;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
            return(result);
        }
示例#2
0
        public string RepositoryTranslate(string culture, string context, string instance, string reference)
        {
            string result = reference;

            try
            {
                RepositoryTranslation myTranslation = RepositoryTranslations.FirstOrDefault(i => i.Context == context && i.Instance == instance && i.Reference == reference);
                if (myTranslation != null)
                {
                    if (!string.IsNullOrEmpty(culture) && myTranslation.Translations.ContainsKey(culture))
                    {
                        result = myTranslation.Translations[culture];
                        if (string.IsNullOrEmpty(result))
                        {
                            result = reference;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
            return(result);
        }
        static public List<RepositoryTranslation> InitFromCSV(string filePath, bool hasInstance)
        {
            List<RepositoryTranslation> translations = new List<RepositoryTranslation>();
            if (File.Exists(filePath))
            {
                bool isHeader = true;
                Regex regexp = null;
                List<string> languages = new List<string>();

                foreach (string line in File.ReadAllLines(filePath, System.Text.Encoding.Default))
                {
                    if (regexp == null)
                    {
                        string exp = "(?<=^|,)(\"(?:[^\"]|\"\")*\"|[^,]*)";
                        char separator = ',';
                        //use the first line to determine the separator
                        if (line.StartsWith("Context")) separator = line[7];
                        if (separator != ',') exp = exp.Replace(',', separator);
                        regexp = new Regex(exp);
                    }

                    MatchCollection collection = regexp.Matches(line);
                    int startCol = (hasInstance ? 3 : 2);
                    if (collection.Count > startCol)
                    {
                        if (isHeader)
                        {
                            for (int i = startCol; i < collection.Count; i++)
                            {
                                languages.Add(convert(collection[i].Value));
                            }
                            isHeader = false;
                        }
                        else
                        {
                            RepositoryTranslation translation = new RepositoryTranslation() { Context = convert(collection[0].Value), Reference = convert(collection[startCol-1].Value) };
                            if (hasInstance) translation.Instance = convert(collection[1].Value);
                            translations.Add(translation);
                            for (int i = 0; i < languages.Count && i + startCol < collection.Count; i++)
                            {
                                translation.Translations.Add(languages[i], convert(collection[i + startCol].Value));
                            }
                        }
                    }
                }
            }

            return translations;
        }
示例#4
0
        /// <summary>
        /// Translate a reference text
        /// </summary>
        public string Translate(string culture, string context, string reference)
        {
            string result = reference;

            try
            {
                var key = context + "\r" + reference;
                RepositoryTranslation myTranslation = Translations.ContainsKey(key) ? Translations[key] : null;
                if (myTranslation != null)
                {
#if DEBUG
                    myTranslation.Usage++;
#endif
                    if (!string.IsNullOrEmpty(culture) && myTranslation.Translations.ContainsKey(culture))
                    {
                        result = myTranslation.Translations[culture];
                        if (string.IsNullOrEmpty(result))
                        {
                            result = reference;
                        }
                    }
                }
                else
                {
#if DEBUG
                    if (!UnkownTranslations.Exists(i => i.Context == context && i.Reference == reference))
                    {
                        UnkownTranslations.Add(new RepositoryTranslation()
                        {
                            Context = context, Reference = reference
                        });
                    }
#endif
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
            return(result);
        }
        static private void initFromCSV(Dictionary <string, RepositoryTranslation> translations, string filePath, bool hasInstance)
        {
            if (File.Exists(filePath))
            {
                bool          isHeader  = true;
                Regex         regexp    = null;
                List <string> languages = new List <string>();

                foreach (string line in File.ReadAllLines(filePath, System.Text.Encoding.UTF8))
                {
                    if (regexp == null)
                    {
                        string exp       = "(?<=^|,)(\"(?:[^\"]|\"\")*\"|[^,]*)";
                        char   separator = ',';
                        //use the first line to determine the separator
                        if (line.StartsWith("Context"))
                        {
                            separator = line[7];
                        }
                        if (separator != ',')
                        {
                            exp = exp.Replace(',', separator);
                        }
                        regexp = new Regex(exp);
                    }

                    MatchCollection collection = regexp.Matches(line);
                    int             startCol   = (hasInstance ? 3 : 2);
                    if (collection.Count > startCol)
                    {
                        if (isHeader)
                        {
                            for (int i = startCol; i < collection.Count; i++)
                            {
                                languages.Add(ExcelHelper.FromCsv(collection[i].Value));
                            }
                            isHeader = false;
                        }
                        else
                        {
                            var context   = ExcelHelper.FromCsv(collection[0].Value);
                            var reference = ExcelHelper.FromCsv(collection[startCol - 1].Value);

                            RepositoryTranslation translation = null;
                            var key = context + "\r" + reference;
                            if (hasInstance)
                            {
                                var instance = ExcelHelper.FromCsv(collection[1].Value);
                                key += "\r" + instance;
                                if (!translations.ContainsKey(key))
                                {
                                    translation = new RepositoryTranslation()
                                    {
                                        Context = context, Reference = reference, Instance = instance
                                    };
                                    translations.Add(key, translation);
                                }
                                else
                                {
                                    translation = translations[key];
                                }
                            }
                            else
                            {
                                if (!translations.ContainsKey(key))
                                {
                                    translation = new RepositoryTranslation()
                                    {
                                        Context = context, Reference = reference
                                    };
                                    translations.Add(key, translation);
                                }
                                else
                                {
                                    translation = translations[key];
                                }
                            }

                            for (int i = 0; i < languages.Count && i + startCol < collection.Count; i++)
                            {
                                if (string.IsNullOrEmpty(languages[i]) || translation.Translations.ContainsKey(languages[i]))
                                {
                                    continue;
                                }
                                translation.Translations.Add(languages[i], ExcelHelper.FromCsv(collection[i + startCol].Value));
                            }
                        }
                    }
                }
            }
        }
        static public List <RepositoryTranslation> InitFromCSV(string filePath, bool hasInstance)
        {
            List <RepositoryTranslation> translations = new List <RepositoryTranslation>();

            if (File.Exists(filePath))
            {
                bool          isHeader  = true;
                Regex         regexp    = null;
                List <string> languages = new List <string>();

                foreach (string line in File.ReadAllLines(filePath, System.Text.Encoding.Default))
                {
                    if (regexp == null)
                    {
                        string exp       = "(?<=^|,)(\"(?:[^\"]|\"\")*\"|[^,]*)";
                        char   separator = ',';
                        //use the first line to determine the separator
                        if (line.StartsWith("Context"))
                        {
                            separator = line[7];
                        }
                        if (separator != ',')
                        {
                            exp = exp.Replace(',', separator);
                        }
                        regexp = new Regex(exp);
                    }

                    MatchCollection collection = regexp.Matches(line);
                    int             startCol   = (hasInstance ? 3 : 2);
                    if (collection.Count > startCol)
                    {
                        if (isHeader)
                        {
                            for (int i = startCol; i < collection.Count; i++)
                            {
                                languages.Add(ExcelHelper.FromCsv(collection[i].Value));
                            }
                            isHeader = false;
                        }
                        else
                        {
                            RepositoryTranslation translation = new RepositoryTranslation()
                            {
                                Context = ExcelHelper.FromCsv(collection[0].Value), Reference = ExcelHelper.FromCsv(collection[startCol - 1].Value)
                            };
                            if (hasInstance)
                            {
                                translation.Instance = ExcelHelper.FromCsv(collection[1].Value);
                            }
                            translations.Add(translation);
                            for (int i = 0; i < languages.Count && i + startCol < collection.Count; i++)
                            {
                                translation.Translations.Add(languages[i], ExcelHelper.FromCsv(collection[i + startCol].Value));
                            }
                        }
                    }
                }
            }

            return(translations);
        }