public WWW Import_Google_CreateWWWcall(bool ForceUpdate, bool justCheck) { if (!HasGoogleSpreadsheet()) { return(null); } string savedVersion = PersistentStorage.GetSetting_String("I2SourceVersion_" + GetSourcePlayerPrefName(), Google_LastUpdatedVersion); if (savedVersion.Length > 19) // Check for corruption { savedVersion = string.Empty; } #if !UNITY_EDITOR if (IsNewerVersion(savedVersion, Google_LastUpdatedVersion)) { Google_LastUpdatedVersion = savedVersion; } #endif string query = string.Format("{0}?key={1}&action=GetLanguageSource&version={2}", LocalizationManager.GetWebServiceURL(this), Google_SpreadsheetKey, ForceUpdate ? "0" : Google_LastUpdatedVersion); #if UNITY_EDITOR if (justCheck) { query += "&justcheck=true"; } #endif WWW www = new WWW(query); return(www); }
public void SaveLanguages(bool unloadAll, PersistentStorage.eFileType fileLocation = PersistentStorage.eFileType.Temporal) { if (!AllowUnloadingLanguages()) { return; } // Some consoles don't allow IO access if (!PersistentStorage.CanAccessFiles()) { return; } for (int i = 0; i < mLanguages.Count; ++i) { var data = Export_Language_to_Cache(i, IsCurrentLanguage(i)); if (string.IsNullOrEmpty(data)) { continue; } PersistentStorage.SaveFile(PersistentStorage.eFileType.Temporal, GetSavedLanguageFileName(i), data); } if (unloadAll) { for (int i = 0; i < mLanguages.Count; ++i) { if (unloadAll && !IsCurrentLanguage(i)) { UnloadLanguage(i); } } } }
public void UnloadLanguage(int languageIndex) { if (!AllowUnloadingLanguages()) { return; } // Some consoles don't allow IO access if (!PersistentStorage.CanAccessFiles()) { return; } if (!I2Utils.IsPlaying() || !mLanguages[languageIndex].IsLoaded() || !mLanguages[languageIndex].CanBeUnloaded() || IsCurrentLanguage(languageIndex) || !PersistentStorage.HasFile(PersistentStorage.eFileType.Temporal, GetSavedLanguageFileName(languageIndex))) { return; } foreach (var termData in mTerms) { termData.Languages[languageIndex] = null; } mLanguages[languageIndex].SetLoaded(false); }
public void LoadLanguage(int languageIndex, bool UnloadOtherLanguages, bool useFallback, bool onlyCurrentSpecialization, bool forceLoad) { if (!AllowUnloadingLanguages()) { return; } // Some consoles don't allow IO access if (!PersistentStorage.CanAccessFiles()) { return; } if (languageIndex >= 0 && (forceLoad || !mLanguages[languageIndex].IsLoaded())) { var tempPath = GetSavedLanguageFileName(languageIndex); var langData = PersistentStorage.LoadFile(PersistentStorage.eFileType.Temporal, tempPath, false); if (!string.IsNullOrEmpty(langData)) { Import_Language_from_Cache(languageIndex, langData, useFallback, onlyCurrentSpecialization); mLanguages[languageIndex].SetLoaded(true); } } if (UnloadOtherLanguages && I2Utils.IsPlaying()) { for (int lan = 0; lan < mLanguages.Count; ++lan) { if (lan != languageIndex) { UnloadLanguage(lan); } } } }
public void Import_Google_FromCache() { if (GoogleUpdateFrequency == eGoogleUpdateFrequency.Never) { return; } if (!I2Utils.IsPlaying()) { return; } string PlayerPrefName = GetSourcePlayerPrefName(); string I2SavedData = PersistentStorage.LoadFile(PersistentStorage.eFileType.Persistent, "I2Source_" + PlayerPrefName + ".loc", false); if (string.IsNullOrEmpty(I2SavedData)) { return; } if (I2SavedData.StartsWith("[i2e]", StringComparison.Ordinal)) { I2SavedData = StringObfucator.Decode(I2SavedData.Substring(5, I2SavedData.Length - 5)); } //--[ Compare with current version ]----- bool shouldUpdate = false; string savedSpreadsheetVersion = Google_LastUpdatedVersion; if (PersistentStorage.HasSetting("I2SourceVersion_" + PlayerPrefName)) { savedSpreadsheetVersion = PersistentStorage.GetSetting_String("I2SourceVersion_" + PlayerPrefName, Google_LastUpdatedVersion); // Debug.Log (Google_LastUpdatedVersion + " - " + savedSpreadsheetVersion); shouldUpdate = IsNewerVersion(Google_LastUpdatedVersion, savedSpreadsheetVersion); } if (!shouldUpdate) { PersistentStorage.DeleteFile(PersistentStorage.eFileType.Persistent, "I2Source_" + PlayerPrefName + ".loc", false); PersistentStorage.DeleteSetting("I2SourceVersion_" + PlayerPrefName); return; } if (savedSpreadsheetVersion.Length > 19) // Check for corruption from previous versions { savedSpreadsheetVersion = string.Empty; } Google_LastUpdatedVersion = savedSpreadsheetVersion; //Debug.Log ("[I2Loc] Using Saved (PlayerPref) data in 'I2Source_"+PlayerPrefName+"'" ); Import_Google_Result(I2SavedData, eSpreadsheetUpdateMode.Replace); }
static void SelectStartupLanguage() { if (Sources.Count == 0) { return; } // Use the system language if there is a source with that language, // or pick any of the languages provided by the sources string SavedLanguage = PersistentStorage.GetSetting_String("I2 Language", string.Empty); string SysLanguage = GetCurrentDeviceLanguage(); // Try selecting the System Language // But fallback to the first language found if the System Language is not available in any source if (!string.IsNullOrEmpty(SavedLanguage) && HasLanguage(SavedLanguage, Initialize: false, SkipDisabled: true)) { SetLanguageAndCode(SavedLanguage, GetLanguageCode(SavedLanguage)); return; } if (!Sources [0].IgnoreDeviceLanguage) { // Check if the device language is supported. // Also recognize when not region is set ("English (United State") will be used if sysLanguage is "English") string ValidLanguage = GetSupportedLanguage(SysLanguage, true); if (!string.IsNullOrEmpty(ValidLanguage)) { SetLanguageAndCode(ValidLanguage, GetLanguageCode(ValidLanguage), false); return; } } //--[ Use first language that its not disabled ]----------- for (int i = 0, imax = Sources.Count; i < imax; ++i) { if (Sources[i].mLanguages.Count > 0) { for (int j = 0; j < Sources[i].mLanguages.Count; ++j) { if (Sources[i].mLanguages[j].IsEnabled()) { SetLanguageAndCode(Sources[i].mLanguages[j].Name, Sources[i].mLanguages[j].Code, false); return; } } } } }
public void UnloadLanguage(int languageIndex) { if (!I2Utils.IsPlaying() || !mLanguages[languageIndex].IsLoaded() || !mLanguages[languageIndex].CanBeUnloaded() || IsCurrentLanguage(languageIndex) || !PersistentStorage.HasFile(GetSavedLanguageFileName(languageIndex))) { return; } foreach (var termData in mTerms) { termData.Languages[languageIndex] = termData.Languages_Touch[languageIndex] = null; } mLanguages[languageIndex].SetLoaded(false); }
public static bool HasJoinedWords = false; // Some languages (e.g. Chinese, Japanese and Thai) don't add spaces to their words (all characters are placed toguether) #endregion public static void SetLanguageAndCode(string LanguageName, string LanguageCode, bool RememberLanguage = true, bool Force = false) { if (mCurrentLanguage != LanguageName || mLanguageCode != LanguageCode || Force) { if (RememberLanguage) { PersistentStorage.SetSetting_String("I2 Language", LanguageName); } mCurrentLanguage = LanguageName; mLanguageCode = LanguageCode; mCurrentCulture = CreateCultureForCode(LanguageCode); if (mChangeCultureInfo) { SetCurrentCultureInfo(); } IsRight2Left = IsRTL(mLanguageCode); HasJoinedWords = GoogleLanguages.LanguageCode_HasJoinedWord(mLanguageCode); LocalizeAll(Force); } }
public void SaveLanguages(bool unloadAll) { for (int i = 0; i < mLanguages.Count; ++i) { var data = Export_Language(i); if (string.IsNullOrEmpty(data)) { continue; } PersistentStorage.SaveFile(GetSavedLanguageFileName(i), data); } if (unloadAll) { for (int i = 0; i < mLanguages.Count; ++i) { if (unloadAll && !IsCurrentLanguage(i)) { UnloadLanguage(i); } } } }
public void LoadLanguage(int languageIndex, bool UnloadOtherLanguages, bool useFallback) { if (languageIndex >= 0 && !mLanguages[languageIndex].IsLoaded()) { var tempPath = GetSavedLanguageFileName(languageIndex); var langData = PersistentStorage.LoadFile(tempPath); if (!string.IsNullOrEmpty(langData)) { Import_Language(languageIndex, langData, useFallback); mLanguages[languageIndex].SetLoaded(true); } } if (UnloadOtherLanguages && I2Utils.IsPlaying()) { for (int lan = 0; lan < mLanguages.Count; ++lan) { if (lan != languageIndex) { UnloadLanguage(lan); } } } }
public string Import_Google_Result(string JsonString, eSpreadsheetUpdateMode UpdateMode, bool saveInPlayerPrefs = false) { try { string ErrorMsg = string.Empty; if (string.IsNullOrEmpty(JsonString) || JsonString == "\"\"") { return(ErrorMsg); } int idxV = JsonString.IndexOf("version=", StringComparison.Ordinal); int idxSV = JsonString.IndexOf("script_version=", StringComparison.Ordinal); if (idxV < 0 || idxSV < 0) { return("Invalid Response from Google, Most likely the WebService needs to be updated"); } idxV += "version=".Length; idxSV += "script_version=".Length; string newSpreadsheetVersion = JsonString.Substring(idxV, JsonString.IndexOf(",", idxV, StringComparison.Ordinal) - idxV); var scriptVersion = int.Parse(JsonString.Substring(idxSV, JsonString.IndexOf(",", idxSV, StringComparison.Ordinal) - idxSV)); if (newSpreadsheetVersion.Length > 19) // Check for corruption { newSpreadsheetVersion = string.Empty; } if (scriptVersion != LocalizationManager.GetRequiredWebServiceVersion()) { return("The current Google WebService is not supported.\nPlease, delete the WebService from the Google Drive and Install the latest version."); } //Debug.Log (Google_LastUpdatedVersion + " - " + newSpreadsheetVersion); if (saveInPlayerPrefs && !IsNewerVersion(Google_LastUpdatedVersion, newSpreadsheetVersion)) #if UNITY_EDITOR { return(""); } #else { return("LanguageSource is up-to-date"); } #endif if (saveInPlayerPrefs) { string PlayerPrefName = GetSourcePlayerPrefName(); PersistentStorage.SaveFile(PersistentStorage.eFileType.Persistent, "I2Source_" + PlayerPrefName, "[i2e]" + StringObfucator.Encode(JsonString) + ".loc"); PersistentStorage.SetSetting_String("I2SourceVersion_" + PlayerPrefName, newSpreadsheetVersion); PersistentStorage.ForceSaveSettings(); } Google_LastUpdatedVersion = newSpreadsheetVersion; if (UpdateMode == eSpreadsheetUpdateMode.Replace) { ClearAllData(); } int CSVstartIdx = JsonString.IndexOf("[i2category]", StringComparison.Ordinal); while (CSVstartIdx > 0) { CSVstartIdx += "[i2category]".Length; int endCat = JsonString.IndexOf("[/i2category]", CSVstartIdx, StringComparison.Ordinal); string category = JsonString.Substring(CSVstartIdx, endCat - CSVstartIdx); endCat += "[/i2category]".Length; int endCSV = JsonString.IndexOf("[/i2csv]", endCat, StringComparison.Ordinal); string csv = JsonString.Substring(endCat, endCSV - endCat); CSVstartIdx = JsonString.IndexOf("[i2category]", endCSV, StringComparison.Ordinal); Import_I2CSV(category, csv, UpdateMode); // Only the first CSV should clear the Data if (UpdateMode == eSpreadsheetUpdateMode.Replace) { UpdateMode = eSpreadsheetUpdateMode.Merge; } } if (I2Utils.IsPlaying()) { SaveLanguages(true); } #if UNITY_EDITOR if (!string.IsNullOrEmpty(ErrorMsg)) { UnityEditor.EditorUtility.SetDirty(this); } #endif return(ErrorMsg); } catch (System.Exception e) { Debug.LogWarning(e); return(e.ToString()); } }
// When JustCheck is true, importing from google will not download any data, just detect if the Spreadsheet is up-to-date public void Import_Google(bool ForceUpdate, bool justCheck) { if (!ForceUpdate && GoogleUpdateFrequency == eGoogleUpdateFrequency.Never) { return; } if (!I2Utils.IsPlaying()) { return; } #if UNITY_EDITOR if (justCheck && GoogleInEditorCheckFrequency == eGoogleUpdateFrequency.Never) { return; } #endif #if UNITY_EDITOR var updateFrequency = GoogleInEditorCheckFrequency; #else var updateFrequency = GoogleUpdateFrequency; #endif string PlayerPrefName = GetSourcePlayerPrefName(); if (!ForceUpdate && updateFrequency != eGoogleUpdateFrequency.Always) { #if UNITY_EDITOR string sTimeOfLastUpdate = UnityEditor.EditorPrefs.GetString("LastGoogleUpdate_" + PlayerPrefName, ""); #else string sTimeOfLastUpdate = PersistentStorage.GetSetting_String("LastGoogleUpdate_" + PlayerPrefName, ""); #endif DateTime TimeOfLastUpdate; try { if (DateTime.TryParse(sTimeOfLastUpdate, out TimeOfLastUpdate)) { double TimeDifference = (DateTime.Now - TimeOfLastUpdate).TotalDays; switch (updateFrequency) { case eGoogleUpdateFrequency.Daily: if (TimeDifference < 1) { return; } break; case eGoogleUpdateFrequency.Weekly: if (TimeDifference < 8) { return; } break; case eGoogleUpdateFrequency.Monthly: if (TimeDifference < 31) { return; } break; case eGoogleUpdateFrequency.OnlyOnce: return; } } } catch (Exception) { } } #if UNITY_EDITOR UnityEditor.EditorPrefs.SetString("LastGoogleUpdate_" + PlayerPrefName, DateTime.Now.ToString()); #else PersistentStorage.SetSetting_String("LastGoogleUpdate_" + PlayerPrefName, DateTime.Now.ToString()); #endif //--[ Checking google for updated data ]----------------- CoroutineManager.Start(Import_Google_Coroutine(justCheck)); }