Esempio n. 1
0
        public override void Convert()
        {
            DirectoryInfo dInfo    = new DirectoryInfo(this.Context.PoProjPath);
            string        aResPath = Path.Combine(this.Context.AProjPath, AXmlHelper.AXML_PATH);

            string tempPotPath = Path.Combine(dInfo.FullName, "template.pot");

            PoResource tempPot = PoResource.ReadPoFile(tempPotPath);

            var poFiles = dInfo.GetFiles("*.po").Where(f => !f.Name.StartsWith("~"));

            foreach (FileInfo poFile in poFiles)
            {
                PoResource poRes = PoResource.ReadPoFile(poFile.FullName);

                if (poRes == null)
                {
                    Console.WriteLine($"File {poFile.Name} didn't load");
                    continue;
                }

                AXmlResource aRes     = null;
                string       aXmlPath = null;

                if (string.Equals(poRes.Language, "en", StringComparison.OrdinalIgnoreCase))
                {
                    if (this.Context.IgnoreASource)
                    {
                        Console.WriteLine("values/string.xml is skipped");
                        continue;
                    }

                    aXmlPath = Path.Combine(aResPath, "values\\strings.xml");

                    if (File.Exists(aXmlPath))
                    {
                        aRes = AXmlHelper.ReadAXml(aXmlPath);
                    }
                }
                else
                {
                    aXmlPath = Path.Combine(aResPath, $"values-{this.Context.Map.GetA(poRes.Language)}\\strings.xml");

                    if (Directory.Exists(aResPath))
                    {
                        string          pattern = $"values*-{this.Context.Map.GetA(poRes.Language)}";
                        DirectoryInfo[] dirs    = new DirectoryInfo(aResPath).GetDirectories(pattern, SearchOption.TopDirectoryOnly);

                        if (dirs.Length > 0)
                        {
                            aXmlPath = Path.Combine(dirs[0].FullName, "strings.xml");

                            if (File.Exists(aXmlPath))
                            {
                                aRes = AXmlHelper.ReadAXml(aXmlPath);
                            }
                        }
                    }
                }

                if (aRes == null)
                {
                    aRes = new AXmlResource();
                }

                aRes.Language = this.Context.Map.GetA(poRes.Language);

                foreach (PoString poStr in poRes)
                {
                    // If value is empty, then default(en) value should be used
                    if (poStr.IsEmpty)
                    {
                        continue;
                    }

                    //PoString tempStr = tempPot.FirstOrDefault(s => s.Id == poStr.Id);
                    // Finding template string using link (it's android id)
                    PoString tempStr = tempPot.FirstOrDefault(s => s.Links.Any(l => poStr.Links.Any(lp => l == lp)));
                    var      links   = new List <string>();

                    if (tempStr != null)
                    {
                        if (!tempStr.Value.Contains("\n"))
                        {
                            // Replace all extra new lines (for zanata)
                            poStr.Value = poStr.Value.Replace("\n", string.Empty).Replace("\r", string.Empty);
                        }
                    }

                    if (tempStr != null && tempStr.Links != null)
                    {
                        links.AddRange(tempStr.Links);
                    }

                    if (poStr.Links != null)
                    {
                        links.AddRange(poStr.Links.Where(l => !links.Contains(l)));
                    }

                    if (links.Count == 0)
                    {
                        Console.WriteLine($"WARNING: string key not found. File: {poFile.Name}, Res: {poStr.Id}");
                        links.Add(poStr.Id);
                    }

                    foreach (string id in links)
                    {
                        AXmlResourceItem xmlString = aRes.FirstOrDefault(a => a.Name == id);

                        if (xmlString == null)
                        {
                            if (poStr.IsPluralString)
                            {
                                xmlString = new AXmlPlural();
                            }
                            else
                            {
                                xmlString = new AXmlString();
                            }
                            xmlString.Name = id;
                            if (poStr.Comments != null && poStr.Comments.Count > 0)
                            {
                                xmlString.Comments.Clear();
                                xmlString.Comments.AddRange(poStr.Comments);
                            }
                            aRes.Add(xmlString);
                        }

                        if (xmlString is AXmlString)
                        {
                            AXmlString aString = (AXmlString)xmlString;

                            aString.Value = poStr.Value;
                        }
                        else if (xmlString is AXmlPlural)
                        {
                            AXmlPlural aPlural = (AXmlPlural)xmlString;

                            if (!poStr.IsPluralString)
                            {
                                throw new Exception($"Expected plural string: {poFile.Name} - {poStr.Id}");
                            }

                            AXmlPluralItem aPluralItem;
                            if (aPlural.Items.ContainsKey(poStr.PluralType.Value))
                            {
                                aPluralItem = aPlural.Items[poStr.PluralType.Value];
                            }
                            else
                            {
                                aPluralItem          = new AXmlPluralItem();
                                aPluralItem.Quantity = poStr.PluralType.Value;
                                aPlural.Add(aPluralItem);
                            }

                            aPluralItem.Value = poStr.GetXmlValue();
                        }
                    }
                }

                FileInfo ax = new FileInfo(aXmlPath);
                this.MakeBackup(aXmlPath);
                try
                {
                    AXmlHelper.SaveAXml(aRes, aXmlPath);
                    Console.WriteLine($"{ax.Directory.Name}/{ax.Name} converted.");
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"Error saving file: {ax.Directory.Name}/{ax.Name}. Message: {ex.Message}");
                }
            }
        }
