Skip to content

Commit

Permalink
Merge pull request #29401 from CloneWith/patch/wiki-locale
Browse files Browse the repository at this point in the history
Automatically use in-game language for wiki pages
  • Loading branch information
smoogipoo authored Aug 13, 2024
2 parents 58354e3 + 3f02869 commit 513a666
Showing 1 changed file with 45 additions and 29 deletions.
74 changes: 45 additions & 29 deletions osu.Game/Overlays/WikiOverlay.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

#nullable disable

using System;
using System.Linq;
using System.Threading;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Game.Extensions;
using osu.Game.Localisation;
using osu.Game.Online.API;
using osu.Game.Online.API.Requests;
using osu.Game.Online.API.Requests.Responses;
Expand All @@ -24,25 +23,37 @@ public partial class WikiOverlay : OnlineOverlay<WikiHeader>
public string CurrentPath => path.Value;

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]
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 WikiArticlePage articlePage;

public WikiOverlay()
: 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)
{
path.Value = pagePath.Trim('/');
Expand All @@ -55,13 +66,6 @@ public void ShowPage(string pagePath = INDEX_PATH)
ShowParentPage = showParentPage,
};

protected override void LoadComplete()
{
base.LoadComplete();
path.BindValueChanged(onPathChanged);
wikiData.BindTo(Header.WikiPageData);
}

protected override void PopIn()
{
base.PopIn();
Expand Down Expand Up @@ -100,25 +104,18 @@ protected override void UpdateAfterChildren()
}
}

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();
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))
request = new GetWikiRequest(values[1], language);
if (values.Length > 1 && LanguageExtensions.TryParseCultureCode(values[0], out var parsedLang))
request = new GetWikiRequest(values[1], parsedLang);
else
request = new GetWikiRequest(e.NewValue);
request = new GetWikiRequest(path, lang);

Loading.Show();

Expand All @@ -132,6 +129,25 @@ private void onPathChanged(ValueChangedEvent<string> e)
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)
{
wikiData.Value = response;
Expand Down

0 comments on commit 513a666

Please sign in to comment.