Example #1
0
        /// <summary>
        /// Creates an I18n HTML template and translation file (PO file) from an HTML page
        /// Returns true if the process was successful and false otherwise
        /// </summary>
        /// <param name="htmlPath">THe HTML file path</param>
        /// <param name="outputName">Output folder name (Does not include path at the moment)</param>
        /// <param name="sourceLanguage">Current language used in the HTML file</param>
        /// <param name="targetLanguage">The desired translation language</param>
        /// <returns>Returns true if the process was successful and false otherwise</returns>
        public bool CreateI18nFromHTML(string htmlPath, string outputName, string sourceLanguage, string targetLanguage)
        {
            var htmlFileName    = Path.GetFileNameWithoutExtension(htmlPath);
            var uniqueIdsResult = _htmlParser.GenerateUniqueIds(htmlPath);

            if (uniqueIdsResult == null)
            {
                Console.WriteLine($"Failed to parse HTML in path '{htmlPath}. Check log for additional infromation");
                return(false);
            }

            Console.WriteLine($"Creating a translation (PO) file with name '{htmlFileName}' for language '{sourceLanguage}'..");
            var poFileResult = _poFileService.Create(Encoding.UTF8, uniqueIdsResult.HTMLOriginalValues, sourceLanguage, htmlFileName);

            if (!poFileResult)
            {
                Console.WriteLine($"Failed to create PO file '{htmlFileName}' with language '{sourceLanguage}. Check log for additional information");
            }

            Console.WriteLine($"Creating a translation (POT) file with name '{htmlFileName}' for language '{targetLanguage}'..");
            var potFileResult = _potFileService.Create(Encoding.UTF8, uniqueIdsResult.HTMLTextKeys, targetLanguage, htmlFileName);

            if (!potFileResult)
            {
                Console.WriteLine($"Failed to create POT file '{htmlFileName}' with language '{targetLanguage}. Check log for additional information");
            }

            var outputPath           = $"{_fileWrapper.AssemblyPath()}\\{outputName}\\{htmlFileName}";
            var directoyCreateResult = _directoryWrapper.Create(outputPath);

            if (directoyCreateResult != null)
            {
                Console.WriteLine($"Failed to create a directory in path '{outputName}'. Check log for additional information");
                return(false);
            }

            var fullFilePath     = _fileWrapper.CreatePathFromAssemblyPath($"{htmlFileName}_i18n", ParserFileTypes.HTML, $"{outputName}\\{htmlFileName}");
            var fileCreateResult = _fileWrapper.Create(Encoding.UTF8, uniqueIdsResult.UpdatedHTML, fullFilePath);

            if (fileCreateResult != null)
            {
                Console.WriteLine($"Failed to create file in path '{fullFilePath}'. Check log for additional information");
                return(false);
            }

            return(true);
        }
Example #2
0
        /// <summary>
        /// Скачивание файла с FTP-сервера.
        /// </summary>
        /// <param name="path">Каталог на FTP-сервере</param>
        /// <param name="fileName">Имя скачиваемого файла</param>
        /// <param name="downloadToPath">Путь для сохранения скаченного файла</param>
        public async Task DownloadFile(string path, string fileName, string downloadToPath)
        {
            path.AssertArgumentNotNull(nameof(path));

            if (string.IsNullOrEmpty(downloadToPath))
            {
                throw new InvalidOperationException("Не указан путь для загрузки файла");
            }

            using var ftpResponse = await InvokeFtpRequest(WebRequestMethods.Ftp.DownloadFile, path + fileName);

            using var ftpStream = ftpResponse.GetResponseStream() ?? throw new Exception("не удалось скачать файл");

            using var fileStream = _file.Create(downloadToPath + fileName);

            ftpStream.CopyTo(fileStream);
        }
