示例#1
0
        public void LoadFromManifest(string InFileName, string FileText)
        {
            Clear();

            LocaleManifest manifest = JsonConvert.DeserializeObject <LocaleManifest>(FileText);

            if (ManifestVersion != manifest.FormatVersion)
            {
                throw new FormatException("Invalid Manifest::FormatVersion.");
            }

            if (ManifestNamespace != manifest.Namespace)
            {
                throw new FormatException("Invalid Manifest::Namespace. Must be empty.");
            }

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

            Subnamespaces = new List <InternalNamespace>();
            foreach (var ns in manifest.Subnamespaces)
            {
                InternalNamespace ins = new InternalNamespace();
                ins.Name     = ns.Namespace;
                ins.Children = new List <InternalRecord>(ns.Children.Count);

                foreach (var child in ns.Children)
                {
                    InternalRecord record = new InternalRecord();
                    record.Source = child.Source.Text;
                    record.Keys   = new List <InternalKey>(child.Keys.Count);

                    foreach (var key in child.Keys)
                    {
                        InternalKey ikey = new InternalKey();
                        ikey.Key          = key.Key;
                        ikey.Path         = key.Path;
                        ikey.Translations = new List <InternalText>();
                        ikey.parent       = record;
                        record.Keys.Add(ikey);
                    }

                    ins.Children.Add(record);
                }

                Subnamespaces.Add(ins);
            }

            Cultures = new List <string>();
        }
示例#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);
        }