Skip to content

Commit 09f0e44

Browse files
committed
feat: DropDownLanguageSwitcher
1 parent 2c02e9d commit 09f0e44

7 files changed

Lines changed: 1069 additions & 1631 deletions

File tree

Assets/Code/Localization/Code/UI.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using Code.Localization.Code.Services.LocalizeLanguageService;
2+
using TMPro;
3+
using UnityEngine;
4+
using Zenject;
5+
6+
namespace Code.Localization.Code.UI
7+
{
8+
public class DropDownLanguageSwitcher : MonoBehaviour
9+
{
10+
[SerializeField] private TMP_Dropdown _dropdown;
11+
private ILocalizeLanguageService _localizeLanguageService;
12+
13+
private bool _initialized;
14+
15+
private void OnValidate()
16+
{
17+
if (_dropdown == null)
18+
_dropdown = GetComponent<TMP_Dropdown>();
19+
}
20+
21+
[Inject]
22+
public void Construct(ILocalizeLanguageService localizeLanguageService)
23+
{
24+
_localizeLanguageService = localizeLanguageService;
25+
}
26+
27+
private void Start()
28+
{
29+
SetupDropdown();
30+
}
31+
32+
private void SetupDropdown()
33+
{
34+
_dropdown.ClearOptions();
35+
36+
var languages = _localizeLanguageService.GetAvailableLanguages();
37+
_dropdown.AddOptions(languages);
38+
39+
var currentLanguage = _localizeLanguageService.GetCurrentLanguage();
40+
int currentIndex = languages.IndexOf(currentLanguage);
41+
_dropdown.SetValueWithoutNotify(currentIndex >= 0 ? currentIndex : 0);
42+
43+
_dropdown.onValueChanged.AddListener(OnDropdownValueChanged);
44+
_initialized = true;
45+
}
46+
47+
private void OnDropdownValueChanged(int index)
48+
{
49+
if (!_initialized) return;
50+
51+
var selectedLanguage = _dropdown.options[index].text;
52+
_localizeLanguageService.SetLanguage(selectedLanguage);
53+
}
54+
}
55+
}

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

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Code/Localization/Editor/LocalizationEditorWindow.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,17 +179,31 @@ private void FindTMPLocalizersInAssets()
179179
[Button("💾 Save Changes To Assets", ButtonSizes.Large), GUIColor(0.6f, 1f, 0.6f)]
180180
private void ApplyLocalizationKeyChangesToAssets()
181181
{
182+
var pathsToSave = new HashSet<string>();
183+
182184
foreach (var entry in _localizersInAssets)
183185
{
184186
if (entry.Component == null) continue;
185187

186188
Undo.RecordObject(entry.Component, "Change Localization Key");
187189
entry.Component.localizationKey = entry.LocalizationKey;
188-
EditorUtility.SetDirty(entry.Component);
190+
EditorUtility.SetDirty(entry.Component.gameObject);
191+
192+
if (!string.IsNullOrEmpty(entry.AssetPath))
193+
{
194+
pathsToSave.Add(entry.AssetPath);
195+
}
196+
}
197+
198+
foreach (var path in pathsToSave)
199+
{
200+
var prefabRoot = PrefabUtility.LoadPrefabContents(path);
201+
PrefabUtility.SaveAsPrefabAsset(prefabRoot, path);
202+
PrefabUtility.UnloadPrefabContents(prefabRoot);
189203
}
190204

191205
AssetDatabase.SaveAssets();
192-
Debug.Log("✅ Localization keys in assets updated.");
206+
Debug.Log("✅ Localization keys in assets updated and prefabs saved.");
193207
}
194208

195209
[FoldoutGroup("Add Missing Localizers", expanded: true)]

0 commit comments

Comments
 (0)