Example #3
0
        /// <summary>
        /// Creates a translated HTML from using an i18n HTML template, from a selected source language to target language.
        /// If a translation file cannot be found locally (PO file), the HTML file will be translated
        /// using those translations, otherwise the translation will be made using Google Translate.
        /// In case Google Translate will be used, additionaly a translation file (PO file) will be created
        /// for future use.
        /// </summary>
        /// <param name="i18nHTMLFilePath">The i18n HTML file path</param>
        /// <param name="outputName">Output folder name (Does not include path at the moment)</param>
        /// <param name="sourceHTMLFileName">The source HTML file name</param>
        /// <param name="sourceLanguage">Current language used in the HTML file</param>
        /// <param name="targetLanguage">The desired translation language</param>
        public void CreateTranslatedHTMLFromI18n(string i18nHTMLFilePath, string outputName, string sourceHTMLFileName, string sourceLanguage, string targetLanguage)
        {
            var poFullFilePath = _fileWrapper.CreatePathFromAssemblyPath($"{sourceHTMLFileName}_{targetLanguage}", GetTextFileTypes.PO, $"{outputName}\\{sourceHTMLFileName}\\");

            string updatedHTML;

            if (_fileWrapper.Exists(poFullFilePath))
            {
                var poContent = _poFileService.Parse(poFullFilePath);
                if (poContent == null)
                {
                    Console.WriteLine($"Failed to parse PO file in path '{poFullFilePath}' with language '{targetLanguage}'. Check log for additional information");
                    return;
                }

                updatedHTML = _htmlParser.UpdateHTML(i18nHTMLFilePath, poContent);
            }
            else
            {
                poFullFilePath = _fileWrapper.CreatePathFromAssemblyPath($"{sourceHTMLFileName}_{sourceLanguage}", GetTextFileTypes.PO, $"{outputName}\\{sourceHTMLFileName}\\");

                Console.WriteLine($"Parsing translations from a translation (PO) file in path '{poFullFilePath}' with language '{targetLanguage}'..");
                var poContent       = _poFileService.Parse(poFullFilePath);
                var translatedTexts = _googleTranslateProvider.TranslateTexts(sourceLanguage, targetLanguage, poContent);

                Console.WriteLine($"Creating a translation (PO) file with name '{sourceHTMLFileName}' for language '{targetLanguage}'..");
                var poFileResult = _poFileService.Create(Encoding.UTF8, translatedTexts, targetLanguage, sourceHTMLFileName);
                if (!poFileResult)
                {
                    Console.WriteLine($"Failed to create POT file '{sourceHTMLFileName}' with language '{sourceLanguage}. Check log for additional information");
                }

                updatedHTML = _htmlParser.UpdateHTML(i18nHTMLFilePath, translatedTexts);
            }

            var htmlFullFilePath = _fileWrapper.CreatePathFromAssemblyPath($"{sourceHTMLFileName}_{targetLanguage}", ParserFileTypes.HTML, $"{outputName}\\{sourceHTMLFileName}\\");
            var fileCreateResult = _fileWrapper.Create(Encoding.UTF8, updatedHTML, htmlFullFilePath);

            if (fileCreateResult != null)
            {
                _logger.LogError($"Failed to create file '{sourceHTMLFileName}_{targetLanguage} in path '{htmlFullFilePath}'");
            }

            Console.WriteLine("Finished translating the HTML page");
        }
Example #4
0
        public void Download(string path)
        {
            if (path == null)
            {
                throw new ArgumentNullException("path");
            }

            var drNuRtmpStream = _drNuRtmpStreamFactory.CreateDrNuRtmpStream(RtmpUri);

            drNuRtmpStream.Duration += (sender, args) => OnDuration(args);
            drNuRtmpStream.Elapsed  += (sender, args) => OnElapsed(args);

            using (var flvReader = new FlvReader(drNuRtmpStream))
                using (var flvWriter = new FlvWriter(_fileWrapper.Create(path)))
                {
                    IFlvPart flvPart;
                    while ((flvPart = flvReader.Read()) != null)
                    {
                        flvWriter.Write(flvPart);
                    }
                }
        }
Example #5
0
        /// <summary>
        /// Creates a PO (translation) file from a set of translation keys for a specific encoding and language
        /// Returns true if created succesfully and false otherwise
        /// </summary>
        /// <param name="fileEncoding">File encoding</param>
        /// <param name="htmlTextTranslations">Translations dictionary</param>
        /// <param name="language">Language used for the translations</param>
        /// <param name="outputFileName">Output file name</param>
        /// <returns>Returns true if created succesfully and false otherwise</returns>
        public bool Create(Encoding fileEncoding, IOrderedDictionary <string, string> htmlTextTranslations, string language, string outputFileName)
        {
            var catalog   = _catalog.CreateCatalog(fileEncoding, htmlTextTranslations, language);
            var generator = new POGenerator(new POGeneratorSettings()
            {
                IgnoreEncoding = true
            });
            var stringBuilder = new StringBuilder();

            var outputPath           = $"{_fileWrapper.AssemblyPath()}\\output\\{outputFileName}\\";
            var directoyCreateResult = _directoryWrapper.Create(outputPath);

            if (directoyCreateResult != null)
            {
                return(false);
            }

            try
            {
                generator.Generate(stringBuilder, catalog);
            }

            catch (Exception ex)
            {
                _logger.LogError(ex, $"Failed to create PO file {outputFileName}' with language '{language}'");
                return(false);
            }

            var fullFilePath     = $"{outputPath}\\{outputFileName}_{language}.{GetTextFileTypes.PO}";
            var fileCreateResult = _fileWrapper.Create(fileEncoding, stringBuilder, fullFilePath);

            if (fileCreateResult != null)
            {
                return(false);
            }

            return(true);
        }