Skip to content

Commit ea38e59

Browse files
committed
fix: switch language
1 parent ed4afda commit ea38e59

7 files changed

Lines changed: 23 additions & 73 deletions

File tree

Assets/Code/Localization/Code/Localize.cs

Lines changed: 0 additions & 26 deletions
This file was deleted.

Assets/Code/Localization/Code/Localize.cs.meta

Lines changed: 0 additions & 11 deletions
This file was deleted.

Assets/Code/Localization/Code/LocalizeBase.cs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,7 @@ public abstract class LocalizeBase : MonoBehaviour
1111
protected virtual void Start()
1212
{
1313
if (!Locale.currentLanguageHasBeenSet)
14-
{
1514
Locale.currentLanguageHasBeenSet = true;
16-
SetCurrentLanguage(Locale.PlayerLanguage);
17-
}
1815

1916
UpdateLocale();
2017
}
@@ -26,14 +23,5 @@ public static string GetLocalizedString(string key)
2623

2724
return string.Empty;
2825
}
29-
30-
public static void SetCurrentLanguage(SystemLanguage language)
31-
{
32-
Locale.CurrentLanguage = language.ToString();
33-
Locale.PlayerLanguage = language;
34-
Localize[] allTexts = FindObjectsOfType<Localize>();
35-
for (int i = 0; i < allTexts.Length; i++)
36-
allTexts[i].UpdateLocale();
37-
}
3826
}
3927
}

Assets/Code/Localization/Code/Services/LocalizeLanguageService/LocalizeLanguageService.cs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Collections.Generic;
22
using System.IO;
3+
using System.Linq;
34
using UnityEngine;
45

56
namespace Code.Localization.Code.Services.LocalizeLanguageService
@@ -10,32 +11,25 @@ public class LocalizeLanguageService : ILocalizeLanguageService
1011

1112
public List<string> GetAvailableLanguages()
1213
{
13-
List<string> languages = new();
14-
1514
TextAsset[] files = Resources.LoadAll<TextAsset>(ResourcesPath);
16-
foreach (TextAsset file in files)
17-
{
18-
languages.Add(Path.GetFileNameWithoutExtension(file.name));
19-
}
20-
21-
return languages;
15+
return files.Select(file => Path.GetFileNameWithoutExtension(file.name)).ToList();
2216
}
2317

2418
public void SetLanguage(string languageName)
2519
{
26-
Localize.SetCurrentLanguage(LanguageNameToSystemLanguage(languageName));
20+
Locale.CurrentLanguage = languageName.ToString();
21+
Locale.PlayerLanguage = LanguageNameToSystemLanguage(languageName);
22+
LocalizeBase[] allTexts = Object.FindObjectsOfType<LocalizeBase>();
23+
foreach (var t in allTexts)
24+
t.UpdateLocale();
2725
}
2826

29-
public string GetCurrentLanguage()
30-
{
31-
return Locale.CurrentLanguage;
32-
}
33-
27+
public string GetCurrentLanguage() => Locale.CurrentLanguage;
28+
3429
private SystemLanguage LanguageNameToSystemLanguage(string name)
3530
{
3631
if (System.Enum.TryParse<SystemLanguage>(name, true, out var lang))
3732
return lang;
38-
3933
Debug.LogWarning($"⚠️ Unknown language '{name}', fallback to English.");
4034
return SystemLanguage.English;
4135
}

