/*
Yarn Spinner is licensed to you under the terms found in the file LICENSE.md.
*/
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
namespace Yarn.Unity
{
///
/// Provides access to all s supported by Yarn Spinner.
///
public static class Cultures
{
private static Lazy> _allCultures = new Lazy>(() => MakeCultureList());
private static Lazy> _allCulturesTable = new Lazy>(() =>
{
var dict = new Dictionary();
foreach (var entry in _allCultures.Value)
{
dict[entry.Name] = entry;
}
return dict;
});
///
/// Get all s supported by Yarn Spinner.
///
private static IEnumerable MakeCultureList() => CultureInfo.GetCultures(CultureTypes.AllCultures)
.Where(c => c.Name != "")
.Select(c => new Culture
{
Name = c.Name,
DisplayName = c.DisplayName,
NativeName = c.NativeName,
IsNeutralCulture = c.IsNeutralCulture,
})
.Append(new Culture { Name = "mi", DisplayName = "Maori", NativeName = "Māori", IsNeutralCulture = true })
.OrderBy(c => c.DisplayName);
public static IEnumerable GetCultures() => _allCultures.Value;
///
/// Returns the represented by the language code
/// in .
///
/// The name of the to
/// retrieve.
/// The .
/// Thrown when no with the given language ID can be
/// found.
[Obsolete("Use " + nameof(TryGetCulture) + ", which does not throw if the culture can't be found.")]
public static Culture GetCulture(string name)
{
var exists = _allCulturesTable.Value.TryGetValue(name, out var result);
if (exists)
{
return result;
}
else
{
throw new ArgumentException($"Culture {name} not found", name);
}
}
///
/// Gets the represented by the language code in
/// .
///
/// The name of the to
/// retrieve.
/// On return, the if one
/// was found, or a default if otherwise.
/// if a Culture was found; otherwise.
public static bool TryGetCulture(string name, out Culture culture)
{
return _allCulturesTable.Value.TryGetValue(name, out culture);
}
///
/// Returns a boolean value indicating whether
/// is a valid identifier for retrieving a from
/// .
///
///
/// if name is a valid name; otherwise.
public static bool HasCulture(string name)
{
return _allCulturesTable.Value.ContainsKey(name);
}
public static Culture CurrentNeutralCulture
{
get
{
var current = System.Globalization.CultureInfo.CurrentCulture;
if (current.IsNeutralCulture == false)
{
current = current.Parent;
}
TryGetCulture(current.Name, out var culture);
return culture;
}
}
}
}