Beispiel #1
0
        public static void ImportFile(Args.ImportFileCommand cmd)
        {
            var muidb = new MuiDBFile(cmd.Muidb, MuiDBFile.OpenMode.CreateIfMissing);

            switch (cmd.Type.ToLowerInvariant())
            {
            case "resx":
                var result = muidb.ImportResX(cmd.In, cmd.Lang);
                if (cmd.Verbose)
                {
                    foreach (var added in result.AddedItems)
                    {
                        Console.WriteLine($"Added resource '{added}'");
                    }

                    foreach (var updated in result.UpdatedItems)
                    {
                        Console.WriteLine($"Updated resource '{updated}'");
                    }
                }

                Console.WriteLine($"Added items: {result.AddedItems.Count}\nupdated items: {result.UpdatedItems.Count}");
                break;

            case "xliff":
                var doc  = new XliffParser.XlfDocument(cmd.In);
                var file = doc.Files.First();

                Verbose(cmd, $"Adding/updating resources for language '{cmd.Lang}...");

                foreach (var unit in file.TransUnits)
                {
                    var id = unit.Id;
                    if (string.Equals(id, "none"))
                    {
                        id = unit.Optional.Resname;
                    }

                    var comment = unit.Optional.Notes.Any() ? unit.Optional.Notes.First().Value : null;
                    Verbose(cmd, $"Adding/updating resource '{id}': text='{unit.Target}', state='{unit.Optional.TargetState}'");

                    string translatedState;
                    try
                    {
                        translatedState = StateConverter.ToMuiDB(unit.Optional.TargetState);
                    }
                    catch (Exception)
                    {
                        translatedState = StateConverter.MuiDbStates.New;
                        Console.Error.WriteLine($"Warning: state '{unit.Optional.TargetState}' of item '{id}' is unknown and will be mapped to '{translatedState}'");
                    }

                    muidb.AddOrUpdateString(id, cmd.Lang, unit.Target, translatedState, comment);
                }

                break;

            default:
                throw new Exception($"Unknown format: {cmd.Type}");
            }

            muidb.Save();
        }
Beispiel #2
0
        public AddOrUpdateResult AddOrUpdateString(string id, string lang, string text, string state, string comment)
        {
            var validState = StateConverter.ToMuiDB(state);  // may throw ArgumentOutOfRangeException

            var stringsNode = doc.Root.Element(ns + ItemsElementName);

            if (stringsNode == null)
            {
                stringsNode = new XElement(ns + ItemsElementName);
                doc.Root.Add(stringsNode);
            }

            var               items = stringsNode.Elements(ns + ItemElementName).Where(i => i.Attribute(IdAttributeName)?.Value == id);
            XElement          item;
            AddOrUpdateResult result;

            if (items.Any())
            {
                item   = items.First();
                result = AddOrUpdateResult.Updated;
            }
            else
            {
                item = new XElement(ns + ItemElementName);
                item.SetAttributeValue(IdAttributeName, id);
                stringsNode.Add(item);
                result = AddOrUpdateResult.Added;
            }

            var      textNodes = item.Elements(ns + TextElementName).Where(t => t.Attribute(LangAttributeName)?.Value == lang);
            XElement textNode;

            if (textNodes.Any())
            {
                textNode = textNodes.First();
            }
            else
            {
                textNode = new XElement(ns + TextElementName);
                textNode.SetAttributeValue(LangAttributeName, lang);
                item.Add(textNode);

                // add new language to global language list
                var langs = GetLanguages();
                if (!langs.Contains(lang))
                {
                    langs.Add(lang);
                    SetLanguages(langs);
                }
            }

            textNode.SetValue(text);
            textNode.SetAttributeValue(StateAttributeName, validState);

            if (!string.IsNullOrWhiteSpace(comment))
            {
                var commentNodes = item.Elements(ns + CommentElementName);
                ////var commentNodes = item.Elements(ns + CommentName).Where(c => c.Attribute(LangName)?.Value == lang);
                XElement commentNode;
                if (commentNodes.Any())
                {
                    commentNode = commentNodes.First();
                }
                else
                {
                    commentNode = new XElement(ns + CommentElementName);
                    ////commentNode.SetAttributeValue(LangName, lang);
                    item.AddFirst(commentNode);
                }

                commentNode.SetValue(comment);
            }

            return(result);
        }