예제 #1
0
 public bool TryGetTransUnit(string id, XlfDialect dialect, out XlfTransUnit unit)
 {
     try
     {
         unit = GetTransUnit(id, dialect);
         return(true);
     }
     catch (InvalidOperationException)
     {
         unit = null;
         return(false);
     }
 }
예제 #2
0
        public XlfTransUnit AddTransUnit(string id, string source, string target, AddMode addMode, XlfDialect dialect)
        {
            if (addMode != AddMode.DontCheckExisting && TryGetTransUnit(id, dialect, out XlfTransUnit resultUnit))
            {
                switch (addMode)
                {
                case AddMode.FailIfExists:
                    throw new InvalidOperationException($"There is already a trans-unit with id={id}");

                case AddMode.SkipExisting:
                    return(resultUnit);

                default:
                case AddMode.UpdateExisting:
                    resultUnit.Source = source;

                    // only update target value if there is already a target element present
                    if (resultUnit.Target != null)
                    {
                        resultUnit.Target = target;
                    }

                    return(resultUnit);
                }
            }

            var n = new XElement(this.ns + ElementTransUnit);

            n.SetAttributeValue("id", id);
            n.SetAttributeValue("translate", "yes");
            n.SetAttributeValue(XNamespace.Xml + "space", "preserve");
            var transUnits = this.node.Descendants(this.ns + ElementTransUnit).ToList();

            if (transUnits.Any())
            {
                transUnits.Last().AddAfterSelf(n);
            }
            else
            {
                var bodyElements = this.node.Descendants(this.ns + ElementBody).ToList();

                XElement body;

                if (bodyElements.Any())
                {
                    body = bodyElements.First();
                }
                else
                {
                    body = new XElement(this.ns + ElementBody);
                    this.node.Add(body);
                }

                body.Add(n);
            }

            if (dialect == XlfDialect.RCWinTrans11)
            {
                var unit = new XlfTransUnit(n, this.ns, IdNone, source, target)
                {
                    TargetState = "new"
                };
                unit.Optional.Resname = id;
                return(unit);
            }
            else
            {
                return(new XlfTransUnit(n, this.ns, id, source, target)
                {
                    TargetState = "new"
                });
            }
        }
예제 #3
0
        public XlfTransUnit AddTransUnit(string id, string source, string target, AddMode addMode, XlfDialect dialect)
        {
            XlfTransUnit resultUnit;

            if (TryGetTransUnit(id, dialect, out resultUnit))
            {
                switch (addMode)
                {
                case AddMode.FailIfExists:
                    throw new InvalidOperationException($"The is already a trans-unit with id={id}");

                case AddMode.SkipExisting:
                    return(resultUnit);

                default:
                case AddMode.UpdateExisting:
                    resultUnit.Source = source;
                    resultUnit.Target = target;
                    return(resultUnit);
                }
            }

            var n          = new XElement(this.ns + ElementTransUnit);
            var transUnits = this.node.Descendants(this.ns + ElementTransUnit).ToList();

            if (transUnits.Any())
            {
                transUnits.Last().AddAfterSelf(n);
            }
            else
            {
                var bodyElements = this.node.Descendants(this.ns + ElementBody).ToList();

                XElement body;

                if (bodyElements.Any())
                {
                    body = bodyElements.First();
                }
                else
                {
                    body = new XElement(this.ns + ElementBody);
                    this.node.Add(body);
                }

                body.Add(n);
            }

            if (dialect == XlfDialect.RCWinTrans11)
            {
                var unit = new XlfTransUnit(n, this.ns, IdNone, source, target);
                unit.Optional.Resname = id;
                return(unit);
            }
            else if (dialect == XlfDialect.MultilingualAppToolkit)
            {
                if (!id.ToLowerInvariant().StartsWith(XlfTransUnit.ResxPrefix.ToLowerInvariant()))
                {
                    return(new XlfTransUnit(n, this.ns, XlfTransUnit.ResxPrefix + id, source, target));
                }
            }

            return(new XlfTransUnit(n, this.ns, id, source, target));
        }
예제 #4
0
        public XlfTransUnit AddTransUnit(string id, string source, string target, AddMode addMode, XlfDialect dialect)
        {
            if (TryGetTransUnit(id, dialect, out XlfTransUnit resultUnit))
            {
                switch (addMode)
                {
                case AddMode.FailIfExists:
                    ThrowExceptions.Custom(Exc.GetStackTrace(), type, Exc.CallingMethod(), $"There is already a trans-unit with id={id}");
                    return(null);

                case AddMode.SkipExisting:
                    return(resultUnit);

                default:
                case AddMode.UpdateExisting:
                    resultUnit.Source = source;
                    // only update target value if there is already a target element present
                    if (resultUnit.Target != null)
                    {
                        resultUnit.Target = target;
                    }
                    return(resultUnit);
                }
            }
            var n          = new XElement(this.ns + ElementTransUnit);
            var transUnits = this.node.Descendants(this.ns + ElementTransUnit).ToList();

            if (transUnits.Any())
            {
                transUnits.Last().AddAfterSelf(n);
            }
            else
            {
                var      bodyElements = this.node.Descendants(this.ns + ElementBody).ToList();
                XElement body;
                if (bodyElements.Any())
                {
                    body = bodyElements.First();
                }
                else
                {
                    body = new XElement(this.ns + ElementBody);
                    this.node.Add(body);
                }
                body.Add(n);
            }
            if (dialect == XlfDialect.RCWinTrans11)
            {
                var unit = new XlfTransUnit(n, this.ns, IdNone, source, target);
                unit.Optional.Resname = id;
                return(unit);
            }
            else if (dialect == XlfDialect.MultilingualAppToolkit)
            {
                if (!id.StartsWith(XlfTransUnit.ResxPrefix, StringComparison.InvariantCultureIgnoreCase))
                {
                    return(new XlfTransUnit(n, this.ns, XlfTransUnit.ResxPrefix + id, source, target));
                }
            }
            return(new XlfTransUnit(n, this.ns, id, source, target));
        }