Skip to content

Commit 6ea9e2b

Browse files
* refactor(LocalizedDropdown.cs): extract duplicated code to OnEnable method
* fix(LocalizedDropdown.cs): call UpdateCurrentLanguage method in OnEnable instead of Start method * refactor(LocalizedText.cs): remove unused variables and comments * fix(LocalizedText.cs): update OnDrawGizmos method to check for null or empty currentText before calling UpdateCurrentLanguage method * fix(LocalizedText.cs): update UpdateLocalizedDataText method to directly set the text value instead of using a temporary variable * fix(LocalizedText.cs): update UpdateLocalizedDataText method to replace the wordReplace value in the text value * fix(LocalizedText.cs): update UpdateCurrentLanguage method to remove unused variable and add missing null check * refactor(LocalizedText.cs): remove commented out code * chore(LocalizedText.cs): add whitespace for readability * feat(LocalizedText.cs): add SetValue method to update localized text value by word replace * refactor(LocalizationConfig.cs): rename GetPretranslate method to GetPreTranslated and add targetLanguage parameter * refactor(LocalizationConfig.cs): update GetPreTranslated method to return null if localized data is not found * refactor(LocalizationController.cs): move LocalizationController to Laputa.Localization namespace * feat(LocalizationController.cs): add Singleton pattern to LocalizationController * feat(LocalizationController.cs): add localizationConfig field to LocalizationController * feat(LocalizationController.cs): add GetPreTranslatedText method to LocalizationController * chore(default-2022.dwlt): update layout for Font Asset Creator window * fix(default-2022.dwlt): fix EditorHideFlags for MonoBehaviour 2 * fix(default-2022.dwlt): fix PixelRect and ShowMode for MonoBehaviour 1 * fix(default-2022.dwlt): fix PixelRect, ShowMode, and RootView for MonoBehaviour 3 * chore(Layouts): update default-2022.dwlt layout file * chore(default-2022.dwlt): update layout dimensions and sizes * chore(default-2022.dwlt): update layout settings * refactor(default-2022.dwlt): change GameView position and size * refactor(default-2022.dwlt): change the position and size of two other views * chore(default-2022.dwlt): update window layout * chore(default-2022.dwlt): change LocalizationWindowEditor to InspectorWindow * chore(default-2022.dwlt): change Font Asset Creator window position and size * chore(default-2022.dwlt): change Texture2D Preview window position and size * chore(default-2022.dwlt): update layout settings * fix(default-2022.dwlt): adjust m_HBaseRangeMin and m_VBaseRangeMin values * fix(default-2022.dwlt): adjust m_HBaseRangeMax and m_VBaseRangeMax values * fix(default-2022.dwlt): adjust m_LastWindowPixelSize values * fix(default-2022.dwlt): adjust m_Pos values * fix(default-2022.dwlt): adjust snapOffset and snapCorner values * fix(default-2022.dwlt): adjust m_Size values * chore(Layouts/default-2022.dwlt): update layout file * fix(Layouts/default-2022.dwlt): fix x and width values in m_Pos serialized data * fix(Layouts/default-2022.dwlt): update m_LastProjectPath value * fix(Layouts/default-2022.dwlt): update m_ExpandedIDs values in m_FolderTreeState and m_AssetTreeState * fix(Layouts/default-2022.dwlt): update m_ClientGUIView value in m_RenameOverlay of m_AssetTreeState * chore(default-2022.dwlt): update layout positions and sizes
1 parent 098da88 commit 6ea9e2b

File tree

5 files changed

+277
-181
lines changed

5 files changed

+277
-181
lines changed

Assets/Laputa/Localization/Components/LocalizedDropdown.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ private void OnDrawGizmos()
1717
UpdateCurrentLanguage(LocalizationManager.currentLanguageName);
1818
}
1919

