async Task TranslateToCurrentLanguage(LocalizationVar loc) { string langKey = this._languageKey.ToLower(); string originalText = loc.Original; string url = string.Format("https://translate.googleapis.com/translate_a/single?client=gtx&sl={0}&tl={1}&dt=t&q={2}", "en", langKey, UnityWebRequest.EscapeURL(originalText)); var req = UnityWebRequest.Get(url); req.SetRequestHeader("user-agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 " + "(KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36"); await req.SendWebRequest(); if (req.isNetworkError || req.isHttpError) { /*Debug.Log(req.GetResponseHeader("Retry-After"));*/ Debug.LogError(req.error); var responseHeaders = req.GetResponseHeaders(); foreach (var responH in responseHeaders) { Debug.Log(responH.Key + ": " + responH.Value); } throw new System.Exception(); } else { //Debug.Log(req.downloadHandler.text); loc.LocalizedText = this.ExtractTranslationFromJson(req.downloadHandler.text, langKey); Debug.Log(originalText + " => " + loc.LocalizedText); } }
public void CreateLocAsset() { string name = this._label.text.Replace(" ", "_").Replace("?", "").Replace("!", "").Replace(".", ""); if (name.Contains("\n")) { name = name.Substring(0, name.IndexOf("\n")); } name = "Localization - " + name; string originalText = this._label.text; string hint = ""; if (this._label.GetComponentInParent <Button>()) { hint = "UI Button"; } else if (this._label.name.Contains("Title") || this._label.transform.parent.name.Contains("Title")) { hint = "UI Title"; } this._localization = LocalizationManager.GetOrCreateLocAsset(name, hint, originalText, false);; UnityEditor.EditorUtility.SetDirty(this); //UnityEditor.AssetDatabase.SaveAssets(); }
public void EnsureLocVarIntegration(LocalizationVar loc) { if (!this._localizationVars.Contains(loc)) { this._localizationVars.Add(loc); #if UNITY_EDITOR Debug.Log("Added LocVar: " + loc.name); this.BumpVersion(); UnityEditor.EditorUtility.SetDirty(loc); #endif //TODO add runtime integration support } }
public static LocalizationVar GetOrCreateLocAsset(string locName, string hint, string originalText, bool forceCreate) { LocalizationManager locMan = (LocalizationManager)UnityEditor.AssetDatabase.LoadAssetAtPath("Assets/Data/Localization/_LocalizationManager.asset", typeof(LocalizationManager)); while (locMan._localizationVars.Remove(null)) { } LocalizationVar loc = locMan._localizationVars.Find(l => /*l.Hint == hint && l.Original == originalText &&*/ l && l.name == locName); uint nextID = locMan._localizationVars.Max(lv => lv.Id) + 1; if (loc != null) { if (forceCreate || loc.Original != originalText) { loc = null; locName = UnityEditor.AssetDatabase.GenerateUniqueAssetPath("Assets/Data/Localization/" + locName + ".asset"); locName = locName.Substring(locName.LastIndexOf('/') + 1); locName = locName.Substring(0, locName.Length - 6); } else if (!loc.Hint.Contains(hint)) { loc.Hint += "; " + hint; locMan.BumpVersion(); } } if (loc == null) { loc = ScriptableObject.CreateInstance <LocalizationVar>(); loc.name = locName; loc.Id = nextID; loc.Original = originalText; loc.Hint = hint; UnityEditor.AssetDatabase.CreateAsset(loc, "Assets/Data/Localization/" + locName + ".asset"); locMan._localizationVars.Add(loc); Debug.Log("Created LocVar: " + loc.name); locMan.BumpVersion(); UnityEditor.EditorUtility.SetDirty(loc); } UnityEditor.EditorUtility.SetDirty(locMan); return(loc); }
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { EditorGUI.BeginProperty(position, label, property); if (property.serializedObject.isEditingMultipleObjects) { EditorGUI.PropertyField(position, property); EditorGUI.EndProperty(); return; } // Draw label position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label); if (Event.current.type == EventType.Repaint) { this._width = Mathf.Abs(position.width); if (property.objectReferenceValue) { LocalizationVar targetInstance = property.objectReferenceValue as LocalizationVar; this._height = ((int)(GUI.skin.textArea.CalcHeight(new GUIContent(targetInstance.Original), this._width - 40) / EditorGUIUtility.singleLineHeight) + 1) * EditorGUIUtility.singleLineHeight; } } // Don't make child fields be indented var indent = EditorGUI.indentLevel; EditorGUI.indentLevel = 0; if (property.objectReferenceValue) { var prettyPrint = this.GetPrettyPrintPropertyPath(property); // Calculate rects var textRect = new Rect(position.x, position.y, position.width - 40, position.height); var assetRect = new Rect(textRect.xMax + 2, position.y, 35, EditorGUIUtility.singleLineHeight); LocalizationVar targetInstance = property.objectReferenceValue as LocalizationVar; if (targetInstance) { GUI.skin.textArea.wordWrap = true; string text = EditorGUI.TextArea(textRect, targetInstance.Original, GUI.skin.textArea); if (string.Compare(text, targetInstance.Original) != 0) { targetInstance.Original = text; EditorUtility.SetDirty(targetInstance); } } property.objectReferenceValue = EditorGUI.ObjectField(assetRect, GUIContent.none, property.objectReferenceValue, typeof(LocalizationVar), false); } else { var assetRect = new Rect(position.x, position.y, position.width - 65, position.height); var addButtonRect = new Rect(assetRect.xMax + 5, position.y, 30, position.height); var stackAddButtonRect = new Rect(addButtonRect.xMax, position.y, 30, position.height); property.objectReferenceValue = EditorGUI.ObjectField(assetRect, GUIContent.none, property.objectReferenceValue, typeof(LocalizationVar), false); if (GUI.Button(addButtonRect, "+")) { foreach (var targetObject in property.serializedObject.targetObjects) { if (targetObject is LabelLocalization) { (targetObject as LabelLocalization).CreateLocAsset(); } else { var hostObjectName = targetObject.name; var prettyPrint = this.GetPrettyPrintPropertyPath(property); string locName = "Localization - " + hostObjectName + " - " + prettyPrint; string locHint = hostObjectName + " " + prettyPrint; string locOriginal = property.displayName; property.objectReferenceValue = LocalizationManager.GetOrCreateLocAsset(locName, locHint, locOriginal, true); } } } if (GUI.Button(stackAddButtonRect, "V")) { foreach (var targetObject in property.serializedObject.targetObjects) { if (targetObject is LabelLocalization) { (targetObject as LabelLocalization).CreateLocAsset(); } else { var hostObjectName = targetObject.name; var prettyPrint = this.GetPrettyPrintPropertyPath(property); string locName = "Localization - " + hostObjectName + " - " + prettyPrint; string locHint = hostObjectName + " " + prettyPrint; string locOriginal = property.displayName; property.objectReferenceValue = LocalizationManager.GetOrCreateLocAsset(locName, locHint, locOriginal, true); if (targetObject is ScriptableObject) { string sourcePath = AssetDatabase.GetAssetPath(property.objectReferenceValue); AssetDatabase.RemoveObjectFromAsset(property.objectReferenceValue); AssetDatabase.DeleteAsset(sourcePath); AssetDatabase.AddObjectToAsset(property.objectReferenceValue, targetObject); EditorUtility.SetDirty(targetObject); AssetDatabase.SaveAssets(); if (AssetDatabase.IsMainAsset(property.objectReferenceValue)) { Debug.LogError("Locvar became main asset, fixing..."); string assetPath = AssetDatabase.GetAssetPath(targetObject); AssetDatabase.SetMainObject(targetObject, assetPath); AssetDatabase.SaveAssets(); AssetDatabase.ImportAsset(assetPath, ImportAssetOptions.ForceUpdate); if (AssetDatabase.IsMainAsset(property.objectReferenceValue)) { Debug.LogError("ERROR: Locvar is still the main asset"); } else { Debug.Log("SUCCESS: Locvar is no longer the main asset !"); } } } } } } } // Set indent back to what it was EditorGUI.indentLevel = indent; EditorGUI.EndProperty(); }