From 7cbb4ab6f1c689f7f57d1ca6f046d4b49cdd1d65 Mon Sep 17 00:00:00 2001 From: CloneWith Date: Tue, 13 Aug 2024 11:50:33 +0800 Subject: [PATCH 1/3] Get in-game locale for wiki pages --- osu.Game/Overlays/WikiOverlay.cs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/osu.Game/Overlays/WikiOverlay.cs b/osu.Game/Overlays/WikiOverlay.cs index ffbc168fb7..88450ea6db 100644 --- a/osu.Game/Overlays/WikiOverlay.cs +++ b/osu.Game/Overlays/WikiOverlay.cs @@ -10,6 +10,7 @@ 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; @@ -38,11 +39,20 @@ namespace osu.Game.Overlays private WikiArticlePage articlePage; + private Bindable language; + public WikiOverlay() : base(OverlayColourScheme.Orange, false) { } + [BackgroundDependencyLoader] + private void load(OsuGameBase game) + { + // Fetch current language on load for translated pages (if possible) + language = game.CurrentLanguage.GetBoundCopy(); + } + public void ShowPage(string pagePath = INDEX_PATH) { path.Value = pagePath.Trim('/'); @@ -113,12 +123,13 @@ namespace osu.Game.Overlays cancellationToken?.Cancel(); request?.Cancel(); + // Language code + path, or just path1 + path2 in case string[] values = e.NewValue.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 lang)) + request = new GetWikiRequest(values[1], lang); else - request = new GetWikiRequest(e.NewValue); + request = new GetWikiRequest(e.NewValue, language.Value); Loading.Show(); From ca91726190636314d46b9bddd6f865dd870781a2 Mon Sep 17 00:00:00 2001 From: CloneWith Date: Tue, 13 Aug 2024 13:34:11 +0800 Subject: [PATCH 2/3] Reload wiki page on language change --- osu.Game/Overlays/WikiOverlay.cs | 39 +++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/osu.Game/Overlays/WikiOverlay.cs b/osu.Game/Overlays/WikiOverlay.cs index 88450ea6db..0587832533 100644 --- a/osu.Game/Overlays/WikiOverlay.cs +++ b/osu.Game/Overlays/WikiOverlay.cs @@ -68,7 +68,9 @@ namespace osu.Game.Overlays protected override void LoadComplete() { base.LoadComplete(); + path.BindValueChanged(onPathChanged); + language.BindValueChanged(onLangChanged); wikiData.BindTo(Header.WikiPageData); } @@ -110,26 +112,18 @@ namespace osu.Game.Overlays } } - private void onPathChanged(ValueChangedEvent 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(); // Language code + path, or just path1 + path2 in case - string[] values = e.NewValue.Split('/', 2); + string[] values = path.Split('/', 2); - if (values.Length > 1 && LanguageExtensions.TryParseCultureCode(values[0], out var lang)) - request = new GetWikiRequest(values[1], lang); + if (values.Length > 1 && LanguageExtensions.TryParseCultureCode(values[0], out var parsedLang)) + request = new GetWikiRequest(values[1], parsedLang); else - request = new GetWikiRequest(e.NewValue, language.Value); + request = new GetWikiRequest(path, lang); Loading.Show(); @@ -143,6 +137,25 @@ namespace osu.Game.Overlays api.PerformAsync(request); } + private void onPathChanged(ValueChangedEvent 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 e) + { + // Path unmodified, just reload the page with new language value. + loadPage(path.Value, e.NewValue); + } + private void onSuccess(APIWikiPage response) { wikiData.Value = response; From 3f02869bcc7455357910f716aa1f5841fda7c2ac Mon Sep 17 00:00:00 2001 From: Dan Balasescu Date: Tue, 13 Aug 2024 16:08:06 +0900 Subject: [PATCH 3/3] Enable NRT while we're here --- osu.Game/Overlays/WikiOverlay.cs | 40 +++++++++++++------------------- 1 file changed, 16 insertions(+), 24 deletions(-) diff --git a/osu.Game/Overlays/WikiOverlay.cs b/osu.Game/Overlays/WikiOverlay.cs index 0587832533..14a25a909d 100644 --- a/osu.Game/Overlays/WikiOverlay.cs +++ b/osu.Game/Overlays/WikiOverlay.cs @@ -1,8 +1,6 @@ // Copyright (c) ppy Pty Ltd . 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; @@ -25,32 +23,35 @@ namespace osu.Game.Overlays public string CurrentPath => path.Value; private readonly Bindable path = new Bindable(INDEX_PATH); - - private readonly Bindable wikiData = new Bindable(); + private readonly Bindable wikiData = new Bindable(); + private readonly IBindable language = new Bindable(); [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; - - private Bindable language; - public WikiOverlay() : base(OverlayColourScheme.Orange, false) { } - [BackgroundDependencyLoader] - private void load(OsuGameBase game) + protected override void LoadComplete() { - // Fetch current language on load for translated pages (if possible) - language = game.CurrentLanguage.GetBoundCopy(); + base.LoadComplete(); + + path.BindValueChanged(onPathChanged); + wikiData.BindTo(Header.WikiPageData); + + language.BindTo(game.CurrentLanguage); + language.BindValueChanged(onLangChanged); } public void ShowPage(string pagePath = INDEX_PATH) @@ -65,15 +66,6 @@ namespace osu.Game.Overlays ShowParentPage = showParentPage, }; - protected override void LoadComplete() - { - base.LoadComplete(); - - path.BindValueChanged(onPathChanged); - language.BindValueChanged(onLangChanged); - wikiData.BindTo(Header.WikiPageData); - } - protected override void PopIn() { base.PopIn();