Esempio n. 1
0
        public uint AddToDB(string message, string hint, bool withParams)
        {
            if (hint == "")
            {
                hint = null;
            }
            var key = new TranslationKey(message, hint, withParams);

            if (!Key2Id.TryGetValue(key, out var id))
            {
                id = (uint)Id2Key.Count;
                Id2Key.Add(key);
                Key2Id.Add(key, id);
            }
            return(id);
        }
Esempio n. 2
0
        public bool SubtractExportedLanguage(string file1, string file2, string outputFile)
        {
            var keys = new HashSet <TranslationKey>();

            ImportTranslatedLanguageInternal(file1,
                                             (source, hint, target) => keys.Add(new TranslationKey(source, hint, false)));
            ImportTranslatedLanguageInternal(file2, (source, hint, target) =>
            {
                var key = new TranslationKey(source, hint, false);
                if (keys.Contains(key))
                {
                    keys.Remove(key);
                }
            });

            var strBuilder = new StringBuilder();

            foreach (var key in keys)
            {
                var content = ExportLanguageItem(key.Message, key.Hint);
                strBuilder.Append(content);
            }

            if (strBuilder.Length == 0)
            {
                _logger?.Warn("The result is empty set. Nothing saved.");
                return(false);
            }

            try
            {
                File.WriteAllText(outputFile, strBuilder.ToString());
            }
            catch (Exception e)
            {
                _logger?.Error(e.Message);
                return(false);
            }

            return(true);
        }
Esempio n. 3
0
        public bool ImportTranslatedLanguage(string pathFrom, string pathTo = null)
        {
            var normalizedPath = PathUtils.Normalize(pathFrom);
            var language       = Path.GetFileNameWithoutExtension(normalizedPath);

            if (pathTo != null)
            {
                normalizedPath = PathUtils.Normalize(pathTo);
                language       = Path.GetFileNameWithoutExtension(normalizedPath);
            }

            try
            {
                if (!HasLanguage(language))
                {
                    throw new Exception($"Language {language} does not exist. Probably file name is not valid.");
                }

                ImportTranslatedLanguageInternal(pathFrom, (source, hint, target) =>
                {
                    var key = new TranslationKey(source, hint, true);
                    if (Key2Id.TryGetValue(key, out var idt))
                    {
                        var msg = _messageParser.Parse(target);
                        if (msg is ErrorAst errorMsg)
                        {
                            Logger?.Error("Skipping wrong translation entry:");
                            Logger?.Warn($"S: {source}");
                            Logger?.Warn($"I: {hint}");
                            Logger?.Warn($"T: {target}");
                            Logger?.Error($"Error in g11n format: {errorMsg.Message}");
                        }
                        else
                        {
                            var values = Lang2ValueList[language];
                            while (values.Count < idt)
                            {
                                values.Add(null);
                            }
                            values[(int)idt] = target;
                        }
                    }
                    else
                    {
                        key = new TranslationKey(source, hint, false);
                        if (Key2Id.TryGetValue(key, out var idf))
                        {
                            var values = Lang2ValueList[language];
                            while (values.Count < idf)
                            {
                                values.Add(null);
                            }
                            values[(int)idf] = target;
                        }
                    }
                });
            }
            catch (Exception ex)
            {
                Logger?.Error(ex.Message);
                return(false);
            }

            return(true);
        }