Skip to content

Commit

Permalink
Reload translations when the language is changed
Browse files Browse the repository at this point in the history
  • Loading branch information
Bl4ckSh4rk committed Jan 29, 2024
1 parent 5ab9995 commit 0932326
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 53 deletions.
2 changes: 1 addition & 1 deletion Test/Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
<PackageReference Include="NUnit.Analyzers" Version="3.6.1" />
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="PKHeX.Core" Version="23.10.12" />
<PackageReference Include="PKHeX.Core" Version="24.1.12" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion WC3Plugin/Forms/ECBForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public ECBForm(SAV3 sav)

InitializeComponent();

if (!(ecb = sav.ExportECB()).IsEmpty())
if (!MysteryDataUtil.IsEmpty(ecb = sav.ExportECB()))
{
TitleBox.Text = sav.EBerryName.Trim();
ECBExportButton.Enabled = true;
Expand Down
2 changes: 1 addition & 1 deletion WC3Plugin/Forms/ECTForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public ECTForm(SAV3 sav)

InitializeComponent();

if (!(ect = sav.ExportECT()).IsEmpty())
if (!MysteryDataUtil.IsEmpty(ect = sav.ExportECT()))
{
TitleBox.Text = StringConverter3.GetString(ect.AsSpan(4, sav.Japanese ? 5 : 7), sav.Japanese).Trim();
ECTExportButton.Enabled= true;
Expand Down
20 changes: 4 additions & 16 deletions WC3Plugin/LocalizationUtil.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,14 @@
using PKHeX.Core;
namespace WC3Plugin;

namespace WC3Plugin;

// Partially based on PKHeX's LocalizationUtil, thanks Kaphotics!
// https://github.com/kwsch/PKHeX/blob/master/PKHeX.Core/Util/ResourceUtil.cs
// https://github.com/kwsch/PKHeX/blob/master/PKHeX.Core/Util/Localization/LocalizationUtil.cs
public static class LocalizationUtil
{
private const string TranslationSplitter = " = ";
private const string StringCachePrefix = nameof(WC3Plugin); // to distinguish from cashed PKHeX resources
private const string LineSplitter = "\n";

public static void SetLocalization(string currentCultureCode)
{
SetLocalization(GetStringList($"lang_{currentCultureCode}"));
}

private static string[] GetStringList(string fileName)
{
if (Util.IsStringListCached($"{StringCachePrefix}_{fileName}", out var result))
return result;
var txt = Properties.Resources.ResourceManager.GetObject(fileName)?.ToString();
return Util.LoadStringList($"{StringCachePrefix}_{fileName}", txt);
var txt = Properties.Resources.ResourceManager.GetObject($"lang_{currentCultureCode}")?.ToString();
SetLocalization(txt == null ? [] : txt.Split(LineSplitter, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries));
}

private static void SetLocalization(IReadOnlyCollection<string> lines)
Expand Down
27 changes: 9 additions & 18 deletions WC3Plugin/MysteryDataUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static byte[] ExportWC3(this SAV3 sav)

public static bool HasWC3(this SAV3 sav)
{
return sav is IGen3Wonder wonder && !wonder.WonderCard.Data.IsEmpty();
return sav is IGen3Wonder wonder && !IsEmpty(wonder.WonderCard.Data);
}

public static int GetWC3FileSize(this SAV3 sav) => GetWC3ScriptOffset(sav) + MysteryEvent3.SIZE;
Expand Down Expand Up @@ -93,8 +93,8 @@ public static byte[] ExportME3(this SAV3 sav)
public static bool HasME3(this SAV3 sav)
{
return sav is SAV3RS
? !sav.MysteryData.Data.IsEmpty()
: sav is IGen3Wonder wonder && !sav.MysteryData.Data.IsEmpty() && wonder.WonderCard.Data.IsEmpty();
? !IsEmpty(sav.MysteryData.Data)
: sav is IGen3Wonder wonder && !IsEmpty(sav.MysteryData.Data) && IsEmpty(wonder.WonderCard.Data);
}
#endregion ME3

Expand Down Expand Up @@ -122,7 +122,7 @@ public static byte[] ExportWN3(this SAV3 sav)

public static bool HasWN3(this SAV3 sav)
{
return sav is IGen3Wonder wonder && !wonder.WonderNews.Data.IsEmpty();
return sav is IGen3Wonder wonder && !IsEmpty(wonder.WonderNews.Data);
}

public static int GetWN3FileSize(this SAV3 sav) => sav.Japanese ? WonderNews3.SIZE_JAP : WonderNews3.SIZE;
Expand All @@ -141,7 +141,7 @@ public static byte[] ExportECT(this SAV3 sav)

public static bool HasECT(this SAV3 sav)
{
return !sav.EReaderTrainer().IsEmpty();
return !IsEmpty(sav.EReaderTrainer());
}

public static int GetECTFileSize(this SAV3 _) => ECT_SIZE;
Expand All @@ -158,10 +158,10 @@ private static uint GetECTChecksum(byte[] data)

for (int i = 0; i < ECT_SIZE - 4; i += 4)
{
chk += data[i] + (uint)(data[i + 1] << 8) + (uint)(data[i + 2] << 16) + (uint)(data[i + 3] << 24);
chk += ReadUInt32LittleEndian(data.AsSpan(i));
}

return chk & 0xFFFFFFFF;
return chk;
}

private const int ECT_SIZE = 188;
Expand All @@ -181,7 +181,7 @@ public static byte[] ExportECB(this SAV3 sav)

public static bool HasECB(this SAV3 sav)
{
return !sav.EReaderBerry().IsEmpty();
return !IsEmpty(sav.EReaderBerry());
}

public static int GetECBFileSize(this SAV3 sav) => sav is SAV3RS ? ECB_SIZE_RS : ECB_SIZE_FRLGE;
Expand Down Expand Up @@ -256,16 +256,7 @@ public static List<ComboItem> GetRecordMixingItemDataSource(this SAV3 sav) => [
#endregion RM3

#region Utility
internal static bool IsEmpty(this byte[] data)
{
foreach (byte b in data)
{
if (b is not (0 or 0xFF))
return false;
}
return true;
}
internal static bool IsEmpty(this Span<byte> data)
internal static bool IsEmpty(Span<byte> data)
{
foreach (byte b in data)
{
Expand Down
3 changes: 3 additions & 0 deletions WC3Plugin/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@
<data name="lang_de" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\txt\lang_de.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-8</value>
</data>
<data name="lang_en" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\txt\lang_en.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-8</value>
</data>
<data name="lang_es" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\txt\lang_es.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-8</value>
</data>
Expand Down
8 changes: 4 additions & 4 deletions WC3Plugin/Resources/txt/lang_de.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
PluginName = WC3 Plugin
MysteryGift = Mystery Gift
MysteryEvent = Mystery Event
MysteryGift = Geheimgeschenk
MysteryEvent = Geheimgeschehen
ECardTrainer = e-Card Trainer
ECardBerry = e-Card Berry
WonderNews = Wonder News
ECardBerry = e-Card Beere
WonderNews = Wundernachricht
Import = Importieren
Export = Exportieren
AllFiles = Alle Dateien
Expand Down
22 changes: 22 additions & 0 deletions WC3Plugin/Resources/txt/lang_en.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
PluginName = WC3 Plugin
MysteryGift = Mystery Gift
MysteryEvent = Mystery Event
ECardTrainer = e-Card Trainer
ECardBerry = e-Card Berry
WonderNews = Wonder News
Import = Import
Export = Export
AllFiles = All files
Error = Error
Success = Success
InvalidFileSize = Invalid file size ({0} bytes). Expected {1} bytes.
OpenFile = Open {0} file
SaveFile = Save {0} file
ReadFileError = Unable to read {0} file.
WriteFileError = Unable to write {0} data to file.
FileImported = {0} imported!
FileExported = {0} exported to {1}!
RecordMixing = Record Mixing
Item = Item
Count = Count
Save = Save
34 changes: 24 additions & 10 deletions WC3Plugin/WC3Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace WC3Plugin;

public class WC3Plugin : IPlugin
{
public string Name => TranslationStrings.PluginName;
public string Name => nameof(WC3Plugin);
public int Priority => 1; // Loading order, lowest is first.
public ISaveFileProvider SaveFileEditor { get; private set; } = null!;

Expand All @@ -19,10 +19,9 @@ public class WC3Plugin : IPlugin

public void Initialize(params object[] args)
{
Console.WriteLine($"Loading {nameof(WC3Plugin)}...");
Console.WriteLine($"Loading {Name}...");
if (args != null)
{
LocalizationUtil.SetLocalization(GameInfo.CurrentLanguage);
SaveFileEditor = (ISaveFileProvider)Array.Find(args, z => z is ISaveFileProvider)!;

LoadMenuStrip((ToolStrip)Array.Find(args, z => z is ToolStrip)!);
Expand All @@ -39,34 +38,36 @@ private void LoadMenuStrip(ToolStrip menuStrip)

private void AddPluginControl(ToolStripDropDownItem tools)
{
ctrl = new(Name) { Visible = false, Image = Properties.Resources.icon };
ctrl = new() { Visible = false, Image = Properties.Resources.icon };
_ = (tools?.DropDownItems.Add(ctrl));

wc3 = new($"{TranslationStrings.MysteryGift} (WC3)") { Visible = false, Image = Properties.Resources.icon };
wc3 = new() { Visible = false, Image = Properties.Resources.icon };
wc3.Click += (s, e) => { _ = new WC3Form((SAV3)SaveFileEditor.SAV).ShowDialog(); };
_ = ctrl.DropDownItems.Add(wc3);

me3 = new($"{TranslationStrings.MysteryEvent} (ME3)") { Visible = false, Image = Properties.Resources.me3 };
me3 = new() { Visible = false, Image = Properties.Resources.me3 };
me3.Click += (s, e) => { _ = new ME3Form((SAV3)SaveFileEditor.SAV).ShowDialog(); };
_ = ctrl.DropDownItems.Add(me3);

ect = new($"{TranslationStrings.ECardTrainer} (ECT)") { Image = Properties.Resources.ect };
ect = new() { Image = Properties.Resources.ect };
ect.Click += (s, e) => { _ = new ECTForm((SAV3)SaveFileEditor.SAV).ShowDialog(); };
_ = ctrl.DropDownItems.Add(ect);

ecb = new($"{TranslationStrings.ECardBerry} (ECB)") { Image = Properties.Resources.ecb };
ecb = new() { Image = Properties.Resources.ecb };
ecb.Click += (s, e) => { _ = new ECBForm((SAV3)SaveFileEditor.SAV).ShowDialog(); };
_ = ctrl.DropDownItems.Add(ecb);

wn3 = new($"{TranslationStrings.WonderNews} (WN3)") { Visible = false, Image = Properties.Resources.wn3 };
wn3 = new() { Visible = false, Image = Properties.Resources.wn3 };
wn3.Click += (s, e) => { _ = new WN3Form((SAV3)SaveFileEditor.SAV).ShowDialog(); };
_ = ctrl.DropDownItems.Add(wn3);

_ = ctrl.DropDownItems.Add(rm3Separator);

rm3 = new(TranslationStrings.RecordMixing) { Visible = false, Image = Properties.Resources.rm3 };
rm3 = new() { Visible = false, Image = Properties.Resources.rm3 };
rm3.Click += (s, e) => { _ = new RM3Form((SAV3)SaveFileEditor.SAV).ShowDialog(); };
_ = ctrl.DropDownItems.Add(rm3);

NotifyDisplayLanguageChanged(GameInfo.CurrentLanguage);
}

public void NotifySaveLoaded()
Expand All @@ -80,5 +81,18 @@ public void NotifySaveLoaded()
}
}

public void NotifyDisplayLanguageChanged(string language)
{
LocalizationUtil.SetLocalization(language);

ctrl.Text = TranslationStrings.PluginName;
wc3.Text = $"{TranslationStrings.MysteryGift} (WC3)";
me3.Text = $"{TranslationStrings.MysteryEvent} (ME3)";
ect.Text = $"{TranslationStrings.ECardTrainer} (ECT)";
ecb.Text = $"{TranslationStrings.ECardBerry} (ECB)";
wn3.Text = $"{TranslationStrings.WonderNews} (WN3)";
rm3.Text = TranslationStrings.RecordMixing;
}

public bool TryLoadFile(string filePath) => false;
}
4 changes: 2 additions & 2 deletions WC3Plugin/WC3Plugin.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<Description>WC3 Plugin</Description>
<FileVersion>2.2.0.0</FileVersion>
<FileVersion>2.2.1.0</FileVersion>
<TargetFramework>net8.0-windows</TargetFramework>
<LangVersion>12</LangVersion>
<UseWindowsForms>true</UseWindowsForms>
Expand All @@ -11,7 +11,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="PKHeX.Core" Version="23.10.12" />
<PackageReference Include="PKHeX.Core" Version="24.1.12" />
</ItemGroup>

</Project>

0 comments on commit 0932326

Please sign in to comment.