Exemplo n.º 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);
        }
Exemplo n.º 2
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");
        }