public void LoadFromArchive(string Culture, string FileText)
        {
            if (Subnamespaces == null)
            {
                throw new System.InvalidOperationException("Load manifest first.");
            }

            if (Cultures.Contains(Culture))
            {
                throw new System.ArgumentException("Culture already appended: " + Culture);
            }

            LocaleArchive archive = JsonConvert.DeserializeObject <LocaleArchive>(FileText);

            if (ArchiveVersion != archive.FormatVersion)
            {
                throw new FormatException("Invalid Archive::FormatVersion.");
            }

            if (ArchiveNamespace != archive.Namespace)
            {
                throw new FormatException("Invalid Archive::Namespace. Must be empty.");
            }

            // Move "Children" to "Subnamespace" without name
            if (archive.Children != null && archive.Children.Count > 0)
            {
                LocaleArchiveNamespace emptyNS = new LocaleArchiveNamespace();
                emptyNS.Namespace = "";
                emptyNS.Children  = archive.Children;
                archive.Subnamespaces.Insert(0, emptyNS);
            }

            foreach (var ns in archive.Subnamespaces)
            {
                InternalNamespace ins = GetNamespace(ns.Namespace);

                if (ins == null)
                {
                    throw new FormatException("Archive::Subnamespace not found: " + ns.Namespace + "!");
                }

                foreach (var child in ns.Children)
                {
                    InternalKey ikey = ins.GetKey(child.Key);

                    if (ikey == null)
                    {
                        throw new FormatException("Invalid key (" + child.Key + ") in Archive::Subnamespace::Child " + child.Source.Text + "!");
                    }

                    InternalText text = new InternalText();
                    text.Culture = Culture;
                    text.Text    = child.Translation.Text;
                    ikey.Translations.Add(text);
                }
            }

            Cultures.Add(Culture);
        }
Beispiel #2
0
        public string SaveToArchive(string Culture)
        {
            if (Cultures == null || Cultures.Contains(Culture) == false)
            {
                throw new System.ArgumentException("Culture not found: " + Culture);
            }

            if (Subnamespaces != null && Subnamespaces.Count > 0)
            {
                LocaleArchive archive = new LocaleArchive();
                archive.FormatVersion = ArchiveVersion;
                archive.Namespace     = ArchiveNamespace;
                archive.Subnamespaces = new List <LocaleArchiveNamespace>(Subnamespaces.Count);

                foreach (var ins in Subnamespaces)
                {
                    LocaleArchiveNamespace ns = new LocaleArchiveNamespace();
                    ns.Namespace = ins.Name;
                    if (ins.Children != null && ins.Children.Count > 0)
                    {
                        ns.Children = new List <LocaleArchiveChild>(ins.Children.Count);
                        foreach (var rec in ins.Children)
                        {
                            foreach (var ikey in rec.Keys)
                            {
                                LocaleArchiveChild child = new LocaleArchiveChild();
                                child.Source           = new LocaleSource();
                                child.Source.Text      = (Culture == NativeCulture) ? rec.Source : ikey.GetTranslationForCulture(NativeCulture);
                                child.Translation      = new LocaleTranslation();
                                child.Translation.Text = ikey.GetTranslationForCulture(Culture);
                                child.Key = ikey.Key;
                                ns.Children.Add(child);
                            }
                        }
                    }

                    if (ns.Namespace == "")
                    {
                        archive.Children = ns.Children;
                    }
                    else
                    {
                        archive.Subnamespaces.Add(ns);
                    }
                }

                return(JsonConvert.SerializeObject(archive, Formatting.Indented));
            }

            return("");
        }