Merge pull request #29401 from CloneWith/patch/wiki-locale

Automatically use in-game language for wiki pages
This commit is contained in:
Dan Balasescu 2024-08-13 17:18:29 +09:00 committed by GitHub
commit 513a666847
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,8 +1,6 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text. // See the LICENCE file in the repository root for full licence text.
#nullable disable
using System; using System;
using System.Linq; using System.Linq;
using System.Threading; using System.Threading;
@ -10,6 +8,7 @@ using osu.Framework.Allocation;
using osu.Framework.Bindables; using osu.Framework.Bindables;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Game.Extensions; using osu.Game.Extensions;
using osu.Game.Localisation;
using osu.Game.Online.API; using osu.Game.Online.API;
using osu.Game.Online.API.Requests; using osu.Game.Online.API.Requests;
using osu.Game.Online.API.Requests.Responses; using osu.Game.Online.API.Requests.Responses;
@ -24,25 +23,37 @@ namespace osu.Game.Overlays
public string CurrentPath => path.Value; public string CurrentPath => path.Value;
private readonly Bindable<string> path = new Bindable<string>(INDEX_PATH); private readonly Bindable<string> path = new Bindable<string>(INDEX_PATH);
private readonly Bindable<APIWikiPage?> wikiData = new Bindable<APIWikiPage?>();
private readonly Bindable<APIWikiPage> wikiData = new Bindable<APIWikiPage>(); private readonly IBindable<Language> language = new Bindable<Language>();
[Resolved] [Resolved]
private IAPIProvider api { get; set; } private IAPIProvider api { get; set; } = null!;
private GetWikiRequest request; [Resolved]
private OsuGameBase game { get; set; } = null!;
private CancellationTokenSource cancellationToken; private GetWikiRequest? request;
private CancellationTokenSource? cancellationToken;
private WikiArticlePage? articlePage;
private bool displayUpdateRequired = true; private bool displayUpdateRequired = true;
private WikiArticlePage articlePage;
public WikiOverlay() public WikiOverlay()
: base(OverlayColourScheme.Orange, false) : base(OverlayColourScheme.Orange, false)
{ {
} }
protected override void LoadComplete()
{
base.LoadComplete();
path.BindValueChanged(onPathChanged);
wikiData.BindTo(Header.WikiPageData);
language.BindTo(game.CurrentLanguage);
language.BindValueChanged(onLangChanged);
}
public void ShowPage(string pagePath = INDEX_PATH) public void ShowPage(string pagePath = INDEX_PATH)
{ {
path.Value = pagePath.Trim('/'); path.Value = pagePath.Trim('/');
@ -55,13 +66,6 @@ namespace osu.Game.Overlays
ShowParentPage = showParentPage, ShowParentPage = showParentPage,
}; };
protected override void LoadComplete()
{
base.LoadComplete();
path.BindValueChanged(onPathChanged);
wikiData.BindTo(Header.WikiPageData);
}
protected override void PopIn() protected override void PopIn()
{ {
base.PopIn(); base.PopIn();
@ -100,25 +104,18 @@ namespace osu.Game.Overlays
} }
} }
private void onPathChanged(ValueChangedEvent<string> e) private void loadPage(string path, Language lang)
{ {
// the path could change as a result of redirecting to a newer location of the same page.
// we already have the correct wiki data, so we can safely return here.
if (e.NewValue == wikiData.Value?.Path)
return;
if (e.NewValue == "error")
return;
cancellationToken?.Cancel(); cancellationToken?.Cancel();
request?.Cancel(); request?.Cancel();
string[] values = e.NewValue.Split('/', 2); // Language code + path, or just path1 + path2 in case
string[] values = path.Split('/', 2);
if (values.Length > 1 && LanguageExtensions.TryParseCultureCode(values[0], out var language)) if (values.Length > 1 && LanguageExtensions.TryParseCultureCode(values[0], out var parsedLang))
request = new GetWikiRequest(values[1], language); request = new GetWikiRequest(values[1], parsedLang);
else else
request = new GetWikiRequest(e.NewValue); request = new GetWikiRequest(path, lang);
Loading.Show(); Loading.Show();
@ -132,6 +129,25 @@ namespace osu.Game.Overlays
api.PerformAsync(request); api.PerformAsync(request);
} }
private void onPathChanged(ValueChangedEvent<string> e)
{
// the path could change as a result of redirecting to a newer location of the same page.
// we already have the correct wiki data, so we can safely return here.
if (e.NewValue == wikiData.Value?.Path)
return;
if (e.NewValue == "error")
return;
loadPage(e.NewValue, language.Value);
}
private void onLangChanged(ValueChangedEvent<Language> e)
{
// Path unmodified, just reload the page with new language value.
loadPage(path.Value, e.NewValue);
}
private void onSuccess(APIWikiPage response) private void onSuccess(APIWikiPage response)
{ {
wikiData.Value = response; wikiData.Value = response;