Esempio n. 1
0
        private void WarningDataAdded(object?sender, System.Management.Automation.DataAddedEventArgs e)
        {
            var data = powershell.Streams.Warning[e.Index];

            consoleLogger.Warn(data.Message);
        }
Esempio n. 2
0
        void RunTranslation(TranslationCommand tCommand)
        {
            InitDiskCache();
            var           project = AddProject(PathUtils.Normalize(Environment.CurrentDirectory), false);
            TranslationDb trDb;
            var           addLanguage = tCommand.AddLang.Value;

            if (addLanguage != null)
            {
                project.InitializeTranslationDb();
                trDb        = project.TranslationDb;
                addLanguage = addLanguage.ToLowerInvariant();
                if (trDb.HasLanguage(addLanguage))
                {
                    _logger.WriteLine($"Cannot add language {addLanguage} because it already exists. Doing nothing.");
                }
                else
                {
                    _logger.WriteLine($"Adding language {addLanguage}");
                    trDb.AddLanguage(addLanguage);
                    trDb.SaveLangDb(PathToTranslations(project), addLanguage);
                    _logger.WriteLine($"Added language {addLanguage}");
                }

                return;
            }

            var removeLanguage = tCommand.RemoveLang.Value;

            if (removeLanguage != null)
            {
                project.InitializeTranslationDb();
                trDb = project.TranslationDb;
                if (!trDb.HasLanguage(removeLanguage))
                {
                    _logger.Warn($"Cannot remove language {removeLanguage} because it does not exist. Doing nothing.");
                }
                else
                {
                    _logger.WriteLine($"Removing language {removeLanguage}");
                    File.Delete(PathUtils.Join(PathToTranslations(project), $"{removeLanguage}.json"));
                    _logger.WriteLine($"Removed language {removeLanguage}");
                }

                return;
            }

            var export       = tCommand.Export.Value;
            var exportAll    = tCommand.ExportAll.Value;
            var lang         = tCommand.Lang.Value;
            var specificPath = tCommand.SpecificPath.Value;

            if (export != null || exportAll != null)
            {
                project.InitializeTranslationDb(specificPath);
                trDb = project.TranslationDb;

                if (lang != null && !trDb.HasLanguage(lang))
                {
                    _logger.Error(
                        $"You have entered unsupported language '{lang}'. Please enter one of {string.Join(',', trDb.GetLanguages())}");
                    return;
                }

                var destinationFile        = export;
                var exportOnlyUntranslated = true;

                if (exportAll != null)
                {
                    destinationFile        = exportAll;
                    exportOnlyUntranslated = false;
                }

                if (!trDb.ExportLanguages(destinationFile, exportOnlyUntranslated, lang, specificPath))
                {
                    _logger.Warn("Nothing to export. No export file created.");
                }
                else
                {
                    if (specificPath == null)
                    {
                        _logger.WriteLine(lang != null
                            ? $"Exported {(exportOnlyUntranslated ? "untranslated " : string.Empty)}language '{lang}' to {destinationFile}."
                            : $"Exported {(exportOnlyUntranslated ? "untranslated " : string.Empty)}languages to {destinationFile}.");
                    }
                    else
                    {
                        _logger.WriteLine($"Exported file from {specificPath} into file {destinationFile}");
                    }
                }

                return;
            }

            var import = tCommand.Import.Value;

            if (import != null)
            {
                project.InitializeTranslationDb(specificPath);
                trDb = project.TranslationDb;
                if (specificPath == null)
                {
                    if (!trDb.ImportTranslatedLanguage(import, specificPath))
                    {
                        _logger.Error("Import failed. See output for more information.");
                        return;
                    }

                    var importedLang = Path.GetFileNameWithoutExtension(PathUtils.Normalize(import));
                    trDb.SaveLangDb(PathToTranslations(project), importedLang);

                    _logger.WriteLine($"Translated language from file {import} successfully imported.");
                }
                else
                {
                    if (!trDb.ImportTranslatedLanguage(import, specificPath))
                    {
                        _logger.Error("Import failed. See output for more information.");
                        return;
                    }

                    var language = trDb.GetLanguageFromSpecificFile(specificPath);
                    var dir      = Path.GetDirectoryName(specificPath);
                    trDb.SaveLangDb(dir, language);

                    _logger.WriteLine(
                        $"Translated language from file {import} successfully imported to file {specificPath}.");
                }

                return;
            }

            var union = tCommand.Union.Value;

            if (union != null && union.All(x => x != null))
            {
                if (union.Length != 3)
                {
                    _logger.Error("Incorrect count of parameters.");
                    return;
                }

                project.InitializeTranslationDb();
                trDb = project.TranslationDb;

                if (trDb.UnionExportedLanguage(union[0], union[1], union[2]))
                {
                    _logger.Success($"Union of {union[0]} with {union[1]} successfully saved to {union[2]}");
                }

                return;
            }

            var subtract = tCommand.Subtract.Value;

            if (subtract != null && subtract.All(x => x != null))
            {
                if (subtract.Length != 3)
                {
                    _logger.Error("Incorrect count of parameters.");
                    return;
                }

                project.InitializeTranslationDb();
                trDb = project.TranslationDb;

                if (trDb.SubtractExportedLanguage(subtract[0], subtract[1], subtract[2]))
                {
                    _logger.Success(
                        $"Subtract of {subtract[0]} with {subtract[1]} successfully saved to {subtract[2]}");
                }

                return;
            }
        }