Skip to content

Commit

Permalink
Make sure to load localization values from en-US when they are not …
Browse files Browse the repository at this point in the history
…present in the specified language
  • Loading branch information
EliteAsian123 committed Jul 20, 2024
1 parent 22a76ed commit d0bd804
Showing 1 changed file with 38 additions and 18 deletions.
56 changes: 38 additions & 18 deletions Assets/Script/Localization/LocalizationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,39 +41,44 @@ public static async UniTask LoadLanguage(LoadingContext loadingContext)
await UniTask.RunOnThreadPool(() =>
{
// Attempt to load the selected language
if (!ParseAndLoadLanguage(CultureCode))
if (!TryParseAndLoadLanguage(CultureCode))
{
YargLogger.LogError("Failed to parse and load language! Falling back to default.");
CultureCode = DEFAULT_CULTURE;

// If that fails for whatever reason, load the default one instead
if (!ParseAndLoadLanguage(CultureCode))
if (CultureCode != DEFAULT_CULTURE)
{
// If that fails for whatever reason, load the default one instead
YargLogger.LogError("Failed to parse and load language! Falling back to default.");

CultureCode = DEFAULT_CULTURE;
if (!TryParseAndLoadLanguage(CultureCode))
{
YargLogger.LogError("Failed to parse and load default language!");
}
}
else
{
YargLogger.LogError("Failed to parse and load default language!");
YargLogger.LogError("Failed to parse and load the default language! (no fallback)");
}
}
});
}

private static bool ParseAndLoadLanguage(string cultureCode)
private static bool TryParseAndLoadLanguage(string cultureCode)
{
YargLogger.LogFormatInfo("Loading language `{0}`...", cultureCode);

try
{
_localizationMap.Clear();

// Get the path of the localization file
var file = Path.Combine(PathHelper.StreamingAssetsPath, "lang", $"{cultureCode}.json");
if (!File.Exists(file))
ParseAndLoadLanguage(cultureCode);

// Also combine the keys of the default culture. The default culture is guaranteed to be
// the most up to date as that is the one attached to the repo. The other languages are
// fetched periodically from Crowdin which means there may be some desync.
if (cultureCode != DEFAULT_CULTURE)
{
return false;
ParseAndLoadLanguage(DEFAULT_CULTURE);
}

// Read, parse, and scan for localization keys
var json = File.ReadAllText(file);
var obj = JObject.Parse(json);
ParseObjectRecursive(null, obj);
}
catch (Exception e)
{
Expand All @@ -84,6 +89,21 @@ private static bool ParseAndLoadLanguage(string cultureCode)
return true;
}

private static void ParseAndLoadLanguage(string cultureCode)
{
// Get the path of the localization file
var file = Path.Combine(PathHelper.StreamingAssetsPath, "lang", $"{cultureCode}.json");
if (!File.Exists(file))
{
throw new Exception($"The language file for the specified culture ({cultureCode}) does not exist!");
}

// Read, parse, and scan for localization keys
var json = File.ReadAllText(file);
var obj = JObject.Parse(json);
ParseObjectRecursive(null, obj);
}

private static void ParseObjectRecursive(string parentKey, JObject obj)
{
foreach ((string key, var token) in obj)
Expand All @@ -109,7 +129,7 @@ private static void ParseObjectRecursive(string parentKey, JObject obj)
break;
// If a string is found, that's the end! Add it to the localization map.
case JTokenType.String:
_localizationMap.Add(fullKey, token.ToString());
_localizationMap.TryAdd(fullKey, token.ToString());
break;
// Otherwise... something went wrong.
default:
Expand Down

0 comments on commit d0bd804

Please sign in to comment.