Пример #1
0
 public ProgramScope(ResourceData resourceData, IConfiguration configuration)
 {
     ResourceData  = resourceData;
     Configuration = configuration;
 }
        public static void Export(string project, string module, string fName, string language, string exportPath, string key = null)
        {
            var filter = new ResCurrent
            {
                Project = new ResProject {
                    Name = project
                },
                Module = new ResModule {
                    Name = module
                },
                Language = new ResCulture {
                    Title = language
                },
                Word = new ResWord()
                {
                    ResFile = new ResFile()
                    {
                        FileName = fName
                    }
                }
            };

            var words = ResourceData.GetListResWords(filter, string.Empty).GroupBy(x => x.ResFile.FileID).ToList();

            if (!words.Any())
            {
                Console.WriteLine("Error!!! Can't find appropriate project and module. Possibly wrong names!");
                return;
            }

            foreach (var fileWords in words)
            {
                var wordsDictionary = new Dictionary <string, object>();
                var firstWord       = fileWords.FirstOrDefault();
                var fileName        = firstWord == null
                    ? module
                    : Path.GetFileNameWithoutExtension(firstWord.ResFile.FileName);
                var zipFileName = Path.Combine(exportPath, language == "Neutral" ? "en" : language, $"{fileName}.json");

                var dirName = Path.GetDirectoryName(zipFileName);
                if (!Directory.Exists(dirName))
                {
                    Directory.CreateDirectory(dirName);
                }

                var toAdd = new List <ResWord>();
                if (!string.IsNullOrEmpty(key))
                {
                    if (File.Exists(zipFileName))
                    {
                        var jObject = JObject.Parse(File.ReadAllText(zipFileName));
                        foreach (var j in jObject)
                        {
                            toAdd.Add(new ResWord {
                                Title = j.Key, ValueFrom = j.Value.ToString()
                            });
                        }
                    }

                    if (!toAdd.Any(r => r.Title == key))
                    {
                        toAdd.Add(fileWords.FirstOrDefault(r => r.Title == key));
                    }
                }
                else
                {
                    toAdd.AddRange(fileWords.OrderBy(x => x.Title).Where(word => !wordsDictionary.ContainsKey(word.Title)));
                }

                foreach (var word in toAdd.Where(r => r != null))
                {
                    if (string.IsNullOrEmpty(word.ValueTo))
                    {
                        continue;
                    }

                    var newVal = word.ValueTo ?? word.ValueFrom;

                    if (!string.IsNullOrEmpty(newVal))
                    {
                        newVal = newVal.TrimEnd('\n').TrimEnd('\r');
                    }

                    var newKey = GetKey(word.Title, newVal);
                    wordsDictionary.Add(newKey.Keys.First(), newKey.Values.First());
                }

                using TextWriter writer = new StreamWriter(zipFileName);

                var obj = JsonConvert.SerializeObject(wordsDictionary, Formatting.Indented);
                writer.Write(obj);
            }
        }
Пример #3
0
        public static void Upload(IOptionsMonitor <ILog> option, ResourceData resourceData, string fileName, Stream fileStream, string projectName, string moduleName)
        {
            var culture = GetCultureFromFileName(fileName);

            string jsonString;

            using (var reader = new StreamReader(fileStream))
            {
                jsonString = reader.ReadToEnd();
            }

            var jsonObj = new Dictionary <string, string>();

            if (Path.GetExtension(fileName) == ".xml")
            {
                var doc = new XmlDocument();
                doc.LoadXml(jsonString);
                var list = doc.SelectNodes("//resources//string");
                if (list != null)
                {
                    try
                    {
                        var nodes = list.Cast <XmlNode>().ToList();
                        jsonObj = nodes.ToDictionary(r => r.Attributes["name"].Value, r => r.InnerText);
                    }
                    catch (Exception e)
                    {
                        option.CurrentValue.ErrorFormat("parse xml " + fileName, e);
                    }
                }
            }
            else
            {
                jsonObj = JsonSerializer.Deserialize <Dictionary <string, string> >(jsonString);
            }

            var          fileID       = resourceData.AddFile(fileName, projectName, moduleName);
            const string resourceType = "text";

            foreach (var key in jsonObj.Keys)
            {
                var word = new ResWord
                {
                    Title     = key,
                    ValueFrom = jsonObj[key],
                    ResFile   = new ResFile {
                        FileID = fileID
                    }
                };
                if (culture != "Neutral")
                {
                    var neutralKey = new ResWord
                    {
                        Title     = key,
                        ValueFrom = jsonObj[key],
                        ResFile   = new ResFile {
                            FileID = fileID
                        }
                    };

                    resourceData.GetValueByKey(neutralKey, "Neutral");
                    if (string.IsNullOrEmpty(neutralKey.ValueTo))
                    {
                        continue;
                    }
                }

                resourceData.AddResource(culture, resourceType, DateTime.UtcNow, word, true, "Console");
            }
        }
