/// <inheritdoc /> public bool RemoveLocalizationData(LocalizationData data) { if (!GetCapabilities().Contains(data.languageIETF)) { return(false); } LocalizationData subtractionTarget = null; foreach (LocalizationData dataTarget in loadedData) { if (data.languageIETF != dataTarget.languageIETF) { continue; } subtractionTarget = dataTarget; break; } if (subtractionTarget == null) { return(false); } return(subtractionTarget.SubtractData(data)); }
/// <summary> /// Removes data using another source as reference. /// </summary> /// <param name="data">Target reference data.</param> /// <returns>True if succeeds, False otherwise.</returns> public bool SubtractData(LocalizationData data) { //check if language is the same if (languageIETF != data.languageIETF) { return(false); } //check if data contains all the keys foreach (KeyValuePair <string, string> kvp in data.stringData) { if (!stringData.ContainsKey(kvp.Key)) { return(false); } } //for each key to remove foreach (KeyValuePair <string, string> kvp in data.stringData) { if (--dataRefCount[kvp.Key] == 0) { stringData.Remove(kvp.Key); } } return(true); }
/// <inheritdoc /> public async void LoadLocalizationFile(string url) { //check if file is actual json file if (!url.EndsWith(".json")) { return; } WWW target = new WWW(url); while (!target.isDone) { await Task.Delay(1000); } string[] parts = url.Split(new [] { "." }, StringSplitOptions.None); LocalizationData data = ScriptableObject.CreateInstance <LocalizationData>(); CultureInfo info = CultureInfo.GetCultureInfoByIetfLanguageTag(parts[parts.Length - 2]); data.languageIETF = info.IetfLanguageTag; data.languageDescriptor = info.ThreeLetterISOLanguageName; data.FromJSON(target.text); AddLocalizationData(data); }
/// <inheritdoc /> public bool AddLocalizationData(LocalizationData data) { LocalizationData mergeTarget = null; if (GetCapabilities().Contains(data.languageIETF)) { foreach (LocalizationData dataTarget in loadedData) { if (data.languageIETF != dataTarget.languageIETF) { continue; } mergeTarget = dataTarget; break; } } else { mergeTarget = ScriptableObject.CreateInstance <LocalizationData>(); mergeTarget.languageDescriptor = data.languageDescriptor; mergeTarget.languageIETF = data.languageIETF; loadedData.Add(mergeTarget); } if (mergeTarget == null) { return(false); } return(mergeTarget.AddData(data)); }
// Initialization function to restore values from serialized object private void Intialize() { foreach (LocalizationData data in baseLocalization) { AddLocalizationData(data); } foreach (LocalizationData data in loadedData) { if (data.languageIETF == _currentLocalization) { currentLocalizationData = data; if (eventBroker != null) { eventBroker.Invoke(new LocalizationChangedArgs() { ietf = _currentLocalization, localeDecorator = currentLocalizationData.languageDescriptor }); } } } if (currentLocalizationData == null) { AciLog.LogFormat(LogType.Error, "LocalizationManager", "Target language \"{0}\" could not be loaded from persivously serialzed values. Please check if the correct files are set on the LocalizationManager instance.", _currentLocalization); _currentLocalization = null; } initialized = true; }
void ImportLocalizationJSON() { string file = EditorUtility.OpenFilePanelWithFilters("Import Localization from JSON...", Application.dataPath, new [] { "JSON localization file", "json" }); LocalizationData data = (LocalizationData)target; string url = "file://" + file; //check if file is actual json file if (!url.EndsWith(".json")) { return; } WWW targetFile = new WWW(url); while (!targetFile.isDone) { } string[] parts = url.Split(new[] { ".", "/" }, StringSplitOptions.None); CultureInfo info = CultureInfo.GetCultureInfoByIetfLanguageTag(parts[parts.Length - 2]); data.languageIETF = info.IetfLanguageTag; data.languageDescriptor = info.ThreeLetterISOLanguageName; data.FromJSON(targetFile.text); }
void ExportLocalizationJSON() { string file = EditorUtility.SaveFilePanel("Export Localization to JSON...", Application.dataPath, target.name, "json"); LocalizationData data = (LocalizationData)target; using (FileStream fs = new FileStream(file, FileMode.OpenOrCreate, FileAccess.Write)) { using (StreamWriter sw = new StreamWriter(fs)) sw.Write(data.ToJSON()); } }
/// <summary> /// Adds data from another data source to this instance. /// </summary> /// <param name="data">Target data to incorporate into </param> /// <returns>True if succeeds, False otherwise.</returns> public bool AddData(LocalizationData data) { //check if language is the same if (languageIETF != data.languageIETF) { return(false); } //for each string foreach (KeyValuePair <string, string> kvp in data.stringData) { //if string does not exist if (!stringData.ContainsKey(kvp.Key)) { //add string stringData[kvp.Key] = kvp.Value; //add ref count dataRefCount[kvp.Key] = 0; } //increment ref count ++dataRefCount[kvp.Key]; } return(true); }
/// <inheritdoc /> public void ClearData() { loadedData.Clear(); currentLocalizationData = null; }
private void ManageIdentifierLocalizationBindings() { LocalizationData data = (LocalizationData)target; bindingsShown = EditorGUILayout.Foldout(bindingsShown, new GUIContent("Localized Strings"), true); if (!bindingsShown) { return; } SerializedProperty bufferProp = internalStringData.Copy(); SerializedProperty keysProperty = null; SerializedProperty valuesProperty = null; SerializedProperty endProperty = bufferProp.GetEndProperty(); while (bufferProp.NextVisible(true)) { if (SerializedProperty.EqualContents(bufferProp, endProperty)) { break; } if (bufferProp.name == "keys") { keysProperty = bufferProp.Copy(); } if (bufferProp.name == "values") { valuesProperty = bufferProp.Copy(); } } Vector2 removeButtonSize = GUIStyle.none.CalcSize(iconToolbarMinus); Vector2 addButtonSize = GUIStyle.none.CalcSize(iconToolbarPlus); // Decorators EditorGUILayout.BeginHorizontal(); float curWidth = EditorGUIUtility.currentViewWidth - 10 * 2 - removeButtonSize.x - removeButtonSize.x; GUILayout.Space(removeButtonSize.x + 2); GUILayout.Label("String Identifier", GUILayout.Width(curWidth * 0.5f - 5)); GUILayout.Label("Localized String Value", GUILayout.Width(curWidth * 0.5f - 5)); if (GUILayout.Button(iconToolbarPlus, iconStyle)) { // new element here keysProperty.InsertArrayElementAtIndex(keysProperty.arraySize); SerializedProperty newProp = keysProperty.GetArrayElementAtIndex(keysProperty.arraySize - 1); string newValue = "newValue"; int count = 0; while (data.stringData.ContainsKey(newValue)) { newValue = "newValue" + ++count; } newProp.stringValue = newValue; valuesProperty.InsertArrayElementAtIndex(valuesProperty.arraySize); } EditorGUILayout.EndHorizontal(); // Elements int toBeRemovedEntry = -1; Rect stringsRect = EditorGUILayout.BeginVertical(); for (int i = 0; i < keysProperty.arraySize; ++i) { Rect kvpRect = EditorGUILayout.BeginHorizontal(); GUILayout.Button(iconPause, iconStyle); SerializedProperty keyProperty = keysProperty.GetArrayElementAtIndex(i); SerializedProperty valueProperty = valuesProperty.GetArrayElementAtIndex(i); string newValue = EditorGUILayout.TextField(keyProperty.stringValue, GUILayout.Width(curWidth * 0.5f - 5)); if (!data.stringData.ContainsKey(newValue)) { keyProperty.stringValue = newValue; } valueProperty.stringValue = EditorGUILayout.TextField(valueProperty.stringValue, GUILayout.Width(curWidth * 0.5f - 5)); if (GUILayout.Button(iconToolbarMinus, iconStyle)) { toBeRemovedEntry = i; } EditorGUILayout.EndHorizontal(); } // remove Elements if (toBeRemovedEntry > -1) { keysProperty.DeleteArrayElementAtIndex(toBeRemovedEntry); valuesProperty.DeleteArrayElementAtIndex(toBeRemovedEntry); } EditorGUILayout.EndVertical(); }