Пример #1
0
        private void UpdateResourceFromDelta(ResxKey resource, string culture, string filePath)
        {
            if (File.Exists(filePath))
            {
                var xml = XDocument.Load(filePath);
                var root = xml.Root;
                if (root == null)
                    return;

                var element = root.Elements("data").FirstOrDefault(x => x.Attribute("name").Value == resource.Key);
                if (element == null)
                    return;

                if (!resource.Values.ContainsKey(culture))
                    resource.Values[culture] = new ResxValue("");

                resource.Values[culture].HasChangedInDelta = true;

                var valueNode = element.Element("value");
                resource.Values[culture.ToLower()].Value = valueNode == null ? "" : valueNode.Value;
            }
        }
Пример #2
0
        private void SaveDelta(FormCollection form, string deltaDirectoryPath, ResxKey resource, string culture)
        {
            var oldValue = resource.Values.ContainsKey(culture) ? resource.Values[culture].Value ?? "" : null;
            var newValue = form[resource.RelativeFilePath + "|" + resource.Key + "|" + culture];

            //fix line breaks
            oldValue = oldValue == null ? null : oldValue.Replace("\r", "").Replace("\n", Environment.NewLine);
            newValue = newValue == null ? null : newValue.Replace("\r", "").Replace("\n", Environment.NewLine);

            if (oldValue != newValue && !string.IsNullOrEmpty(newValue))
            {
                var deltaPath = ResxDeltaReader.GetDeltaFilePath(deltaDirectoryPath, resource.RelativeFilePath, culture);

                XDocument xml;
                if (File.Exists(deltaPath))
                    xml = XDocument.Load(deltaPath);
                else
                    xml = new XDocument(new XDeclaration("1.0", "UTF-8", "yes"), new XElement("root"));

                var root = xml.Root;
                var node = new XElement("data");
                root.Add(node);

                node.Add(new XAttribute("name", resource.Key), new XAttribute(XNamespace.Xml + "space", "preserve"), new XElement("value", newValue));

                if (!Directory.Exists(Path.GetDirectoryName(deltaPath)))
                    Directory.CreateDirectory(Path.GetDirectoryName(deltaPath));
                xml.Save(deltaPath);
            }
        }
Пример #3
0
        private void ReadResources(string rootPath, string defaultFilePath, string cultureFilePath, string culture, IList<ResxKey> result)
        {
            var xml = XDocument.Load(cultureFilePath);
            var root = xml.Root;
            if (root == null)
                throw new ArgumentException("There's no root node in '" + cultureFilePath + "'.");

            foreach (var node in root.Elements("data"))
            {
                var key = node.Attribute("name").Value;
                if (KeysToIgnore.Select(x => x.ToLower()).Any(x => x == key.ToLower()))
                    continue;

                var relativePath = defaultFilePath.Substring(rootPath.Length + 1);

                var resource = result.FirstOrDefault(x => x.Key == key && x.RelativeFilePath == relativePath);
                if (resource == null)
                    result.Add(resource = new ResxKey(relativePath, key));

                var valueNode = node.Element("value");
                resource.Values[culture] = new ResxValue(valueNode == null ? "" : valueNode.Value);
            }
        }