Exemplo n.º 1
0
        public static string GetPoValue(this AXmlPluralItem item)
        {
            switch (item.Quantity)
            {
            case QuantityType.zero:
                return(item.Value.QuantityReplace(0));

            case QuantityType.one:
                return(item.Value.QuantityReplace(1));

            case QuantityType.two:
                return(item.Value.QuantityReplace(2));

            case QuantityType.few:
                return(item.Value.QuantityReplace(3));

            case QuantityType.other:
                return(item.Value.QuantityReplace(100));

            case QuantityType.many:
                return(item.Value.QuantityReplace(59));

            default:
                return(item.Value);
            }
        }
Exemplo n.º 2
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}");
                }
            }
        }