Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 2
0
        public static InternalFormat Import(string FileName)
        {
            InternalFormat data = null;

            // open excel document
            Excel.Application App       = new Excel.Application();
            Excel.Workbooks   Workbooks = App.Workbooks;
            Excel.Workbook    Workbook  = Workbooks.Open(FileName);
            Excel._Worksheet  Worksheet = App.ActiveSheet;
            Excel.Range       Range     = Worksheet.UsedRange;

            // read document data

            var Cells       = Range.Value2;
            int rowCount    = Cells.GetLength(0);
            int columnCount = Cells.GetLength(1);

            data = new InternalFormat();

            // read native and other cultures
            data.Cultures = new List <string>();
            for (int col = 3; col <= columnCount; col++)
            {
                if (Cells[1, col] != null)
                {
                    // third column is NativeCulture
                    if (col == 3)
                    {
                        data.NativeCulture = Cells[1, col];
                    }
                    data.Cultures.Add(Cells[1, col]);
                }
            }

            int index               = 2;
            int cultureCount        = data.Cultures.Count;
            List <InternalKey> keys = new List <InternalKey>(rowCount / 2);

            // read all translation keys
            for (; Cells[index, 1].ToString() != serviceData; index++)
            {
                InternalKey key = new InternalKey();
                key.Key          = InternalNamespace.SplitFullName(Cells[index, 2])[1];
                key.Translations = new List <InternalText>(cultureCount);
                for (int culture = 0; culture < cultureCount; culture++)
                {
                    InternalText translation = new InternalText();
                    translation.Culture = data.Cultures[culture];
                    translation.Text    = Cells[index, culture + 3];
                    if (translation.Text == null)
                    {
                        translation.Text = "";
                    }
                    else // replace \n to \r\n
                    {
                        translation.Text = Regex.Replace(translation.Text, "(?<!\r)\n", "\r\n");
                    }
                    key.Translations.Add(translation);
                }
                keys.Add(key);
            }

            int indexOfServiceData = index;

            data.Subnamespaces = new List <InternalNamespace>(rowCount / 2);
            InternalNamespace lastNS  = null;
            InternalRecord    lastRec = null;

            index++;
            for (; index < rowCount + 1; index++)
            {
                string source = Cells[index, 1];
                string ns     = Cells[index, 2];
                string key    = Cells[index, 3];
                string path   = Cells[index, 4];

                if (lastNS == null || lastNS.Name != ns)
                {
                    lastNS          = new InternalNamespace();
                    lastNS.Name     = ns;
                    lastNS.Children = new List <InternalRecord>();
                    data.Subnamespaces.Add(lastNS);
                    lastRec = null;
                }

                if (lastRec == null || lastRec.Source != source)
                {
                    lastRec        = new InternalRecord();
                    lastRec.Source = source;
                    lastRec.Keys   = new List <InternalKey>();
                    lastNS.Children.Add(lastRec);
                }

                InternalKey ikey = keys[index - indexOfServiceData - 1];
                if (ikey.Key != key)
                {
                    throw new FormatException("Unexpected key: " + key + "!");
                }

                ikey.Path   = path;
                ikey.parent = lastRec;
                lastRec.Keys.Add(ikey);
            }

            // close excel and clear all headres
            Marshal.ReleaseComObject(Range); Range         = null;
            Marshal.ReleaseComObject(Worksheet); Worksheet = null;
            Workbook.Close(false, Type.Missing, Type.Missing);
            Marshal.ReleaseComObject(Workbook); Workbook = null;
            Workbooks.Close();
            Marshal.ReleaseComObject(Workbooks); Workbooks = null;
            App.Quit();
            Marshal.ReleaseComObject(App); App = null;

            return(data);
        }