Skip to content
This repository has been archived by the owner on Aug 28, 2024. It is now read-only.

Commit

Permalink
fix: make localization online.
Browse files Browse the repository at this point in the history
  • Loading branch information
ArchiDog1998 committed Oct 2, 2023
1 parent 25c2e8f commit 7578487
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 25 deletions.
4 changes: 2 additions & 2 deletions RotationSolver/Helpers/DownloadHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ private static async Task<T> DownloadOneAsync<T>(string url)
using var client = new HttpClient();
try
{
var bts = await client.GetByteArrayAsync(url);
return JsonConvert.DeserializeObject<T>(Encoding.Default.GetString(bts));
var str = await client.GetStringAsync(url);
return JsonConvert.DeserializeObject<T>(str);
}
catch (Exception ex)
{
Expand Down
38 changes: 21 additions & 17 deletions RotationSolver/Localization/LocalizationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,41 +10,45 @@ internal class LocalizationManager : IDisposable
private readonly Dictionary<string, Strings> _translations = new();
public LocalizationManager()
{
var assembly = Assembly.GetCallingAssembly();

foreach (var lang in Dalamud.Localization.ApplicableLangCodes)
{
ReadFile(lang, assembly);
}

SetLanguage(Svc.PluginInterface.UiLanguage);
Svc.PluginInterface.LanguageChanged += OnLanguageChange;
}
#if DEBUG
ExportLocalization();
#endif

private void ReadFile(string lang, Assembly assembly)
{
Stream manifestResourceStream = assembly.GetManifestResourceStream("RotationSolver.Localization." + lang + ".json");
if (manifestResourceStream == null) return;
using StreamReader streamReader = new(manifestResourceStream);
_translations[lang] = JsonConvert.DeserializeObject<Strings>(streamReader.ReadToEnd());
}

private void SetLanguage(string lang)
private async void SetLanguage(string lang)
{
if (_translations.TryGetValue(lang, out var value))
{
RightLang = value;
}
else
{
RightLang = new Strings();
try
{
var url = $"https://raw.githubusercontent.com/{Service.USERNAME}/{Service.REPO}/main/RotationSolver/Localization/{lang}.json";
using var client = new HttpClient();
RightLang = _translations[lang] = JsonConvert.DeserializeObject<Strings>(await client.GetStringAsync(url));
}
catch (HttpRequestException ex) when (ex?.StatusCode == System.Net.HttpStatusCode.NotFound)
{
Svc.Log.Information(ex, $"No language {lang}");
RightLang = new Strings();
}
catch (Exception ex)
{
Svc.Log.Warning(ex, $"Failed to download the language {lang}");
RightLang = new Strings();
}
}

RotationSolverPlugin.ChangeUITranslation();
}

#if DEBUG
public static void ExportLocalization()
private static void ExportLocalization()
{
var directory = @"E:\OneDrive - stu.zafu.edu.cn\PartTime\FFXIV\RotationSolver\RotationSolver\Localization";
if (!Directory.Exists(directory)) return;
Expand Down
2 changes: 0 additions & 2 deletions RotationSolver/RotationSolver.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@
<ProjectReference Include="..\RotationSolver.Basic\RotationSolver.Basic.csproj" />
<ProjectReference Include="..\ECommons\ECommons\ECommons.csproj" />
<ProjectReference Include="..\XIVPainter\XIVPainter\XIVPainter.csproj" />
<EmbeddedResource Include="Localization\*.json" Exclude="Localization\Localization.json" />
<EmbeddedResource Include="Logos\*.png" />

<Using Include="Dalamud.Game.ClientState.Objects.Types" />

<Using Include="RotationSolver.Basic" />
<Using Include="RotationSolver.Basic.Actions" />
<Using Include="RotationSolver.Basic.Attributes" />
Expand Down
3 changes: 0 additions & 3 deletions RotationSolver/RotationSolverPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,6 @@ public RotationSolverPlugin(DalamudPluginInterface pluginInterface)
Watcher.Enable();
OtherConfiguration.Init();
_dis.Add(new LocalizationManager());
#if DEBUG
LocalizationManager.ExportLocalization();
#endif
ChangeUITranslation();

OpenLinkPayload = pluginInterface.AddChatLinkHandler(0, (id, str) =>
Expand Down

0 comments on commit 7578487

Please sign in to comment.