-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: js6pak <[email protected]>
- Loading branch information
1 parent
f2da251
commit 0c8156a
Showing
14 changed files
with
523 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using Reactor.Localization; | ||
|
||
namespace Reactor.Example; | ||
|
||
public class ExampleLocalizationProvider : LocalizationProvider | ||
{ | ||
public override bool TryGetText(StringNames stringName, out string? result) | ||
{ | ||
if (stringName == (StringNames) 1337) | ||
{ | ||
switch (CurrentLanguage) | ||
{ | ||
case SupportedLangs.English: | ||
result = "Cringe English"; | ||
return true; | ||
|
||
default: | ||
result = "Based " + CurrentLanguage; | ||
return true; | ||
} | ||
} | ||
|
||
result = null; | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
39 changes: 39 additions & 0 deletions
39
Reactor/Localization/Extensions/SupportedLangsExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using System; | ||
using System.Globalization; | ||
|
||
namespace Reactor.Localization.Extensions; | ||
|
||
/// <summary> | ||
/// Provides extension methods for <see cref="SupportedLangs"/>. | ||
/// </summary> | ||
public static class SupportedLangsExtensions | ||
{ | ||
/// <summary> | ||
/// Gets a <see cref="CultureInfo"/> from the specified <paramref name="language"/>. | ||
/// </summary> | ||
/// <param name="language">The <see cref="SupportedLangs"/>.</param> | ||
/// <returns>a <see cref="CultureInfo"/>.</returns> | ||
public static CultureInfo ToCultureInfo(this SupportedLangs language) | ||
{ | ||
return language switch | ||
{ | ||
SupportedLangs.English => CultureInfo.GetCultureInfo("en"), | ||
SupportedLangs.Latam => CultureInfo.GetCultureInfo("es"), | ||
SupportedLangs.Brazilian => CultureInfo.GetCultureInfo("pt-BR"), | ||
SupportedLangs.Portuguese => CultureInfo.GetCultureInfo("pt"), | ||
SupportedLangs.Korean => CultureInfo.GetCultureInfo("ko"), | ||
SupportedLangs.Russian => CultureInfo.GetCultureInfo("ru"), | ||
SupportedLangs.Dutch => CultureInfo.GetCultureInfo("nl"), | ||
SupportedLangs.Filipino => CultureInfo.GetCultureInfo("fil"), | ||
SupportedLangs.French => CultureInfo.GetCultureInfo("fr"), | ||
SupportedLangs.German => CultureInfo.GetCultureInfo("de"), | ||
SupportedLangs.Italian => CultureInfo.GetCultureInfo("it"), | ||
SupportedLangs.Japanese => CultureInfo.GetCultureInfo("ja"), | ||
SupportedLangs.Spanish => CultureInfo.GetCultureInfo("es"), | ||
SupportedLangs.SChinese => CultureInfo.GetCultureInfo("zh-Hans"), | ||
SupportedLangs.TChinese => CultureInfo.GetCultureInfo("zh-Hant"), | ||
SupportedLangs.Irish => CultureInfo.GetCultureInfo("ga"), | ||
_ => throw new ArgumentOutOfRangeException(nameof(language), language, null), | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
using System.Collections.Generic; | ||
using Il2CppInterop.Runtime.InteropTypes.Arrays; | ||
|
||
namespace Reactor.Localization; | ||
|
||
/// <summary> | ||
/// Handles custom <see cref="StringNames"/> localization. | ||
/// </summary> | ||
public static class LocalizationManager | ||
{ | ||
private static readonly List<LocalizationProvider> _providers = new(); | ||
|
||
/// <summary> | ||
/// Gets registered <see cref="LocalizationProvider"/>s. | ||
/// </summary> | ||
public static IReadOnlyList<LocalizationProvider> Providers { get; } = _providers.AsReadOnly(); | ||
|
||
/// <summary> | ||
/// Registers a new <see cref="LocalizationProvider"/> to be used for obtaining translations. | ||
/// </summary> | ||
/// <param name="provider">A <see cref="LocalizationProvider"/> instance.</param> | ||
public static void Register(LocalizationProvider provider) | ||
{ | ||
if (!_providers.Contains(provider)) | ||
{ | ||
_providers.Add(provider); | ||
|
||
if (TranslationController.InstanceExists) | ||
{ | ||
provider.SetLanguage(TranslationController.Instance.currentLanguage.languageID); | ||
} | ||
|
||
_providers.Sort((a, b) => b.Priority - a.Priority); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Unregisters a <see cref="LocalizationProvider"/>. | ||
/// </summary> | ||
/// <param name="provider">The <see cref="LocalizationProvider"/> to unregister.</param> | ||
public static void Unregister(LocalizationProvider provider) | ||
{ | ||
_providers.Remove(provider); | ||
} | ||
|
||
internal static bool TryGetTextFormatted(StringNames stringName, Il2CppReferenceArray<Il2CppSystem.Object> parts, out string text) | ||
{ | ||
foreach (var provider in _providers) | ||
{ | ||
if (provider.TryGetTextFormatted(stringName, parts, out text!)) | ||
{ | ||
return true; | ||
} | ||
} | ||
|
||
text = string.Empty; | ||
return false; | ||
} | ||
|
||
internal static bool TryGetText(StringNames stringName, out string text) | ||
{ | ||
foreach (var provider in _providers) | ||
{ | ||
if (provider.TryGetText(stringName, out text!)) | ||
{ | ||
return true; | ||
} | ||
} | ||
|
||
text = string.Empty; | ||
return false; | ||
} | ||
|
||
internal static bool TryGetStringName(SystemTypes systemType, out StringNames stringName) | ||
{ | ||
foreach (var provider in _providers) | ||
{ | ||
if (provider.TryGetStringName(systemType, out var stringNameNullable)) | ||
{ | ||
stringName = stringNameNullable!.Value; | ||
return true; | ||
} | ||
} | ||
|
||
stringName = default; | ||
return false; | ||
} | ||
|
||
internal static bool TryGetStringName(TaskTypes taskTypes, out StringNames stringName) | ||
{ | ||
foreach (var provider in _providers) | ||
{ | ||
if (provider.TryGetStringName(taskTypes, out var stringNameNullable)) | ||
{ | ||
stringName = stringNameNullable!.Value; | ||
return true; | ||
} | ||
} | ||
|
||
stringName = default; | ||
return false; | ||
} | ||
|
||
internal static void OnLanguageChanged(SupportedLangs newLanguage) | ||
{ | ||
foreach (var provider in _providers) | ||
{ | ||
provider.SetLanguage(newLanguage); | ||
} | ||
} | ||
} |
Oops, something went wrong.