Skip to content

Commit

Permalink
fix: incorrect display for zh-* culture names in lang switcher (#172)
Browse files Browse the repository at this point in the history
Also: Remove uncultured language names from language switcher when a
cultured variant is found

Closes: #171
Closes: #157
  • Loading branch information
bdunderscore authored Feb 18, 2024
1 parent d7f8963 commit ac48b51
Show file tree
Hide file tree
Showing 7 changed files with 862 additions and 12 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [unreleased]

### Fixed
- Incorrect language display names for some locales (#171)

### Changed
- Uncultured language variants (e.g. `en`) are not displayed in the language switcher when cultured variants (e.g.
`en-US`) are registered. (#171)

### Removed

Expand Down
16 changes: 4 additions & 12 deletions Editor/UI/LanguageSwitcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,10 @@ public static void DrawImmediate()
var curLang = LanguagePrefs.Language;

var curIndex = LanguagePrefs.RegisteredLanguages.IndexOf(curLang);
var DisplayNames = LanguagePrefs.RegisteredLanguages.Select(
lang =>
{
try
{
return CultureInfo.GetCultureInfo(lang).NativeName;
}
catch (Exception)
{
return lang;
}
})
var DisplayNames = LanguagePrefs.RegisteredLanguages
.Where(lang => lang.Contains("-") ||
LanguagePrefs.RegisteredLanguages.All(l2 => !l2.StartsWith(lang + "-")))
.Select(LanguagePrefs.GetLocaleNativeName)
.ToArray();

var newIndex = EditorGUILayout.Popup("Editor Language", curIndex, DisplayNames);
Expand Down
39 changes: 39 additions & 0 deletions Editor/UI/Localization/LanguagePrefs.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Globalization;
using System.Linq;
using System.Runtime.CompilerServices;
using Newtonsoft.Json;
using UnityEditor;
using UnityEngine;

Expand All @@ -12,9 +15,13 @@ namespace nadena.dev.ndmf.localization
/// </summary>
public static class LanguagePrefs
{
private const string LocaleNameDatasetPath =
"Packages/nadena.dev.ndmf/Editor/UI/Localization/language_names.json";
private const string EditorPrefKey = "nadena.dev.ndmf.language-selection";
private static string _curLanguage = "en-us";

private static ImmutableDictionary<String, String> LocaleNames;

[InitializeOnLoadMethod]
private static void Init()
{
Expand Down Expand Up @@ -111,8 +118,40 @@ private static void TriggerLanguageChangeCallbacks()
/// </summary>
public static ImmutableSortedSet<string> RegisteredLanguages { get; private set; }

internal static string GetLocaleNativeName(string locale)
{
if (LocaleNames.TryGetValue(locale.ToLowerInvariant().Replace("-", "_"), out var name))
{
return name;
}
else
{
try
{
return CultureInfo.CreateSpecificCulture(locale).NativeName;
}
catch (Exception e)
{
return locale;
}
}
}

static LanguagePrefs()
{
try
{
var localeNameJson = System.IO.File.ReadAllText(LocaleNameDatasetPath);
LocaleNames = JsonConvert.DeserializeObject<Dictionary<string, string>>(localeNameJson)
.Select(kvp => { return new KeyValuePair<String, String>(kvp.Key.ToLowerInvariant(), kvp.Value); })
.ToImmutableDictionary();
}
catch (Exception e)
{
Debug.LogException(e);
LocaleNames = ImmutableDictionary<string, string>.Empty;
}

RegisteredLanguages = ImmutableSortedSet<string>.Empty;
}

Expand Down
13 changes: 13 additions & 0 deletions Editor/UI/Localization/import_icu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import icu
import json

locales = icu.Locale.getAvailableLocales()

displayNames = {}
for locale in locales:
icu_locale = icu.Locale(locale)
displayNames[locale] = icu_locale.getDisplayName(icu_locale)

displayNames["__comment__"] = "Derived from ICU dataset. See import_icu.py";

print(json.dumps(displayNames, indent=4, ensure_ascii=False))
7 changes: 7 additions & 0 deletions Editor/UI/Localization/import_icu.py.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit ac48b51

Please sign in to comment.