Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simple localisation for framework-side strings #6075

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using System;
using System.Collections.Generic;
using NUnit.Framework;
using osu.Framework.Allocation;
using osu.Framework.Configuration;
using osu.Framework.Extensions.LocalisationExtensions;
using osu.Framework.Graphics.UserInterface;
using osu.Framework.Localisation.Strings;

namespace osu.Framework.Tests.Visual.Localisation
{
public partial class TestSceneFrameworkStringLocalisation : LocalisationTestScene
{
[BackgroundDependencyLoader]
private void load()
{
Manager.AddLanguage("en-US", new TestLocalisationStore("en-US", new Dictionary<string, string>
{
// no translations, use the fallback value
}));

Manager.AddLanguage("en-GB", new TestLocalisationStore("en-GB", new Dictionary<string, string>
{
[WindowModeStrings.Windowed.GetTranslationKey()] = "Windowed",
[WindowModeStrings.Borderless.GetTranslationKey()] = "Borderless",
[WindowModeStrings.Fullscreen.GetTranslationKey()] = "Full screen",
}));

Manager.AddLanguage("hr-HR", new TestLocalisationStore("hr-HR", new Dictionary<string, string>
{
[WindowModeStrings.Windowed.GetTranslationKey()] = "Prozor",
[WindowModeStrings.Borderless.GetTranslationKey()] = "Bez ruba",
[WindowModeStrings.Fullscreen.GetTranslationKey()] = "Puni zaslon",
}));
}

[Test]
public void TestEnumDropdown()
{
BasicDropdown<WindowMode> dropdown = null!;

AddStep("add dropdown", () =>
{
Child = dropdown = new BasicDropdown<WindowMode>
{
Width = 200,
Items = Enum.GetValues<WindowMode>()
};
});
AddStep("open dropdown", () => dropdown.Menu.Open());
SetLocale("en-US");
SetLocale("en-GB");
SetLocale("hr-HR");
}
}
}
8 changes: 8 additions & 0 deletions osu.Framework/Configuration/WindowMode.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using osu.Framework.Localisation;
using osu.Framework.Localisation.Strings;

namespace osu.Framework.Configuration
{
public enum WindowMode
{
[LocalisableDescription(typeof(WindowModeStrings), nameof(WindowModeStrings.Windowed))]
Windowed = 0,

[LocalisableDescription(typeof(WindowModeStrings), nameof(WindowModeStrings.Borderless))]
Borderless = 1,

[LocalisableDescription(typeof(WindowModeStrings), nameof(WindowModeStrings.Fullscreen))]
Fullscreen = 2
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,22 @@ public static class LocalisableStringExtensions
/// <param name="data">The string data.</param>
/// <returns>A case transformable string with its string data transformed to sentence case.</returns>
public static CaseTransformableString ToSentence(this ILocalisableStringData data) => new LocalisableString(data).ToSentence();

/// <summary>
/// Returns the <see cref="TranslatableString.Key"/> of the <see cref="TranslatableString"/> that is the underlying data of this <see cref="LocalisableString"/>.
/// </summary>
/// <param name="str">
/// The <see cref="LocalisableString"/> whose underlying data is the <see cref="TranslatableString"/>.
/// This is usually a static property of a "strings" class containing translation definitions.
/// </param>
/// <returns>The <see cref="TranslatableString.Key"/> of the underlying <see cref="TranslatableString"/>.</returns>
/// <exception cref="ArgumentException">Thrown if this <see cref="LocalisableString"/> doesn't have underlying <see cref="TranslatableString"/> data.</exception>
public static string GetTranslationKey(this LocalisableString str)
{
if (str.Data is not TranslatableString translatable)
throw new ArgumentException($"The {nameof(LocalisableString)} doesn't have underlying {nameof(TranslatableString)} data.", nameof(str));

return translatable.Key;
}
}
}
27 changes: 27 additions & 0 deletions osu.Framework/Localisation/Strings/WindowModeStrings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

namespace osu.Framework.Localisation.Strings
{
public static class WindowModeStrings
{
private const string prefix = "osu.Framework.Resources.Localisation.WindowMode";

/// <summary>
/// "Windowed"
/// </summary>
public static LocalisableString Windowed => new TranslatableString(getKey("windowed"), "Windowed");

/// <summary>
/// "Borderless"
/// </summary>
public static LocalisableString Borderless => new TranslatableString(getKey("borderless"), "Borderless");

/// <summary>
/// "Fullscreen"
/// </summary>
public static LocalisableString Fullscreen => new TranslatableString(getKey("fullscreen"), "Fullscreen");

private static string getKey(string key) => $"{prefix}:{key}";
}
}
Loading