Assets/Code/Localization/Code/TMP_Localizer.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace Code.Localization.Code
66
[DisallowMultipleComponent]
77
public class TMP_Localizer : LocalizeBase
88
{
9-
private TMP_Text _text;
9+
[SerializeField] private TMP_Text _text;
1010

1111
private void OnValidate()
1212
{
@@ -16,7 +16,8 @@ private void OnValidate()
1616

1717
public override void UpdateLocale()
1818
{
19-
if (!_text) return;
19+
if (!_text)
20+
return;
2021

2122
if (!System.String.IsNullOrEmpty(localizationKey) &&
2223
Locale.currentLanguageStrings.ContainsKey(localizationKey))

Assets/Code/Localization/Code/UI/DropDownSwitcherLanguage.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,10 @@ private void SetupDropdown()
4141
_dropdown.SetValueWithoutNotify(currentIndex >= 0 ? currentIndex : 0);
4242

4343
_dropdown.onValueChanged.AddListener(OnDropdownValueChanged);
44-
_initialized = true;
4544
}
4645

4746
private void OnDropdownValueChanged(int index)
4847
{
49-
if (!_initialized) return;
50-
5148
var selectedLanguage = _dropdown.options[index].text;
5249
_localizeLanguageService.SetLanguage(selectedLanguage);
5350
}

Assets/Resources/UI/GameHud.prefab

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ MonoBehaviour:
150150
m_Name:
151151
m_EditorClassIdentifier:
152152
localizationKey: so
153+
_text: {fileID: 2703089668246871777}
153154
--- !u!1 &1873461456170480768
154155
GameObject:
155156
m_ObjectHideFlags: 0
@@ -185,8 +186,8 @@ RectTransform:
185186
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
186187
m_AnchorMin: {x: 0, y: 0}
187188
m_AnchorMax: {x: 1, y: 1}
188-
m_AnchoredPosition: {x: -185.53857, y: 55.832886}
189-
m_SizeDelta: {x: -461.2179, y: -314.38458}
189+
m_AnchoredPosition: {x: -214.17346, y: 55.832886}
190+
m_SizeDelta: {x: -518.4877, y: -314.3846}
190191
m_Pivot: {x: 0.5, y: 0.5}
191192
--- !u!222 &5603375788990825691
192193
CanvasRenderer:
@@ -298,6 +299,7 @@ MonoBehaviour:
298299
m_Name:
299300
m_EditorClassIdentifier:
300301
localizationKey: ru
302+
_text: {fileID: 8802848066749572383}
301303
--- !u!1 &2088966450242780577
302304
GameObject:
303305
m_ObjectHideFlags: 0
@@ -518,6 +520,7 @@ MonoBehaviour:
518520
m_Name:
519521
m_EditorClassIdentifier:
520522
localizationKey: go
523+
_text: {fileID: 6745997590823684149}
521524
--- !u!1 &3038911369929102861
522525
GameObject:
523526
m_ObjectHideFlags: 0
@@ -741,6 +744,7 @@ MonoBehaviour:
741744
m_Name:
742745
m_EditorClassIdentifier:
743746
localizationKey: eng
747+
_text: {fileID: 8204655942856900796}
744748
--- !u!1 &3512033971405753791
745749
GameObject:
746750
m_ObjectHideFlags: 0
@@ -1006,8 +1010,8 @@ RectTransform:
10061010
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
10071011
m_AnchorMin: {x: 0, y: 0}
10081012
m_AnchorMax: {x: 1, y: 1}
1009-
m_AnchoredPosition: {x: 187, y: 55.833008}
1010-
m_SizeDelta: {x: -461.7052, y: -314.38}
1013+
m_AnchoredPosition: {x: 213.62549, y: 55.833008}
1014+
m_SizeDelta: {x: -514.9562, y: -314.38}
10111015
m_Pivot: {x: 0.5, y: 0.5}
10121016
--- !u!222 &784651582178854056
10131017
CanvasRenderer:
@@ -1119,6 +1123,7 @@ MonoBehaviour:
11191123
m_Name:
11201124
m_EditorClassIdentifier:
11211125
localizationKey: uk
1126+
_text: {fileID: 6341152590287427063}
11221127
--- !u!1 &5174823346860950775
11231128
GameObject:
11241129
m_ObjectHideFlags: 0
@@ -1432,6 +1437,7 @@ MonoBehaviour:
14321437
m_Name:
14331438
m_EditorClassIdentifier:
14341439
localizationKey: pla
1440+
_text: {fileID: 7309555813235861460}
14351441
--- !u!1 &5365011530554689134
14361442
GameObject:
14371443
m_ObjectHideFlags: 0
@@ -2098,6 +2104,7 @@ MonoBehaviour:
20982104
m_Name:
20992105
m_EditorClassIdentifier:
21002106
localizationKey: ex
2107+
_text: {fileID: 6140615958831474822}
21012108
--- !u!1 &8566553790827359936
21022109
GameObject:
21032110
m_ObjectHideFlags: 0

0 commit comments

Comments
 (0)