Esempio n. 2
0
        public override void Convert()
        {
            DirectoryInfo aProjDir = new DirectoryInfo(Path.Combine(this.Context.AProjPath, AXmlHelper.AXML_PATH));

            FileInfo[] aFiles = aProjDir.GetFiles("strings.xml", SearchOption.AllDirectories);

            List <AXmlResource> aResources = new List <AXmlResource>();
            AXmlResource        sourceRes  = null;

            foreach (FileInfo aFile in aFiles)
            {
                AXmlResource resource = AXmlHelper.ReadAXml(aFile.FullName);
                if (resource != null)
                {
                    Console.WriteLine($"{aFile.Directory.Name}/{aFile.Name} successfully read");
                }
                else
                {
                    Console.WriteLine($"Error reading file {aFile.Directory.Name}/{aFile.Name}");
                }

                if (resource == null)
                {
                    continue;
                }

                if (this.Context.Map.IsAIgnored(resource.Language))
                {
                    Console.WriteLine($"{aFile.Directory.Name}/{aFile.Name} ignored");
                    continue;
                }

                if (String.Equals(this.Context.Map.GetPo(resource.Language), "en", StringComparison.OrdinalIgnoreCase))
                {
                    sourceRes = resource;
                }

                aResources.Add(resource);
            }

            // Create template.pot
            PoResource poFile = new PoResource();

            poFile.Language = "en";

            foreach (AXmlResourceItem xmlString in sourceRes)
            {
                if (xmlString is AXmlString)
                {
                    AXmlString aString = (AXmlString)xmlString;
                    if (aString.IsTranslatable)
                    {
                        PoString poString = poFile.FirstOrDefault(p => p.Id == aString.Value);

                        if (poString == null)
                        {
                            poString = new PoString();
                            poFile.Add(poString);
                        }

                        // Save id of android resource
                        poString.Links.Add(aString.Name);
                        poString.Id    = aString.Value;
                        poString.Value = "";
                        poString.Comments.Clear();
                        poString.Comments.AddRange(aString.Comments);
                    }
                }
                else if (xmlString is AXmlPlural)
                {
                    AXmlPlural aPlural = (AXmlPlural)xmlString;

                    foreach (AXmlPluralItem aPluralItem in aPlural.Items.Values)
                    {
                        PoString poString = poFile.FirstOrDefault(p => aPluralItem.GetPoValue() == p.Id);

                        if (poString == null)
                        {
                            poString = new PoString();
                            poFile.Add(poString);
                        }

                        poString.PluralType = aPluralItem.Quantity;
                        //poString.PluralLink = aPlural.Name;
                        poString.Links.Add(aPlural.Name);
                        poString.Id    = aPluralItem.GetPoValue();
                        poString.Value = "";
                        poString.Comments.Clear();
                        poString.Comments.AddRange(aPlural.Comments);
                    }
                }
            }
            this.MakeBackup("template.pot");
            poFile.Save(Path.Combine(this.Context.PoProjPath, "template.pot"));

            Console.WriteLine("template.pot converted");

            // Create po files

            foreach (AXmlResource aRes in aResources)
            {
                poFile          = new PoResource();
                poFile.Language = this.Context.Map.GetPo(aRes.Language);

                foreach (AXmlResourceItem xmlString in aRes)
                {
                    if (xmlString is AXmlPlural)
                    {
                        AXmlPlural aPlural      = (AXmlPlural)xmlString;
                        AXmlPlural sourcePlural = (AXmlPlural)sourceRes.FirstOrDefault(s => s.Name == aPlural.Name);

                        foreach (AXmlPluralItem aPluralItem in aPlural.Items.Values)
                        {
                            string pid;
                            if (sourcePlural != null)
                            {
                                pid = sourcePlural.Items[aPluralItem.Quantity].GetPoValue();
                            }
                            else
                            {
                                // Should not be called
                                pid = aPlural.Name;
                            }

                            PoString poString = poFile.FirstOrDefault(p => p.Id == pid);

                            if (poString == null)
                            {
                                poString = new PoString();
                                poFile.Add(poString);
                            }

                            poString.Id    = pid;
                            poString.Value = aPluralItem.GetPoValue();
                            // Save id of android resource
                            poString.Links.Add(aPlural.Name);
                            poString.PluralType = aPluralItem.Quantity;
                            poString.Comments.Clear();
                            poString.Comments.AddRange(aPlural.Comments);
                            //poString.PluralLink = aPlural.Name;
                        }
                        continue;
                    }
                    else if (xmlString is AXmlString)
                    {
                        AXmlString aString = (AXmlString)xmlString;
                        if (aString.IsTranslatable)
                        {
                            AXmlString sourceStr = (AXmlString)sourceRes.FirstOrDefault(s => s.Name == aString.Name);
                            string     pid;
                            if (sourceStr != null)
                            {
                                pid = sourceStr.Value;
                            }
                            else
                            {
                                pid = aString.Name;
                            }

                            PoString poString = poFile.FirstOrDefault(p => p.Id == pid);

                            if (poString == null)
                            {
                                poString = new PoString();
                                poFile.Add(poString);
                            }

                            poString.Id    = pid;
                            poString.Value = aString.Value;
                            // Save id of android resource
                            poString.Links.Add(aString.Name);
                            poString.Comments.Clear();
                            poString.Comments.AddRange(aString.Comments);
                        }
                    }
                }

                this.MakeBackup(poFile.GetFileName());
                poFile.Save(Path.Combine(this.Context.PoProjPath, poFile.GetFileName()));
                Console.WriteLine($"{poFile.GetFileName()} converted");
            }
        }