-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29401 from CloneWith/patch/wiki-locale
Automatically use in-game language for wiki pages
- Loading branch information
Showing
1 changed file
with
45 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
@@ -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('/'); | ||
|
@@ -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(); | ||
|
@@ -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(); | ||
|
||
|
@@ -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; | ||
|