Files
aibis-dream/Assets/Scripts/UI/Panel/PlayTool/LocalizedObjSpriteNaming.cs
T

101 lines
3.3 KiB
C#

using System;
using System.Collections.Generic;
using AibisDream.Framework;
namespace AibisDream.UI
{
/// <summary>
/// Defines the file-name convention used by localized Obj sprites.
/// Chinese uses the unsuffixed source asset; every other supported locale uses
/// "{logicalName}-{localeCode}".
/// </summary>
internal static class LocalizedObjSpriteNaming
{
internal const string DefaultLocaleCode = "zh-Hans";
private static readonly string[] NonDefaultLocaleCodeValues =
{
"en",
"ja-JP",
"ru",
"es",
"pt-BR"
};
internal static IReadOnlyList<string> NonDefaultLocaleCodes =>
NonDefaultLocaleCodeValues;
internal static string NormalizeLogicalName(string picName)
{
if (string.IsNullOrEmpty(picName)) return picName;
return picName.EndsWith(".png", StringComparison.OrdinalIgnoreCase)
? picName.Substring(0, picName.Length - 4)
: picName;
}
internal static string GetAssetLocaleCode(string localeCode)
{
return LocalizationKit.GetLocaleKind(localeCode) switch
{
LocaleKind.En => "en",
LocaleKind.Ja => "ja-JP",
LocaleKind.Ru => "ru",
LocaleKind.Es => "es",
LocaleKind.PtBr => "pt-BR",
_ => DefaultLocaleCode
};
}
internal static string GetPicName(string logicalName, string localeCode)
{
string normalizedName = NormalizeLogicalName(logicalName);
string assetLocaleCode = GetAssetLocaleCode(localeCode);
return string.Equals(
assetLocaleCode,
DefaultLocaleCode,
StringComparison.Ordinal)
? normalizedName
: $"{normalizedName}-{assetLocaleCode}";
}
internal static IReadOnlyList<string> GetCandidatePicNames(
string logicalName,
string localeCode)
{
string normalizedName = NormalizeLogicalName(logicalName);
string localizedName = GetPicName(normalizedName, localeCode);
if (string.Equals(localizedName, normalizedName, StringComparison.Ordinal))
return new[] { normalizedName };
return new[] { localizedName, normalizedName };
}
internal static bool TryParseLocalizedVariant(
string fileNameWithoutExtension,
out string logicalName,
out string localeCode)
{
foreach (string candidateCode in NonDefaultLocaleCodeValues)
{
string suffix = $"-{candidateCode}";
if (!fileNameWithoutExtension.EndsWith(
suffix,
StringComparison.OrdinalIgnoreCase))
{
continue;
}
logicalName = fileNameWithoutExtension.Substring(
0,
fileNameWithoutExtension.Length - suffix.Length);
localeCode = candidateCode;
return true;
}
logicalName = null;
localeCode = null;
return false;
}
}
}