Esempio 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);
        }
Esempio n. 2
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);
        }