// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. using System; using System.Globalization; using osu.Framework.Configuration; using osu.Framework.Localisation; using osu.Game.Localisation; namespace osu.Game.Extensions { /// /// Conversion utilities for the enum. /// public static class LanguageExtensions { /// /// Returns the culture code of the that corresponds to the supplied . /// /// /// This is required as enum member names are not allowed to contain hyphens. /// public static string ToCultureCode(this Language language) => language.ToString().Replace("_", "-"); /// /// Attempts to parse the supplied to a value. /// /// The code of the culture to parse. /// The parsed . Valid only if the return value of the method is . /// Whether the parsing succeeded. public static bool TryParseCultureCode(string cultureCode, out Language language) => Enum.TryParse(cultureCode.Replace("-", "_"), out language); /// /// Parses the that is specified in , /// or if that is not valid, the language of the current as exposed by . /// /// The current . /// The current of the . /// The parsed language. public static Language GetLanguageFor(string frameworkLocale, LocalisationParameters localisationParameters) { // the usual case when the user has changed the language if (TryParseCultureCode(frameworkLocale, out var language)) return language; if (localisationParameters.Store != null) { // startup case, locale not explicitly set, or the set language was removed in an update if (TryParseCultureCode(localisationParameters.Store.EffectiveCulture.Name, out language)) return language; } return Language.en; } } }