public static ResourceContainer ReadFile(string path) { ResxXmlReader reader = new ResxXmlReader(); if (reader.TryRead(path, out ResourceContainer cont)) { return(cont); } return(null); }
DataItem[] GetItems(string type, string locale) { ResxXmlReader reader = new ResxXmlReader(); string resPath = Path.Combine(_paths.LocalizationRoot, "Localization", type + "." + locale + ".resx"); if (reader.TryRead(resPath, out ResourceContainer cont1)) { return(cont1.DataItems ?? new DataItem[0]); } return(new DataItem[0]); }
protected void SaveFile(string type, string lang, DataItem[] lst) { string resLang1 = Path.Combine(_paths.LocalizationRoot, "Localization", type + "." + lang + ".resx"); ResxXmlReader reader = new ResxXmlReader(); var headers1 = new ResHeaderItem[0]; if (reader.TryRead(resLang1, out ResourceContainer cont1)) { headers1 = cont1.Headers; } reader.Save(resLang1, new ResourceContainer { DataItems = lst, Headers = headers1 }); }
public static string ResourceToJson(string resourceFile) { ResxXmlReader reader = new ResxXmlReader(); Dictionary <string, string> dic = new Dictionary <string, string>(); if (reader.TryRead(resourceFile, out ResourceContainer cont)) { if (cont.DataItems != null) { foreach (var s in cont.DataItems) { dic[s.Name] = s.Value; } } } return(DictionaryToJson(dic)); }
public virtual void Import(string type, string lang, List <DataItem> strs, bool suspendOut = false) { string resLang1 = Path.Combine(_paths.LocalizationRoot, "Localization", type + "." + lang + ".resx"); ResxXmlReader reader = new ResxXmlReader(); var data1 = new List <DataItem>(); var headers1 = new ResHeaderItem[0]; if (reader.TryRead(resLang1, out ResourceContainer cont1)) { cont1.DataItems = cont1.DataItems ?? new DataItem[0]; if (!suspendOut) { Out.WriteLine("Found " + type + "." + lang + ".resx with " + cont1.DataItems.Length + " items"); } headers1 = cont1.Headers; data1 = new List <DataItem>(); data1.AddRange(cont1.DataItems); } foreach (var item in strs) { if (!string.IsNullOrEmpty(item.Name)) { var ex = data1.FirstOrDefault(d => d.Name == item.Name); if (ex == null) { data1.Add(new DataItem { Name = item.Name.Trim(), Value = string.IsNullOrEmpty(item.Value) ? "" : item.Value.Trim(), Space = "preserve" }); } else if (!string.IsNullOrEmpty(item.Value)) { ex.Value = item.Value; } } } reader.Save(resLang1, new ResourceContainer { DataItems = data1.ToArray(), Headers = headers1 }); }
public virtual LoadResult <CustomText> LoadForTenant(CustomTextRequest req, LoadOptions opts) { string resLang1 = Path.Combine(_paths.LocalizationRoot, "Localization", ((TextTypes)req.Type).ToString() + "." + req.Locale + ".resx"); ResxXmlReader reader = new ResxXmlReader(); var res = new LoadResult <CustomText>(); if (reader.TryRead(resLang1, out ResourceContainer cont1)) { var items = cont1.DataItems.ToList(); if (!string.IsNullOrEmpty(opts.SearchTerm)) { items = items.Where(e => e.Name.ToLower().Contains(opts.SearchTerm.ToLower()) || e.Value.ToLower().Contains(opts.SearchTerm.ToLower())).ToList(); } res.TotalCount = items.Count(); IEnumerable <DataItem> q; if (opts.Showing > 0) { q = items.Skip(opts.Skip).Take(opts.Showing); } else { q = items; } res.List = q.Select(e => new CustomText { Code = e.Name, Value = e.Value, TenantId = req.TenantId, State = "Detached", Locale = req.Locale, Type = req.Type }).ToList(); } return(res); }
public virtual void SyncLanguages(string lang1, string lang2) { using (var x = SW.Measure()) { string[] types = new string[] { "Columns", "Words", "Pages", "Messages" }; foreach (string type in types) { string resLang1 = Path.Combine(_paths.LocalizationRoot, "Localization", type + "." + lang1 + ".resx"); string resLang2 = Path.Combine(_paths.LocalizationRoot, "Localization", type + "." + lang2 + ".resx"); ResxXmlReader reader = new ResxXmlReader(); var data1 = new List <DataItem>(); var data2 = new List <DataItem>(); var headers1 = new ResHeaderItem[0]; var headers2 = new ResHeaderItem[0]; if (reader.TryRead(resLang1, out ResourceContainer cont1)) { cont1.DataItems = cont1.DataItems ?? new DataItem[0]; Out.WriteLine("Found " + type + "." + lang1 + ".resx with " + cont1.DataItems.Length + " items"); headers1 = cont1.Headers; data1 = new List <DataItem>(); data1.AddRange(cont1.DataItems); } if (reader.TryRead(resLang2, out ResourceContainer cont2)) { cont2.DataItems = cont2.DataItems ?? new DataItem[0]; Out.WriteLine("Found " + type + "." + lang2 + ".resx with " + cont2.DataItems.Length + " items"); headers2 = cont2.Headers; data2 = new List <DataItem>(); data2.AddRange(cont2.DataItems); } int i = 0; foreach (var item in data1) { if (!data2.Any(d => d.Name == item.Name)) { data2.Add(new DataItem { Name = item.Name, Value = "", Space = item.Space }); i++; } } Out.WriteLine($"{lang1} --> {lang2} : Added {i} Entries.."); i = 0; foreach (var item in data2) { if (!data1.Any(d => d.Name == item.Name)) { data1.Add(new DataItem { Name = item.Name, Value = type == "Messages" ? LangUtils.IdToPhrase(item.Name) : "", Space = item.Space }); i++; } } Out.WriteLine($"{lang2} --> {lang1} : Added {i} Entries.."); reader.Save(resLang1, new ResourceContainer { DataItems = data1.ToArray(), Headers = headers1 }); reader.Save(resLang2, new ResourceContainer { DataItems = data2.ToArray(), Headers = headers2 }); } WriteSuccess(x.Elapsed); } }