Exemplo n.º 1
0
        public async Task <LocalizationError> Export(
            IEnumerable <LocalizedLanguage> languages,
            IPluginConfig config)
        {
            LocalizationError result = null;
            var outputFile           = config.GetString("outputFile");

            try
            {
                using (var stream = File.OpenWrite(outputFile))
                    using (var streamWriter = new StreamWriter(stream))
                    {
                        Configuration configuration = new Configuration
                        {
                            Encoding = Encoding.UTF8
                        };

                        CsvWriter         writer         = new CsvWriter(streamWriter, configuration);
                        LocalizedLanguage masterLanguage = null;

                        writer.WriteField("Key");
                        writer.WriteField("Type");
                        writer.WriteField("Desc");
                        foreach (var language in languages)
                        {
                            if (language.IsMasterLanguage)
                            {
                                masterLanguage = language;
                            }

                            writer.WriteField(language.Name);
                        }

                        writer.NextRecord();

                        foreach (var row in masterLanguage.Rows.Values)
                        {
                            writer.WriteField(row.Key);
                            writer.WriteField("Text");
                            writer.WriteField(row.Description);
                            foreach (var language in languages)
                            {
                                writer.WriteField(language.Rows[row.Key].Value);
                            }

                            writer.NextRecord();
                        }

                        await writer.FlushAsync();
                    }
            }
            catch (Exception e)
            {
                if (e is FileNotFoundException || e is IOException)
                {
                    result = new LocalizationError(
                        LocalizationError.ErrorType.FileError,
                        string.Format("Unable to write to output file: {0}. {1}", outputFile, e.Message),
                        0);
                }
                else
                {
                    result = new LocalizationError(
                        LocalizationError.ErrorType.PluginError,
                        string.Format("Plugin failed: {0}", e.Message),
                        0);
                }
            }

            return(result);
        }
Exemplo n.º 2
0
        public async Task <LocalizationError> Export(
            IEnumerable <LocalizedLanguage> languages,
            IPluginConfig config)
        {
            LocalizationError result = null;
            var templateFile         = config.GetString("templateFile");
            var outputFile           = config.GetString("outputFile");

            string[] keysToProcess;
            string   template = string.Empty;

            try
            {
                template = File.ReadAllText(templateFile);

                List <string> args     = new List <string>();
                var           text     = template;
                int           argStart = text.IndexOf(Constants.ArgStart);
                int           argStop  = text.IndexOf(Constants.ArgStop);
                while (argStart >= 0)
                {
                    if (argStop < 0)
                    {
                        // We have a problem
                        throw new FormatException();
                    }

                    args.Add(text.Substring(argStart + Constants.ArgStart.Length, argStop - argStart - Constants.ArgStart.Length));

                    text     = text.Substring(argStop + 1);
                    argStart = text.IndexOf(Constants.ArgStart);
                    argStop  = text.IndexOf(Constants.ArgStop);
                }

                keysToProcess = args.ToArray();

                using (var stream = File.OpenWrite(outputFile))
                    using (var streamWriter = new StreamWriter(stream))
                    {
                        var writer = new StringBuilder();

                        foreach (var language in languages)
                        {
                            var templateForLanguage = template;
                            writer.AppendLine("Language: " + language.Name);
                            foreach (var key in keysToProcess)
                            {
                                ILocalizedText row;
                                if (language.Rows.ContainsKey(key))
                                {
                                    row = language.Rows[key];
                                    templateForLanguage = templateForLanguage.Replace(Constants.ArgStart + key + Constants.ArgStop, row.Value);
                                }
                                else
                                {
                                    System.Console.WriteLine("Key missing: {0}", key);
                                }
                            }

                            writer.Append(templateForLanguage);
                            writer.AppendLine();
                        }

                        await streamWriter.WriteAsync(writer.ToString());
                    }
            }
            catch (Exception e)
            {
                if (e is FileNotFoundException || e is IOException)
                {
                    if (string.IsNullOrEmpty(template))
                    {
                        result = new LocalizationError(
                            LocalizationError.ErrorType.FileError,
                            string.Format("Unable to read template file: {0}. {1}", templateFile, e.Message),
                            0);
                    }
                    else
                    {
                        result = new LocalizationError(
                            LocalizationError.ErrorType.FileError,
                            string.Format("Unable to write to output file: {0}. {1}", outputFile, e.Message),
                            0);
                    }
                }
                else if (e is FormatException)
                {
                    result = new LocalizationError(
                        LocalizationError.ErrorType.PluginError,
                        "Template file has malformed format args.",
                        0);
                }
                else
                {
                    result = new LocalizationError(
                        LocalizationError.ErrorType.PluginError,
                        string.Format("Plugin failed: {0}", e.Message),
                        0);
                }
            }

            return(result);
        }