20+
private void OnEnable()
21+
{
22+
UpdateCurrentLanguage(LocalizationManager.currentLanguageName);
23+
}
24+
2025
private void Awake()
2126
{
2227
if (TmpDropDown)

Assets/Laputa/Localization/Components/LocalizedText.cs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@ public class LocalizedText : MonoBehaviour
1717

1818
private void OnDrawGizmos()
1919
{
20-
if (Application.isPlaying) return;
20+
if (Application.isPlaying || String.IsNullOrEmpty(currentText)) return;
21+
UpdateCurrentLanguage(LocalizationManager.currentLanguageName);
22+
}
23+
24+
private void OnEnable()
25+
{
2126
UpdateCurrentLanguage(LocalizationManager.currentLanguageName);
2227
}
2328

@@ -38,30 +43,26 @@ private LanguageLocalizedData GetLanguageLocalizedData()
3843

3944
private void UpdateLocalizedDataText()
4045
{
41-
//Debug.Log("Log 1 text");
4246
if (TextMeshProUGUI)
4347
{
44-
var tmpText = TextMeshProUGUI.text;
4548
foreach (var text in localizedDataTextList)
4649
{
47-
tmpText = tmpText.Replace("{" +$"{text.wordReplace}" + "}", text.value);
50+
TextMeshProUGUI.text = TextMeshProUGUI.text.Replace("{" +$"{text.wordReplace}" + "}", text.value);
4851
}
4952
}
5053
else
5154
{
52-
var textLegacy = Text.text;
5355
foreach (var text in localizedDataTextList)
5456
{
55-
textLegacy = textLegacy.Replace($"{text.wordReplace}", text.value);
57+
Text.text = Text.text.Replace($"{text.wordReplace}", text.value);
5658
}
5759
}
5860
}
5961

6062
public void UpdateCurrentLanguage(LanguageName languageName)
6163
{
6264
var data = GetLanguageLocalizedData();
63-
//var languageTemp = data.languageData.languageName;
64-
65+
6566
if (TextMeshProUGUI)
6667
{
6768
TextMeshProUGUI.text = data.text;
@@ -78,7 +79,7 @@ public void UpdateCurrentLanguage(LanguageName languageName)
7879
public async void AutoGenerate()
7980
{
8081
Debug.Log("<color=green> Start generating ... </color>");
81-
LocalizationConfig localizationConfig= Resources.Load<LocalizationConfig>("LocalizationConfig");
82+
LocalizationConfig localizationConfig = Resources.Load<LocalizationConfig>("LocalizationConfig");
8283

8384
if (localizationConfig)
8485
{
@@ -122,6 +123,18 @@ private LanguageLocalizedData GetLanguage(LanguageData languageData)
122123
{
123124
return languageDataList.Find(item => item.languageData.languageName == languageData.languageName);
124125
}
126+
127+
public void SetValue(string word, string val)
128+
{
129+
foreach (LocalizedDataText text in localizedDataTextList)
130+
{
131+
if (text.wordReplace == word)
132+
{
133+
text.value = val;
134+
UpdateCurrentLanguage(LocalizationManager.currentLanguageName);
135+
}
136+
}
137+
}
125138
}
126139

127140
[Serializable]

Assets/Laputa/Localization/LocalizationConfig.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,16 @@ public void TranslateAllPredata()
4949
}
5050
}
5151

52-
// public void GetPretranslate(string content, LanguageName languageName)
53-
// {
54-
// return localizedDataList.Find(item => item.GetLocalizedData(languageName))
55-
// }
52+
public string GetPreTranslated(string content, LanguageName targetLanguage)
53+
{
54+
var data = localizedDataList.Find(item => item.content == content);
55+
if (data!=null)
56+
{
57+
return data.GetTranslatedContent(targetLanguage);
58+
}
59+
60+
return null;
61+
}
5662
}
5763

5864
[Serializable]
Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,42 @@
11
using System;
2-
using Laputa.Localization;
32
using UnityEngine;
43

5-
public class LocalizationController : MonoBehaviour
4+
namespace Laputa.Localization
65
{
7-
private void Awake()
6+
public class LocalizationController : MonoBehaviour
87
{
9-
LanguageName currentLanguageName = Enum.Parse<LanguageName>(PlayerPrefs.GetString("localization","English"));
10-
LocalizationManager.OnChangeLanguage(currentLanguageName);
11-
}
8+
public static LocalizationController Instance;
9+
public LocalizationConfig localizationConfig;
10+
private void Awake()
11+
{
12+
if (Instance == null)
13+
{
14+
Instance = this;
15+
}
16+
else
17+
{
18+
Destroy(gameObject);
19+
}
20+
21+
LanguageName currentLanguageName = Enum.Parse<LanguageName>(PlayerPrefs.GetString("localization","English"));
22+
LocalizationManager.OnChangeLanguage(currentLanguageName);
1223

13-
private void Update()
14-
{
15-
//Debug.Log(LocalizationManager.currentLanguageName);
24+
if (localizationConfig==null) localizationConfig = Resources.Load<LocalizationConfig>("LocalizationConfig");
25+
}
26+
27+
private void Start()
28+
{
29+
DontDestroyOnLoad(this);
30+
}
31+
32+
protected void OnDestroy()
33+
{
34+
if (Instance == this) Instance = null;
35+
}
36+
37+
public string GetPreTranslatedText(string content)
38+
{
39+
return localizationConfig.GetPreTranslated(content, LocalizationManager.currentLanguageName);
40+
}
1641
}
17-
}
42+
}

0 commit comments

Comments
 (0)