mirror of
https://github.com/kwsch/PKHeX.git
synced 2026-08-26 03:38:26 -05:00
Finish legality string translation feature
Same " = " separator as the main form translation, uses the cache style in GameStrings to only load once. Closes #959, feel free to discuss there.
This commit is contained in:
@@ -1414,6 +1414,10 @@ private void InitializeStrings()
|
||||
string l = curlanguage;
|
||||
GameInfo.Strings = GameInfo.getStrings(l);
|
||||
|
||||
// Update Legality Strings
|
||||
// Clipboard.SetText(string.Join(Environment.NewLine, CheckStrings.getLocalization()));
|
||||
new Thread(() => { CheckStrings.setLocalization(GameInfo.getCheckStrings(l)); }).Start();
|
||||
|
||||
// Force an update to the met locations
|
||||
origintrack = GameVersion.Unknown;
|
||||
|
||||
|
||||
@@ -9,28 +9,33 @@ public static class GameInfo
|
||||
private static readonly string[] ptransp = { "ポケシフター", "Poké Transfer", "Poké Fret", "Pokétrasporto", "Poképorter", "Pokétransfer", "포케시프터", "宝可传送", "寶可傳送", "ポケシフター" };
|
||||
public static readonly string[] lang_val = { "ja", "en", "fr", "it", "de", "es", "ko", "zh", "zh2", "pt" };
|
||||
private const string DefaultLanguage = "en";
|
||||
private const string LegalityName = "legality_";
|
||||
private static readonly GameStrings[] Languages = new GameStrings[lang_val.Length];
|
||||
private static readonly string[][] CheckStrings = new string[lang_val.Length][];
|
||||
|
||||
// Lazy fetch implementation
|
||||
public static GameStrings getStrings(string lang)
|
||||
private static int DefaultLanguageIndex => Array.IndexOf(lang_val, DefaultLanguage);
|
||||
private static int getLanguageIndex(string lang)
|
||||
{
|
||||
int l = Array.IndexOf(lang_val, lang);
|
||||
if (l < 0)
|
||||
l = 1;
|
||||
return getIndex(l);
|
||||
return l < 0 ? DefaultLanguageIndex : l;
|
||||
}
|
||||
private static GameStrings getIndex(int index)
|
||||
public static GameStrings getStrings(string lang)
|
||||
{
|
||||
int index = getLanguageIndex(lang);
|
||||
return Languages[index] ?? (Languages[index] = new GameStrings(lang_val[index]));
|
||||
}
|
||||
|
||||
private static string getTransporterName(string Language)
|
||||
public static IEnumerable<string> getCheckStrings(string lang)
|
||||
{
|
||||
int lang = Array.IndexOf(lang_val, Language);
|
||||
if (lang < 0 || lang >= ptransp.Length)
|
||||
lang = Array.IndexOf(lang_val, DefaultLanguage);
|
||||
|
||||
return ptransp[lang < 0 ? 1 : lang];
|
||||
int index = getLanguageIndex(lang);
|
||||
return CheckStrings[index] ?? (CheckStrings[index] = Util.getStringList(LegalityName + lang_val[index]));
|
||||
}
|
||||
private static string getTransporterName(string lang)
|
||||
{
|
||||
int index = getLanguageIndex(lang);
|
||||
if (index >= ptransp.Length)
|
||||
index = DefaultLanguageIndex;
|
||||
return ptransp[index];
|
||||
}
|
||||
|
||||
// String providing
|
||||
|
||||
@@ -9,13 +9,27 @@ namespace PKHeX.Core
|
||||
{
|
||||
public static class CheckStrings
|
||||
{
|
||||
public const string splitter = " = ";
|
||||
public static void RefreshStrings(IEnumerable<string> lines)
|
||||
private const string splitter = " = ";
|
||||
private static readonly Type t = typeof(CheckStrings);
|
||||
private static string[] getProps(IEnumerable<string> input)
|
||||
{
|
||||
var t = typeof (CheckStrings);
|
||||
return input.Select(l => l.Substring(0, l.IndexOf(splitter, StringComparison.Ordinal))).ToArray();
|
||||
}
|
||||
private static IEnumerable<string> DumpStrings()
|
||||
{
|
||||
var props = ReflectUtil.getPropertiesStartWithPrefix(t, "V");
|
||||
return props.Select(p => $"{p}{splitter}{ReflectUtil.GetValue(t, p).ToString()}");
|
||||
}
|
||||
|
||||
public static void setLocalization(IEnumerable<string> lines)
|
||||
{
|
||||
if (lines == null)
|
||||
return;
|
||||
foreach (var line in lines.Where(l => l != null))
|
||||
{
|
||||
var index = line.IndexOf(splitter, StringComparison.Ordinal);
|
||||
if (index < 0)
|
||||
continue;
|
||||
var prop = line.Substring(0, index);
|
||||
var value = line.Substring(index + splitter.Length);
|
||||
|
||||
@@ -29,24 +43,20 @@ public static void RefreshStrings(IEnumerable<string> lines)
|
||||
}
|
||||
}
|
||||
}
|
||||
public static IEnumerable<string> DumpStrings()
|
||||
public static string[] getLocalization(string[] existingLines = null)
|
||||
{
|
||||
var t = typeof (CheckStrings);
|
||||
var props = ReflectUtil.getPropertiesStartWithPrefix(t, "V");
|
||||
return props.Select(p => $"{p}{splitter}{ReflectUtil.GetValue(t, p).ToString()}");
|
||||
}
|
||||
public static string[] UpdateLocalization(string[] lines)
|
||||
{
|
||||
List<string> list = new List<string>();
|
||||
var current = DumpStrings();
|
||||
foreach (var line in current)
|
||||
existingLines = existingLines ?? new string[0];
|
||||
var currentLines = DumpStrings().ToArray();
|
||||
var existing = getProps(existingLines);
|
||||
var current = getProps(currentLines);
|
||||
|
||||
var result = new string[currentLines.Length];
|
||||
for (int i = 0; i < current.Length; i++)
|
||||
{
|
||||
int index = line.IndexOf(splitter, StringComparison.Ordinal);
|
||||
string prop = line.Substring(0, index);
|
||||
string match = lines.FirstOrDefault(l => l.StartsWith(prop));
|
||||
list.Add(match ?? line);
|
||||
int index = Array.IndexOf(existing, current[i]);
|
||||
result[i] = index < 0 ? currentLines[i] : existingLines[index];
|
||||
}
|
||||
return list.ToArray();
|
||||
return result;
|
||||
}
|
||||
|
||||
#region General Strings
|
||||
|
||||
@@ -3523,6 +3523,7 @@
|
||||
<ItemGroup>
|
||||
<Content Include="Resources\img\misc\horohoro.png" />
|
||||
<Content Include="Resources\img\misc\vc.png" />
|
||||
<None Include="Resources\text\en\legality_en.txt" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||
|
||||
9
PKHeX/Properties/Resources.Designer.cs
generated
9
PKHeX/Properties/Resources.Designer.cs
generated
@@ -18134,6 +18134,15 @@ public class Resources {
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to V193 = testlel.
|
||||
/// </summary>
|
||||
public static string legality_en {
|
||||
get {
|
||||
return ResourceManager.GetString("legality_en", resourceCulture);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized resource of type System.Drawing.Bitmap.
|
||||
/// </summary>
|
||||
|
||||
@@ -7561,4 +7561,7 @@
|
||||
<data name="pgf" type="System.Resources.ResXFileRef, System.Windows.Forms">
|
||||
<value>..\Resources\byte\pgf.pkl;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
||||
</data>
|
||||
<data name="legality_en" type="System.Resources.ResXFileRef, System.Windows.Forms">
|
||||
<value>..\Resources\text\en\legality_en.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-16</value>
|
||||
</data>
|
||||
</root>
|
||||
BIN
PKHeX/Resources/text/en/legality_en.txt
Normal file
BIN
PKHeX/Resources/text/en/legality_en.txt
Normal file
Binary file not shown.
@@ -27,6 +27,9 @@ public static object GetValue(object obj, string propertyName)
|
||||
return pi.GetValue(obj, null);
|
||||
}
|
||||
|
||||
public static object GetValue(Type t, string propertyName) => t.GetProperty(propertyName).GetValue(null);
|
||||
public static void SetValue(Type t, string propertyName, object value) => t.GetProperty(propertyName).SetValue(null, value);
|
||||
|
||||
public static IEnumerable<string> getPropertiesStartWithPrefix(Type type, string prefix, BindingFlags flags = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public)
|
||||
{
|
||||
return type.GetProperties(flags)
|
||||
|
||||
Reference in New Issue
Block a user