예제 #1
0
        public void Convert(Xliff xlifobject, string jsonFile)
        {
            if (xlifobject != null && xlifobject.Files != null)
            {
                // convert format
                foreach (var f in xlifobject.Files)
                {
                    f.keyValuePairs = f.body.transunits.ToDictionary(x => x.Key, x => x.Value);
                    f.body          = null;
                }

                var translationunit = xlifobject.Files.ToDictionary(x => GetOutputFile(x), x => x.keyValuePairs);

                ExtensionTranslatedObject finaloutput = new ExtensionTranslatedObject();
                finaloutput.contents = translationunit;

                JsonSerializerSettings setting = new JsonSerializerSettings()
                {
                    NullValueHandling = NullValueHandling.Ignore,
                    Formatting        = Newtonsoft.Json.Formatting.Indented
                };
                string jsonString = JsonConvert.SerializeObject(finaloutput, setting);
                File.WriteAllText(jsonFile, jsonString);
            }
        }
        private void DirectoryWalk(string dir, int prefixLen, bool expectJson = true)
        {
            try
            {
                // check files at current level before going into lower level directories
                foreach (string f in Directory.GetFiles(dir))
                {
                    if (f.EndsWith(extension, StringComparison.OrdinalIgnoreCase))
                    {
                        int    startIndex = this.directory.Length + (prefixLen);
                        string parsedFile = CleanUpPath(startIndex, f, extension);
                        if (this.pathMap != null)
                        {
                            parsedFile = this.pathMap.Map(parsedFile);
                        }

                        string resourceContent = File.ReadAllText(f);
                        if (expectJson)
                        {
                            Dictionary <string, object> resourceMap = JsonConvert.DeserializeObject <Dictionary <string, object> >(resourceContent);
                            if (resourceMap != null && resourceMap.Keys.Count > 0)
                            {
                                resourceList.Add(parsedFile, resourceMap);
                            }
                        }
                        else
                        {
                            XmlRootAttribute xRoot = new XmlRootAttribute();
                            xRoot.ElementName = "xliff";
                            xRoot.Namespace   = "urn:oasis:names:tc:xliff:document:1.2";
                            xRoot.IsNullable  = true;

                            System.IO.StreamReader file = new System.IO.StreamReader(f);
                            System.Xml.Serialization.XmlSerializer reader = new System.Xml.Serialization.XmlSerializer(typeof(Xliff), xRoot);
                            Xliff obj = (Xliff)reader.Deserialize(file);
                            if (obj != null)
                            {
                                this.xlfResourceList.Add(parsedFile, obj);
                            }
                        }

                        resourceFiles.Add(parsedFile);
                        Console.WriteLine(f);
                    }
                }
                // repeat for lower levels
                foreach (string d in Directory.GetDirectories(dir))
                {
                    DirectoryWalk(d, prefixLen);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }