Example #1
0
        public string Import_CSV(string Category, List <string[]> CSV, eSpreadsheetUpdateMode UpdateMode = eSpreadsheetUpdateMode.Replace)
        {
            string[] array = CSV[0];
            int      num   = 1;
            int      num2  = -1;
            int      num3  = -1;

            string[] texts = new string[1]
            {
                "Key"
            };
            string[] texts2 = new string[1]
            {
                "Type"
            };
            string[] texts3 = new string[2]
            {
                "Desc",
                "Description"
            };
            if (array.Length > 1 && ArrayContains(array[0], texts))
            {
                if (UpdateMode == eSpreadsheetUpdateMode.Replace)
                {
                    ClearAllData();
                }
                if (array.Length > 2)
                {
                    if (ArrayContains(array[1], texts2))
                    {
                        num2 = 1;
                        num  = 2;
                    }
                    if (ArrayContains(array[1], texts3))
                    {
                        num3 = 1;
                        num  = 2;
                    }
                }
                if (array.Length > 3)
                {
                    if (ArrayContains(array[2], texts2))
                    {
                        num2 = 2;
                        num  = 3;
                    }
                    if (ArrayContains(array[2], texts3))
                    {
                        num3 = 2;
                        num  = 3;
                    }
                }
                int   num4   = Mathf.Max(array.Length - num, 0);
                int[] array2 = new int[num4];
                for (int i = 0; i < num4; i++)
                {
                    if (string.IsNullOrEmpty(array[i + num]))
                    {
                        array2[i] = -1;
                        continue;
                    }
                    string text = array[i + num];
                    bool   flag = true;
                    if (text.StartsWith("$"))
                    {
                        flag = false;
                        text = text.Substring(1);
                    }
                    GoogleLanguages.UnPackCodeFromLanguageName(text, out string Language, out string code);
                    int num5 = -1;
                    num5 = (string.IsNullOrEmpty(code) ? GetLanguageIndex(Language) : GetLanguageIndexFromCode(code));
                    if (num5 < 0)
                    {
                        LanguageData languageData = new LanguageData();
                        languageData.Name  = Language;
                        languageData.Code  = code;
                        languageData.Flags = (byte)(0 | ((!flag) ? 1 : 0));
                        mLanguages.Add(languageData);
                        num5 = mLanguages.Count - 1;
                    }
                    array2[i] = num5;
                }
                num4 = mLanguages.Count;
                int j = 0;
                for (int count = mTerms.Count; j < count; j++)
                {
                    TermData termData = mTerms[j];
                    if (termData.Languages.Length < num4)
                    {
                        Array.Resize(ref termData.Languages, num4);
                        Array.Resize(ref termData.Languages_Touch, num4);
                        Array.Resize(ref termData.Flags, num4);
                    }
                }
                int k = 1;
                for (int count2 = CSV.Count; k < count2; k++)
                {
                    array = CSV[k];
                    string Term  = string.IsNullOrEmpty(Category) ? array[0] : (Category + "/" + array[0]);
                    bool   flag2 = false;
                    if (Term.EndsWith("[touch]"))
                    {
                        Term  = Term.Remove(Term.Length - "[touch]".Length);
                        flag2 = true;
                    }
                    ValidateFullTerm(ref Term);
                    if (string.IsNullOrEmpty(Term))
                    {
                        continue;
                    }
                    TermData termData2 = GetTermData(Term);
                    if (termData2 == null)
                    {
                        termData2                 = new TermData();
                        termData2.Term            = Term;
                        termData2.Languages       = new string[mLanguages.Count];
                        termData2.Languages_Touch = new string[mLanguages.Count];
                        termData2.Flags           = new byte[mLanguages.Count];
                        for (int l = 0; l < mLanguages.Count; l++)
                        {
                            termData2.Languages[l] = (termData2.Languages_Touch[l] = string.Empty);
                        }
                        mTerms.Add(termData2);
                        mDictionary.Add(Term, termData2);
                    }
                    else if (UpdateMode == eSpreadsheetUpdateMode.AddNewTerms)
                    {
                        continue;
                    }
                    if (num2 > 0)
                    {
                        termData2.TermType = GetTermType(array[num2]);
                    }
                    if (num3 > 0)
                    {
                        termData2.Description = array[num3];
                    }
                    for (int m = 0; m < array2.Length && m < array.Length - num; m++)
                    {
                        if (string.IsNullOrEmpty(array[m + num]))
                        {
                            continue;
                        }
                        int num6 = array2[m];
                        if (num6 >= 0)
                        {
                            string text2 = array[m + num];
                            if (flag2)
                            {
                                termData2.Languages_Touch[num6] = text2;
                                termData2.Flags[num6]          &= 253;
                            }
                            else
                            {
                                termData2.Languages[num6] = text2;
                                termData2.Flags[num6]    &= 254;
                            }
                        }
                    }
                }
                return(string.Empty);
            }
            return("Bad Spreadsheet Format.\nFirst columns should be 'Key', 'Type' and 'Desc'");
        }
        public string Import_CSV(string Category, List <string[]> CSV, eSpreadsheetUpdateMode UpdateMode = eSpreadsheetUpdateMode.Replace)
        {
            string[] Tokens = CSV[0];

            int LanguagesStartIdx = 1;
            int TypeColumnIdx     = -1;
            int DescColumnIdx     = -1;

            var ValidColumnName_Key  = new string[] { "Key" };
            var ValidColumnName_Type = new string[] { "Type" };
            var ValidColumnName_Desc = new string[] { "Desc", "Description" };

            if (Tokens.Length > 1 && ArrayContains(Tokens[0], ValidColumnName_Key))
            {
                if (UpdateMode == eSpreadsheetUpdateMode.Replace)
                {
                    ClearAllData();
                }

                if (Tokens.Length > 2)
                {
                    if (ArrayContains(Tokens[1], ValidColumnName_Type))
                    {
                        TypeColumnIdx     = 1;
                        LanguagesStartIdx = 2;
                    }
                    if (ArrayContains(Tokens[1], ValidColumnName_Desc))
                    {
                        DescColumnIdx     = 1;
                        LanguagesStartIdx = 2;
                    }
                }
                if (Tokens.Length > 3)
                {
                    if (ArrayContains(Tokens[2], ValidColumnName_Type))
                    {
                        TypeColumnIdx     = 2;
                        LanguagesStartIdx = 3;
                    }
                    if (ArrayContains(Tokens[2], ValidColumnName_Desc))
                    {
                        DescColumnIdx     = 2;
                        LanguagesStartIdx = 3;
                    }
                }
            }
            else
            {
                return("Bad Spreadsheet Format.\nFirst columns should be 'Key', 'Type' and 'Desc'");
            }

            int nLanguages = Mathf.Max(Tokens.Length - LanguagesStartIdx, 0);

            int[] LanIndices = new int[nLanguages];
            for (int i = 0; i < nLanguages; ++i)
            {
                if (string.IsNullOrEmpty(Tokens[i + LanguagesStartIdx]))
                {
                    LanIndices [i] = -1;
                    continue;
                }

                string LanName, LanCode;
                GoogleLanguages.UnPackCodeFromLanguageName(Tokens[i + LanguagesStartIdx], out LanName, out LanCode);

                int LanIdx = -1;
                if (!string.IsNullOrEmpty(LanCode))
                {
                    LanIdx = GetLanguageIndexFromCode(LanCode);
                }
                else
                {
                    LanIdx = GetLanguageIndex(LanName);
                }

                if (LanIdx < 0)
                {
                    LanguageData lanData = new LanguageData();
                    lanData.Name = LanName;
                    lanData.Code = LanCode;
                    mLanguages.Add(lanData);
                    LanIdx = mLanguages.Count - 1;
                }
                LanIndices[i] = LanIdx;
            }

            //--[ Update the Languages array in the existing terms]-----
            nLanguages = mLanguages.Count;
            for (int i = 0, imax = mTerms.Count; i < imax; ++i)
            {
                TermData termData = mTerms[i];
                if (termData.Languages.Length < nLanguages)
                {
                    Array.Resize(ref termData.Languages, nLanguages);
                    Array.Resize(ref termData.Languages_Touch, nLanguages);
                    Array.Resize(ref termData.Flags, nLanguages);
                }
            }

            //--[ Keys ]--------------

            for (int i = 1, imax = CSV.Count; i < imax; ++i)
            {
                Tokens = CSV[i];
                string sKey    = string.IsNullOrEmpty(Category) ? Tokens[0] : string.Concat(Category, "/", Tokens[0]);
                bool   isTouch = false;
                if (sKey.EndsWith("[touch]"))
                {
                    sKey    = sKey.Remove(sKey.Length - "[touch]".Length);
                    isTouch = true;
                }
                LanguageSource.ValidateFullTerm(ref sKey);
                if (string.IsNullOrEmpty(sKey))
                {
                    continue;
                }

                TermData termData = GetTermData(sKey);

                // Check to see if its a new term
                if (termData == null)
                {
                    termData      = new TermData();
                    termData.Term = sKey;

                    termData.Languages       = new string[mLanguages.Count];
                    termData.Languages_Touch = new string[mLanguages.Count];
                    termData.Flags           = new byte[mLanguages.Count];
                    for (int j = 0; j < mLanguages.Count; ++j)
                    {
                        termData.Languages[j] = termData.Languages_Touch[j] = string.Empty;
                    }

                    mTerms.Add(termData);
                    mDictionary.Add(sKey, termData);
                }
                else
                // This term already exist
                if (UpdateMode == eSpreadsheetUpdateMode.AddNewTerms)
                {
                    continue;
                }

                if (TypeColumnIdx > 0)
                {
                    termData.TermType = GetTermType(Tokens[TypeColumnIdx]);
                }

                if (DescColumnIdx > 0)
                {
                    termData.Description = Tokens[DescColumnIdx];
                }

                for (int j = 0; j < LanIndices.Length && j < Tokens.Length - LanguagesStartIdx; ++j)
                {
                    if (!string.IsNullOrEmpty(Tokens[j + LanguagesStartIdx]))                   // Only change the translation if there is a new value
                    {
                        var lanIdx = LanIndices[j];
                        if (lanIdx < 0)
                        {
                            continue;
                        }
                        var value = Tokens[j + LanguagesStartIdx];
                        //var isAuto = false;// value.Contains("[i2auto]");
                        //if (isAuto)
                        //value = value.Replace ("[i2auto]", string.Empty);

                        //if (value=="-")
                        //	value = string.Empty;

                        if (isTouch)
                        {
                            termData.Languages_Touch[lanIdx] = value;

                            /*if (isAuto)  termData.Flags[lanIdx] |= (byte)TranslationFlag.AutoTranslated_Touch;
                             *              else */termData.Flags[lanIdx] &= byte.MaxValue ^ ((byte)TranslationFlag.AutoTranslated_Touch);
                        }
                        else
                        {
                            termData.Languages[lanIdx] = value;

                            /*if (isAuto)  termData.Flags[lanIdx] |= (byte)TranslationFlag.AutoTranslated_Normal;
                             *              else*/termData.Flags[lanIdx] &= byte.MaxValue ^ ((byte)TranslationFlag.AutoTranslated_Normal);
                        }
                    }
                }
            }

            return(string.Empty);
        }