Exemplo n.º 3
0
        public async Task <LocalizationError> Export(
            IEnumerable <LocalizedLanguage> languages,
            IPluginConfig config)
        {
            LocalizationError result = null;
            var keysListFile         = config.GetString("keysListFile");
            var outputFile           = config.GetString("outputFile");

            string[] keysToProcess = null;

            try
            {
                keysToProcess = File.ReadAllLines(keysListFile);

                using (var stream = File.OpenWrite(outputFile))
                    using (var streamWriter = new StreamWriter(stream))
                    {
                        Configuration configuration = new Configuration
                        {
                            Encoding = Encoding.UTF8
                        };

                        CsvWriter         writer         = new CsvWriter(streamWriter, configuration);
                        LocalizedLanguage masterLanguage = null;

                        // Write the header
                        writer.WriteField("Language");
                        foreach (var key in keysToProcess)
                        {
                            writer.WriteField(key);
                        }

                        writer.NextRecord();

                        // Add a row for each language with the keys as columns.
                        foreach (var language in languages)
                        {
                            if (language.IsMasterLanguage)
                            {
                                masterLanguage = language;
                            }

                            writer.WriteField(language.Name);

                            foreach (var key in keysToProcess)
                            {
                                if (language.Rows.ContainsKey(key))
                                {
                                    var row = language.Rows[key];
                                    writer.WriteField(row.Value);
                                }
                                else
                                {
                                    writer.WriteField(string.Empty);
                                }
                            }

                            writer.NextRecord();
                        }

                        await writer.FlushAsync();
                    }
            }
            catch (Exception e)
            {
                if (e is FileNotFoundException || e is IOException)
                {
                    if (keysToProcess == null)
                    {
                        result = new LocalizationError(
                            LocalizationError.ErrorType.FileError,
                            string.Format("Unable to write to output file: {0}. {1}", outputFile, e.Message),
                            0);
                    }
                    else
                    {
                        result = new LocalizationError(
                            LocalizationError.ErrorType.FileError,
                            string.Format("Unable to read key list: {0}. {1}", keysListFile, e.Message),
                            0);
                    }
                }
                else
                {
                    result = new LocalizationError(
                        LocalizationError.ErrorType.PluginError,
                        string.Format("Plugin failed: {0}", e.Message),
                        0);
                }
            }

            return(result);
        }
Exemplo n.º 4
0
        public async Task <LocalizationError> Export(
            IEnumerable <LocalizedLanguage> languages,
            IPluginConfig config)
        {
            LocalizationError result = null;
            var outputDir            = string.Empty;

            try
            {
                outputDir = config.GetString("outputDir");
                var outputFile      = Path.Combine(outputDir, "{0}-Characters.txt");
                var includeAlphabet = config.GetBool("includeAlphabet", false);

                HashSet <char> uniqueCharacters = new HashSet <char>();
                if (includeAlphabet)
                {
                    // Include common characters if flag is present
                    var commonCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.,:;?!'\"";
                    foreach (var character in commonCharacters)
                    {
                        uniqueCharacters.Add(character);
                    }
                }

                // Gather all the characters in every language
                foreach (var language in languages)
                {
                    // And all the characters per language
                    HashSet <char> uniqueCharactersForLanguage = new HashSet <char>();
                    foreach (var row in language.Rows.Values)
                    {
                        foreach (var character in row.Value)
                        {
                            if (Char.IsWhiteSpace(character))
                            {
                                continue;
                            }

                            uniqueCharactersForLanguage.Add(character);
                            uniqueCharacters.Add(character);
                        }

                        // Write the results for the current language
                        using (var stream = File.OpenWrite(string.Format(outputFile, language.Name)))
                            using (var streamWriter = new StreamWriter(stream))
                            {
                                foreach (var character in uniqueCharactersForLanguage)
                                {
                                    streamWriter.Write(character);
                                }

                                await streamWriter.FlushAsync();
                            }
                    }
                }

                // Write the results for all languages
                using (var stream = File.OpenWrite(string.Format(outputFile, "All")))
                    using (var streamWriter = new StreamWriter(stream))
                    {
                        foreach (var character in uniqueCharacters)
                        {
                            streamWriter.Write(character);
                        }

                        await streamWriter.FlushAsync();
                    }
            }
            catch (Exception e)
            {
                if (e is IOException)
                {
                    result = new LocalizationError(
                        LocalizationError.ErrorType.FileError,
                        string.Format("Unable to write to output file: {0}. {1}", outputDir, e.Message),
                        0);
                }
                else
                {
                    result = new LocalizationError(
                        LocalizationError.ErrorType.PluginError,
                        string.Format("Plugin failed: {0}", e.Message),
                        0);
                }
            }

            return(result);
        }