Пример #4
0
        public static void Export(Options options)
        {
            var services = new ServiceCollection();
            var startup  = new Startup();

            startup.ConfigureServices(services);
            var serviceProvider = services.BuildServiceProvider();

            CommonServiceProvider.Init(serviceProvider);
            ConfigurationManager.Init(serviceProvider);

            var cultures        = new List <string>();
            var projects        = new List <ResFile>();
            var enabledSettings = new EnabledSettings();
            Action <string, string, string, string, string, string> export = null;

            try
            {
                var(project, module, filePath, exportPath, culture, format, key) = options;

                if (format == "json")
                {
                    export = JsonManager.Export;
                }
                else
                {
                    export = ResxManager.Export;
                }

                if (string.IsNullOrEmpty(exportPath))
                {
                    exportPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                }

                if (!Path.IsPathRooted(exportPath))
                {
                    exportPath = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), exportPath));
                }

                if (!Directory.Exists(exportPath))
                {
                    Console.WriteLine("Error!!! Export path doesn't exist! Please enter a valid directory path.");
                    return;
                }

                enabledSettings = ConfigurationManager.GetSetting <EnabledSettings>("enabled");
                cultures        = ResourceData.GetCultures().Where(r => r.Available).Select(r => r.Title).Intersect(enabledSettings.Langs).ToList();
                projects        = ResourceData.GetAllFiles();

                ExportWithProject(project, module, filePath, culture, exportPath, key);

                Console.WriteLine("The data has been successfully exported!");
            }
            catch (Exception err)
            {
                Console.WriteLine(err);
            }

            void ExportWithProject(string projectName, string moduleName, string fileName, string culture, string exportPath, string key = null)
            {
                if (!string.IsNullOrEmpty(projectName))
                {
                    ExportWithModule(projectName, moduleName, fileName, culture, exportPath, key);
                }
                else
                {
                    var projectToExport = projects
                                          .Where(r => string.IsNullOrEmpty(r.ModuleName) || r.ModuleName == moduleName)
                                          .Where(r => string.IsNullOrEmpty(r.FileName) || r.FileName == fileName)
                                          .Select(r => r.ProjectName)
                                          .Intersect(enabledSettings.Projects);

                    foreach (var p in projectToExport)
                    {
                        ExportWithModule(p, moduleName, fileName, culture, exportPath, key);
                    }
                }
            }

            void ExportWithModule(string projectName, string moduleName, string fileName, string culture, string exportPath, string key = null)
            {
                if (!string.IsNullOrEmpty(moduleName))
                {
                    ExportWithFile(projectName, moduleName, fileName, culture, exportPath, key);
                }
                else
                {
                    var moduleToExport = projects
                                         .Where(r => r.ProjectName == projectName)
                                         .Where(r => string.IsNullOrEmpty(r.FileName) || r.FileName == fileName)
                                         .Select(r => r.ModuleName);

                    foreach (var m in moduleToExport)
                    {
                        ExportWithFile(projectName, m, fileName, culture, exportPath, key);
                    }
                }
            }

            void ExportWithFile(string projectName, string moduleName, string fileName, string culture, string exportPath, string key = null)
            {
                if (!string.IsNullOrEmpty(fileName))
                {
                    ExportWithCulture(projectName, moduleName, fileName, culture, exportPath, key);
                }
                else
                {
                    foreach (var f in projects.Where(r => r.ProjectName == projectName && r.ModuleName == moduleName).Select(r => r.FileName))
                    {
                        ExportWithCulture(projectName, moduleName, f, culture, exportPath, key);
                    }
                }
            }

            void ExportWithCulture(string projectName, string moduleName, string fileName, string culture, string exportPath, string key)
            {
                if (!string.IsNullOrEmpty(culture))
                {
                    export(projectName, moduleName, fileName, culture, exportPath, key);
                }
                else
                {
                    ParallelEnumerable.ForAll(cultures.AsParallel(), c => export(projectName, moduleName, fileName, c, exportPath, key));
                }
            }
        }