Example #3
0
        public string Import_CSV(string Category, List <string[]> CSV, eSpreadsheetUpdateMode UpdateMode = eSpreadsheetUpdateMode.Replace)
        {
            string[] Tokens = CSV[0];

            int LanguagesStartIdx = 1;
            int TypeColumnIdx     = -1;
            int DescColumnIdx     = -1;

            var ValidColumnName_Key  = new string[] { "Key" };
            var ValidColumnName_Type = new string[] { "Type" };
            var ValidColumnName_Desc = new string[] { "Desc", "Description" };

            if (Tokens.Length > 1 && ArrayContains(Tokens[0], ValidColumnName_Key))
            {
                if (UpdateMode == eSpreadsheetUpdateMode.Replace)
                {
                    ClearAllData();
                }

                if (Tokens.Length > 2)
                {
                    if (ArrayContains(Tokens[1], ValidColumnName_Type))
                    {
                        TypeColumnIdx     = 1;
                        LanguagesStartIdx = 2;
                    }
                    if (ArrayContains(Tokens[1], ValidColumnName_Desc))
                    {
                        DescColumnIdx     = 1;
                        LanguagesStartIdx = 2;
                    }
                }
                if (Tokens.Length > 3)
                {
                    if (ArrayContains(Tokens[2], ValidColumnName_Type))
                    {
                        TypeColumnIdx     = 2;
                        LanguagesStartIdx = 3;
                    }
                    if (ArrayContains(Tokens[2], ValidColumnName_Desc))
                    {
                        DescColumnIdx     = 2;
                        LanguagesStartIdx = 3;
                    }
                }
            }
            else
            {
                return("Bad Spreadsheet Format.\nFirst columns should be 'Key', 'Type' and 'Desc'");
            }

            int nLanguages = Mathf.Max(Tokens.Length - LanguagesStartIdx, 0);

            int[] LanIndices = new int[nLanguages];
            for (int i = 0; i < nLanguages; ++i)
            {
                if (string.IsNullOrEmpty(Tokens[i + LanguagesStartIdx]))
                {
                    LanIndices [i] = -1;
                    continue;
                }

                string langToken = Tokens[i + LanguagesStartIdx];

                string LanName, LanCode;
                bool   isLangEnabled = true;
                if (langToken.StartsWith("$"))
                {
                    isLangEnabled = false;
                    langToken     = langToken.Substring(1);
                }
                GoogleLanguages.UnPackCodeFromLanguageName(langToken, out LanName, out LanCode);

                int LanIdx = -1;
                if (!string.IsNullOrEmpty(LanCode))
                {
                    LanIdx = GetLanguageIndexFromCode(LanCode);
                }
                else
                {
                    LanIdx = GetLanguageIndex(LanName, SkipDisabled: false);
                }

                if (LanIdx < 0)
                {
                    LanguageData lanData = new LanguageData();
                    lanData.Name  = LanName;
                    lanData.Code  = LanCode;
                    lanData.Flags = (byte)(0 | (isLangEnabled?0:(int)eLanguageDataFlags.DISABLED));
                    mLanguages.Add(lanData);
                    LanIdx = mLanguages.Count - 1;
                }
                LanIndices[i] = LanIdx;
            }

            //--[ Update the Languages array in the existing terms]-----
            nLanguages = mLanguages.Count;
            for (int i = 0, imax = mTerms.Count; i < imax; ++i)
            {
                TermData termData = mTerms[i];
                if (termData.Languages.Length < nLanguages)
                {
                    Array.Resize(ref termData.Languages, nLanguages);
                    Array.Resize(ref termData.Flags, nLanguages);
                }
            }

            //--[ Keys ]--------------

            for (int i = 1, imax = CSV.Count; i < imax; ++i)
            {
                Tokens = CSV[i];
                string sKey = string.IsNullOrEmpty(Category) ? Tokens[0] : string.Concat(Category, "/", Tokens[0]);

                string specialization = null;
                if (sKey.EndsWith("]"))
                {
                    int idx = sKey.LastIndexOf('[');
                    if (idx > 0)
                    {
                        specialization = sKey.Substring(idx + 1, sKey.Length - idx - 2);
                        if (specialization == "touch")
                        {
                            specialization = "Touch";
                        }
                        sKey = sKey.Remove(idx);
                    }
                }
                ValidateFullTerm(ref sKey);
                if (string.IsNullOrEmpty(sKey))
                {
                    continue;
                }

                TermData termData = GetTermData(sKey);

                // Check to see if its a new term
                if (termData == null)
                {
                    termData      = new TermData();
                    termData.Term = sKey;

                    termData.Languages = new string[mLanguages.Count];
                    termData.Flags     = new byte[mLanguages.Count];
                    for (int j = 0; j < mLanguages.Count; ++j)
                    {
                        termData.Languages[j] = string.Empty;
                    }

                    mTerms.Add(termData);
                    mDictionary.Add(sKey, termData);
                }
                else
                // This term already exist
                if (UpdateMode == eSpreadsheetUpdateMode.AddNewTerms)
                {
                    continue;
                }

                if (TypeColumnIdx > 0)
                {
                    termData.TermType = GetTermType(Tokens[TypeColumnIdx]);
                }

                if (DescColumnIdx > 0)
                {
                    termData.Description = Tokens[DescColumnIdx];
                }

                for (int j = 0; j < LanIndices.Length && j < Tokens.Length - LanguagesStartIdx; ++j)
                {
                    if (!string.IsNullOrEmpty(Tokens[j + LanguagesStartIdx]))   // Only change the translation if there is a new value
                    {
                        var lanIdx = LanIndices[j];
                        if (lanIdx < 0)
                        {
                            continue;
                        }
                        var value = Tokens[j + LanguagesStartIdx];

                        if (value == "-")
                        {
                            value = string.Empty;
                        }
                        else
                        if (value == "")
                        {
                            value = null;
                        }

                        termData.SetTranslation(lanIdx, value, specialization);
                    }
                }
            }

            return(string.Empty);
        }