Skip to content

Commit

Permalink
Merge pull request #1812 from andy840119/add-utils-to-load-save-cultu…
Browse files Browse the repository at this point in the history
…re-info

Add utils to load save culture info.
  • Loading branch information
andy840119 authored Dec 27, 2022
2 parents aba9146 + 2c100a8 commit 21be8ec
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,5 @@ public void TestDeserialize(string json, int? lcid)
var result = JsonConvert.DeserializeObject<CultureInfo>(json, CreateSettings());
Assert.AreEqual(lcid, result?.LCID);
}

[Test]
public void TestAllCultureInfo()
{
var cultureInfos = CultureInfo.GetCultures(CultureTypes.AllCultures);

foreach (var cultureInfo in cultureInfos)
{
// this weird cultureInfo will let test case failed.
if (cultureInfo.LCID is 4096 or 4 or 31748)
continue;

string json = JsonConvert.SerializeObject(cultureInfo, CreateSettings());
var actual = JsonConvert.DeserializeObject<CultureInfo>(json, CreateSettings());
Assert.AreEqual(cultureInfo, actual);
}
}
}
}
40 changes: 39 additions & 1 deletion osu.Game.Rulesets.Karaoke.Tests/Utils/CultureInfoUtilsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@

namespace osu.Game.Rulesets.Karaoke.Tests.Utils
{
[Ignore("Cannot run those test cases right now because will got different results in the different platform...")]
public class CultureInfoUtilsTest
{
[Test]
[Ignore("Cannot run those test cases right now because will got different results in the different platform...")]
public void TestGetAvailableLanguages()
{
// seems there are 276 languages in the world.
Expand All @@ -21,6 +21,7 @@ public void TestGetAvailableLanguages()
}

[Test]
[Ignore("Cannot run those test cases right now because will got different results in the different platform...")]
public void TestGetAvailableLanguagesWithUniqueLcid()
{
var languages = CultureInfoUtils.GetAvailableLanguages();
Expand Down Expand Up @@ -63,11 +64,48 @@ public void TesIsLanguage(int lcid, bool isLanguage)
[TestCase(31748, "中文(繁體)")]
[TestCase(30724, "中文")]
[TestCase(1028, "中文(台灣)")]
[Ignore("Cannot run those test cases right now because will got different results in the different platform...")]
public void TestGetLanguageDisplayText(int lcid, string displayText)
{
var cultureInfo = new CultureInfo(lcid);
string actual = CultureInfoUtils.GetLanguageDisplayText(cultureInfo);
Assert.AreEqual(displayText, actual);
}

[Test]
public void TestSaveAndLoadCultureInfoById()
{
var cultureInfos = CultureInfo.GetCultures(CultureTypes.AllCultures);

foreach (var cultureInfo in cultureInfos)
{
// this weird cultureInfo will let test case failed.
if (cultureInfo.LCID is 4096 or 4 or 31748)
continue;

// get the lcid and convert back to culture info again.
int lcid = CultureInfoUtils.GetSaveCultureInfoId(cultureInfo);
var actual = CultureInfoUtils.CreateLoadCultureInfoById(lcid);
Assert.AreEqual(cultureInfo, actual);
}
}

[Test]
public void TestSaveAndLoadCultureInfoByCode()
{
var cultureInfos = CultureInfo.GetCultures(CultureTypes.AllCultures);

foreach (var cultureInfo in cultureInfos)
{
// this weird cultureInfo will let test case failed.
if (cultureInfo.LCID is 4096 or 4 or 31748)
continue;

// get the code and convert back to culture info again.
string lcid = CultureInfoUtils.GetSaveCultureInfoCode(cultureInfo);
var actual = CultureInfoUtils.CreateLoadCultureInfoByCode(lcid);
Assert.AreEqual(cultureInfo, actual);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Globalization;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using osu.Game.Rulesets.Karaoke.Utils;

namespace osu.Game.Rulesets.Karaoke.IO.Serialization.Converters
{
Expand All @@ -18,12 +19,14 @@ public class CultureInfoConverter : JsonConverter<CultureInfo>
if (value == null)
return null;

return new CultureInfo(value.Value);
return CultureInfoUtils.CreateLoadCultureInfoById(value.Value);
}

public override void WriteJson(JsonWriter writer, CultureInfo? value, JsonSerializer serializer)
{
writer.WriteValue(value?.LCID);
int? id = value != null ? CultureInfoUtils.GetSaveCultureInfoId(value) : null;

writer.WriteValue(id);
}
}
}
10 changes: 10 additions & 0 deletions osu.Game.Rulesets.Karaoke/Utils/CultureInfoUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,15 @@ public static bool IsLanguage(CultureInfo cultureInfo)

public static string GetLanguageDisplayText(CultureInfo? cultureInfo)
=> cultureInfo?.NativeName ?? "None";

public static int GetSaveCultureInfoId(CultureInfo cultureInfo)
=> cultureInfo.LCID;

public static CultureInfo CreateLoadCultureInfoById(int lcid) => new(lcid);

public static string GetSaveCultureInfoCode(CultureInfo cultureInfo)
=> cultureInfo.ToString();

public static CultureInfo CreateLoadCultureInfoByCode(string code) => new(code);
}
}

0 comments on commit 21be8ec

Please sign in to comment.