Exemplo n.º 1
0
        public Lang AddLang(string name)
        {
            if (Langs.Any(l => l.Name.ToLower() == name.ToLower()))
            {
                return(Langs.FirstOrDefault(l => l.Name.ToLower() == name.ToLower()));
            }

            Langs.Add(new Lang {
                Id = Langs.Count != 0 ? Langs.Last().Id + 1 : 1, Name = name
            });
            return(Langs.Last());
        }
Exemplo n.º 2
0
        public void RemoveLang(Lang lang)
        {
            Langs.Remove(lang);

            if (CurrentLang == lang)
            {
                CurrentLang = Langs.FirstOrDefault();
            }

            foreach (Word word in Words)
            {
                word.Values?.Remove(lang.Id);
            }
        }
Exemplo n.º 3
0
        public void SaveAsXmlResource(string langFolder)
        {
            try
            {
                if (Directory.Exists(langFolder))
                {
                    Directory.Delete(langFolder, true);
                }

                Directory.CreateDirectory(langFolder);

                if (CurrentLang == null)
                {
                    CurrentLang = Langs.FirstOrDefault();
                }

                SerializerHelper.SaveText(CurrentLang.Id.ToString(), Path.Combine(langFolder, CurrentLangFile));

                foreach (Lang lang in Langs)
                {
                    string path = Path.Combine(langFolder, $"{lang.Id}.{lang.Name}");
                    Directory.CreateDirectory(path);

                    string text = "";
                    foreach (Word word in Words)
                    {
                        text += $"<{word.Key}>{word.Values[lang.Id]}</{word.Key}>{Environment.NewLine}";
                    }

                    SerializerHelper.SaveText(text, Path.Combine(path, WordsFile));
                }
            }
            catch (Exception ex)
            {
                Logger.Log(ex);
            }
        }
Exemplo n.º 4
0
        public bool LoadFromXmlResource(string langFolder)
        {
            try
            {
                if (!Directory.Exists(langFolder))
                {
                    return(false);
                }

                if (int.TryParse(SerializerHelper.LoadText(Path.Combine(langFolder, CurrentLangFile)), out int landId))
                {
                    Langs.Clear();
                    Words.Clear();

                    foreach (string lang in Directory.GetDirectories(langFolder))
                    {
                        string name = new DirectoryInfo(lang).Name;

                        if (int.TryParse(name.Substring(0, name.IndexOf('.')), out int id))
                        {
                            name = name.Substring(name.IndexOf('.') + 1);

                            Langs.Add(new Lang
                            {
                                Id   = id,
                                Name = name
                            });

                            string text = SerializerHelper.LoadText(Path.Combine(lang, WordsFile));

                            while (!string.IsNullOrEmpty(text))
                            {
                                if (text[0] == '<')
                                {
                                    string key   = text.Substring(1, text.IndexOf('>') - 1);
                                    string value = text.Substring(text.IndexOf('>') + 1, text.IndexOf($"</{key}>") - $"</{key}>".Length + 1);

                                    Word word = Words.FirstOrDefault(w => w.Key == key);

                                    if (word == null)
                                    {
                                        word = new Word
                                        {
                                            Key    = key,
                                            Values = new Dictionary <int, string>()
                                        };

                                        Words.Add(word);
                                    }

                                    if (word.Values.ContainsKey(id))
                                    {
                                        word.Values[id] = value;
                                    }
                                    else
                                    {
                                        word.Values.Add(id, value);
                                    }

                                    text = text.Substring(key.Length * 2 + value.Length + 5).TrimStart('\r', '\n');
                                }
                            }
                        }
                    }

                    CurrentLang = Langs.FirstOrDefault(l => l.Id == landId) ?? Langs.FirstOrDefault();

                    return(true);
                }
            }
            catch (Exception ex)
            {
                Logger.Log(ex);
            }

            return(false);
        }
Exemplo n.º 5
0
 public void SetValue(string wordKey, string langName, string value)
 => SetValue(
     Words.FirstOrDefault(w => w.Key.ToLower() == wordKey.ToLower()),
     Langs.FirstOrDefault(l => l.Name.ToLower() == langName.ToLower()),
     value);