1
0
mirror of https://github.com/ppy/osu.git synced 2025-02-26 18:02:57 +08:00

Add a fallback for humanizer localization failure

This commit is contained in:
David Zhao 2019-07-30 13:58:08 +09:00
parent 6f2edaad55
commit 1956098166
3 changed files with 40 additions and 4 deletions

View File

@ -2,11 +2,11 @@
// See the LICENCE file in the repository root for full licence text.
using System;
using Humanizer;
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Cursor;
using osu.Game.Graphics.Sprites;
using osu.Game.Utils;
namespace osu.Game.Graphics
{
@ -71,7 +71,7 @@ namespace osu.Game.Graphics
Scheduler.AddDelayed(updateTimeWithReschedule, timeUntilNextUpdate);
}
protected virtual string Format() => Date.Humanize();
protected virtual string Format() => HumanizerUtils.Humanize(Date);
private void updateTime() => Text = Format();

View File

@ -1,7 +1,6 @@
// 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.
using Humanizer;
using osu.Framework.Allocation;
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics;
@ -14,6 +13,7 @@ using osu.Game.Graphics.Sprites;
using osu.Game.Online.Leaderboards;
using osu.Game.Scoring;
using osu.Game.Users.Drawables;
using osu.Game.Utils;
using osuTK;
using osuTK.Graphics;
@ -132,7 +132,7 @@ namespace osu.Game.Overlays.BeatmapSet.Scores
{
avatar.User = value.User;
flag.Country = value.User.Country;
date.Text = $@"achieved {value.Date.Humanize()}";
date.Text = $@"achieved {HumanizerUtils.Humanize(value.Date)}";
usernameText.Clear();
usernameText.AddUserLink(value.User);

View File

@ -0,0 +1,36 @@
// 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.
using System;
using System.Globalization;
using Humanizer;
namespace osu.Game.Utils
{
public static class HumanizerUtils
{
/// <summary>
/// Humanizes a string using the system culture, then falls back if one cannot be found.
/// <remarks>
/// A localization lookup failure will throw an exception of type <see cref="ArgumentException"/>
/// </remarks>
/// </summary>
/// <param name="dateTimeOffset">The time to humanize.</param>
/// <returns>A humanized string of the given time.</returns>
public static string Humanize(DateTimeOffset dateTimeOffset)
{
string offset;
try
{
offset = dateTimeOffset.Humanize();
}
catch (ArgumentException)
{
offset = dateTimeOffset.Humanize(culture: new CultureInfo("en-US"));
}
return offset;
}
}
}