// Reverse lookup the phrases for this language private void DrawReverse(LeanLocalization localization, string language) { for (var i = 0; i < localization.Phrases.Count; i++) { var labelA = Reserve(); var phrase = localization.Phrases[i]; var translation = phrase.Translations.Find(t => t.Language == language); BeginModifications(); { EditorGUI.LabelField(labelA, phrase.Name); } EndModifications(); if (translation != null) { if (reverseIndex == i) { BeginModifications(); { phrase.Name = EditorGUI.TextField(labelA, "", phrase.Name); } EndModifications(); } if (EditorGUI.Foldout(labelA, reverseIndex == i, reverseIndex == i ? "" : phrase.Name) == true) { reverseIndex = i; EditorGUI.indentLevel += 1; { DrawTranslation(translation); } EditorGUI.indentLevel -= 1; EditorGUILayout.Separator(); } else if (reverseIndex == i) { reverseIndex = -1; } } else { var valueA = Reserve(ref labelA, 120.0f); if (GUI.Button(valueA, "Create Translation") == true) { MarkAsModified(); var newTranslation = new LeanTranslation(); newTranslation.Language = language; newTranslation.Text = phrase.Name; phrase.Translations.Add(newTranslation); } } } }
// Draw the whole inspector public override void OnInspectorGUI() { var localization = (LeanLocalization)target; EditorGUILayout.Separator(); EditorGUILayout.PropertyField(serializedObject.FindProperty("CurrentLanguage")); EditorGUILayout.Separator(); DrawLanguages(localization); EditorGUILayout.Separator(); EditorGUILayout.Separator(); DrawPhrases(localization); EditorGUILayout.Separator(); // Update if dirty? if (serializedObject.ApplyModifiedProperties() == true || dirty == true) { dirty = false; LeanLocalization.UpdateTranslations(); EditorUtility.SetDirty(localization); EditorApplication.MarkSceneDirty(); } }
// This method will return the current instance, or create one public static LeanLocalization GetInstance() { if (Instance == null) { Instance = new GameObject(typeof(LeanLocalization).Name).AddComponent<LeanLocalization>(); } return Instance; }
// Unset instance? protected virtual void OnDisable() { if (Instance == this) { Instance = null; } UpdateTranslations(); }
// This method will return the current instance, or create one public static LeanLocalization GetInstance() { if (Instance == null) { Instance = new GameObject(typeof(LeanLocalization).Name).AddComponent <LeanLocalization>(); } return(Instance); }
// Set the instance, merge old instance, and update translations protected virtual void OnEnable() { // Make sure there's only one LeanLocalization active in the scene if (Instance != null && Instance != this) { MergeLocalizations(Instance, this); Debug.LogWarning("Your scene already contains a " + typeof(LeanLocalization).Name + ", merging & destroying the old one..."); DestroyImmediate(Instance.gameObject); } // Set the instance Instance = this; // Update translations UpdateTranslations(); }
private static void MergeLocalizations(LeanLocalization oldLocalization, LeanLocalization newLocalization) { // Go through all old phrases for (var i = oldLocalization.Phrases.Count - 1; i >= 0; i--) { var oldPhrase = oldLocalization.Phrases[i]; var newPhrase = newLocalization.AddPhrase(oldPhrase.Name); // Go through all old translations for (var j = oldPhrase.Translations.Count - 1; j >= 0; j--) { var oldTranslation = oldPhrase.Translations[j]; var newTranslation = newPhrase.AddTranslation(oldTranslation.Language); newTranslation.Text = oldTranslation.Text; newTranslation.Object = oldTranslation.Object; } } }
public void LoadFromSource() { if (Source != null && string.IsNullOrEmpty(SourceLanguage) == false) { var localization = LeanLocalization.GetInstance(); var lines = Source.text.Split(newlineCharacters, System.StringSplitOptions.RemoveEmptyEntries); foreach (var line in lines) { var equalsIndex = line.IndexOf('='); if (equalsIndex != -1) { var phraseName = line.Substring(0, equalsIndex).Trim(); var phraseTranslation = line.Substring(equalsIndex + 1).Trim(); // Replace line markers with actual newlines phraseTranslation = phraseTranslation.Replace(newlineString, System.Environment.NewLine); // Does this entry have a comment? var commentIndex = phraseTranslation.IndexOf("//"); if (commentIndex != -1) { phraseTranslation = phraseTranslation.Substring(0, commentIndex).Trim(); } // Find or add the translation for this phrase var translation = localization.AddTranslation(SourceLanguage, phraseName); // Set the translation text for this phrase translation.Text = phraseTranslation; } } // Update translations? if (localization.CurrentLanguage == SourceLanguage) { LeanLocalization.UpdateTranslations(); } } }
private void DrawLanguages(LeanLocalization localization) { var labelA = Reserve(); var valueA = Reserve(ref labelA, 35.0f); EditorGUI.LabelField(labelA, "Languages", EditorStyles.boldLabel); // Add a new language? if (GUI.Button(valueA, "Add") == true) { MarkAsModified(); var menu = new GenericMenu(); for (var i = 0; i < newLanguages.Length; i++) { var newLanguage = newLanguages[i]; menu.AddItem(new GUIContent(newLanguage), false, () => localization.AddLanguage(newLanguage)); } menu.DropDown(valueA); } existingLanguages.Clear(); // Draw all added languages for (var i = 0; i < localization.Languages.Count; i++) { var labelB = Reserve(); var valueB = Reserve(ref labelB, 20.0f); var language = localization.Languages[i]; // Edit language name or remove if (languageIndex == i) { BeginModifications(); { localization.Languages[i] = EditorGUI.TextField(labelB, language); } EndModifications(); if (GUI.Button(valueB, "X") == true) { MarkAsModified(); localization.Languages.RemoveAt(i); languageIndex = -1; } } // Expand language? if (EditorGUI.Foldout(labelB, languageIndex == i, languageIndex == i ? "" : language) == true) { if (languageIndex != i) { languageIndex = i; reverseIndex = -1; } EditorGUI.indentLevel += 1; { DrawReverse(localization, language); } EditorGUI.indentLevel -= 1; EditorGUILayout.Separator(); } else if (languageIndex == i) { languageIndex = -1; reverseIndex = -1; } // Already added? if (existingLanguages.Contains(language) == true) { EditorGUILayout.HelpBox("This language already exists in the list!", MessageType.Warning); } else { existingLanguages.Add(language); } } }
private void DrawTranslations(LeanLocalization localization, LeanPhrase phrase) { existingLanguages.Clear(); for (var i = 0; i < phrase.Translations.Count; i++) { var labelA = Reserve(); var valueA = Reserve(ref labelA, 20.0f); var translation = phrase.Translations[i]; if (translationIndex == i) { BeginModifications(); { translation.Language = EditorGUI.TextField(labelA, "", translation.Language); } EndModifications(); if (GUI.Button(valueA, "X") == true) { MarkAsModified(); phrase.Translations.RemoveAt(i); translationIndex = -1; } } if (EditorGUI.Foldout(labelA, translationIndex == i, translationIndex == i ? "" : translation.Language) == true) { translationIndex = i; EditorGUI.indentLevel += 1; { DrawTranslation(translation); } EditorGUI.indentLevel -= 1; EditorGUILayout.Separator(); } else if (translationIndex == i) { translationIndex = -1; } if (existingLanguages.Contains(translation.Language) == true) { EditorGUILayout.HelpBox("This phrase has already been translated to this language!", MessageType.Warning); } else { existingLanguages.Add(translation.Language); } if (localization.Languages.Contains(translation.Language) == false) { EditorGUILayout.HelpBox("This translation uses a language that hasn't been set in the localization!", MessageType.Warning); } } for (var i = 0; i < localization.Languages.Count; i++) { var language = localization.Languages[i]; if (phrase.Translations.Exists(t => t.Language == language) == false) { var labelA = Reserve(); var valueA = Reserve(ref labelA, 120.0f); EditorGUI.LabelField(labelA, language); if (GUI.Button(valueA, "Create Translation") == true) { MarkAsModified(); var newTranslation = new LeanTranslation(); newTranslation.Language = language; newTranslation.Text = phrase.Name; translationIndex = phrase.Translations.Count; phrase.Translations.Add(newTranslation); } } } }
private void DrawPhrases(LeanLocalization localization) { var labelA = Reserve(); var valueA = Reserve(ref labelA, 35.0f); EditorGUI.LabelField(labelA, "Phrases", EditorStyles.boldLabel); if (GUI.Button(valueA, "Add") == true) { MarkAsModified(); var newPhrase = new LeanPhrase(); newPhrase.Name = "New Phrase"; phraseIndex = localization.Phrases.Count; localization.Phrases.Add(newPhrase); } existingPhrases.Clear(); for (var i = 0; i < localization.Phrases.Count; i++) { var labelB = Reserve(); var valueB = Reserve(ref labelB, 20.0f); var phrase = localization.Phrases[i]; if (phraseIndex == i) { BeginModifications(); { phrase.Name = EditorGUI.TextField(labelB, "", phrase.Name); } EndModifications(); if (GUI.Button(valueB, "X") == true) { MarkAsModified(); localization.Phrases.RemoveAt(i); phraseIndex = -1; } } if (EditorGUI.Foldout(labelB, phraseIndex == i, phraseIndex == i ? "" : phrase.Name) == true) { if (phraseIndex != i) { phraseIndex = i; translationIndex = -1; } EditorGUI.indentLevel += 1; { DrawTranslations(localization, phrase); } EditorGUI.indentLevel -= 1; EditorGUILayout.Separator(); } else if (phraseIndex == i) { phraseIndex = -1; translationIndex = -1; } if (existingPhrases.Contains(phrase.Name) == true) { EditorGUILayout.HelpBox("This phrase already exists in the list!", MessageType.Warning); } else { existingPhrases.Add(phrase.Name); } } }
// Call this to force the behaviour to get updated public void UpdateLocalization() { UpdateTranslation(LeanLocalization.GetTranslation(PhraseName)); }