2021-04-20 08:06:01 +00:00
|
|
|
// 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.
|
|
|
|
|
2022-06-17 07:37:17 +00:00
|
|
|
#nullable disable
|
|
|
|
|
2021-04-20 08:06:01 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Globalization;
|
|
|
|
using System.IO;
|
2021-06-14 08:47:31 +00:00
|
|
|
using System.Linq;
|
2021-04-20 08:06:01 +00:00
|
|
|
using System.Resources;
|
2021-12-23 09:33:17 +00:00
|
|
|
using System.Threading;
|
2021-04-20 08:06:01 +00:00
|
|
|
using System.Threading.Tasks;
|
|
|
|
using osu.Framework.Localisation;
|
|
|
|
|
|
|
|
namespace osu.Game.Localisation
|
|
|
|
{
|
|
|
|
public class ResourceManagerLocalisationStore : ILocalisationStore
|
|
|
|
{
|
|
|
|
private readonly Dictionary<string, ResourceManager> resourceManagers = new Dictionary<string, ResourceManager>();
|
|
|
|
|
|
|
|
public ResourceManagerLocalisationStore(string cultureCode)
|
|
|
|
{
|
|
|
|
EffectiveCulture = new CultureInfo(cultureCode);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public string Get(string lookup)
|
|
|
|
{
|
2021-10-27 04:04:41 +00:00
|
|
|
string[] split = lookup.Split(':');
|
2021-04-20 08:06:01 +00:00
|
|
|
|
2021-10-20 06:03:15 +00:00
|
|
|
if (split.Length < 2)
|
|
|
|
return null;
|
|
|
|
|
2021-04-20 08:06:01 +00:00
|
|
|
string ns = split[0];
|
|
|
|
string key = split[1];
|
|
|
|
|
2021-04-21 05:39:36 +00:00
|
|
|
lock (resourceManagers)
|
2021-04-21 05:37:28 +00:00
|
|
|
{
|
2021-04-21 05:39:36 +00:00
|
|
|
if (!resourceManagers.TryGetValue(ns, out var manager))
|
2021-06-14 08:47:31 +00:00
|
|
|
{
|
|
|
|
var loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies();
|
|
|
|
|
|
|
|
// Traverse backwards through periods in the namespace to find a matching assembly.
|
|
|
|
string assemblyName = ns;
|
|
|
|
|
|
|
|
while (!string.IsNullOrEmpty(assemblyName))
|
|
|
|
{
|
|
|
|
var matchingAssembly = loadedAssemblies.FirstOrDefault(asm => asm.GetName().Name == assemblyName);
|
|
|
|
|
|
|
|
if (matchingAssembly != null)
|
|
|
|
{
|
|
|
|
resourceManagers[ns] = manager = new ResourceManager(ns, matchingAssembly);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
int lastIndex = Math.Max(0, assemblyName.LastIndexOf('.'));
|
|
|
|
assemblyName = assemblyName.Substring(0, lastIndex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (manager == null)
|
|
|
|
return null;
|
2023-03-29 05:44:42 +00:00
|
|
|
|
2023-03-31 07:23:37 +00:00
|
|
|
// When using the English culture, prefer the fallbacks rather than osu-resources baked strings.
|
|
|
|
// They are guaranteed to be up-to-date, and is also what a developer expects to see when making changes to `xxxStrings.cs` files.
|
2023-03-31 07:20:16 +00:00
|
|
|
if (EffectiveCulture.Name == @"en")
|
2023-03-29 05:44:42 +00:00
|
|
|
return null;
|
2021-04-21 05:39:36 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return manager.GetString(key, EffectiveCulture);
|
|
|
|
}
|
|
|
|
catch (MissingManifestResourceException)
|
|
|
|
{
|
|
|
|
// in the case the manifest is missing, it is likely that the user is adding code-first implementations of new localisation namespaces.
|
|
|
|
// it's fine to ignore this as localisation will fallback to default values.
|
|
|
|
return null;
|
|
|
|
}
|
2021-04-21 05:37:28 +00:00
|
|
|
}
|
2021-04-20 08:06:01 +00:00
|
|
|
}
|
|
|
|
|
2021-12-23 09:33:17 +00:00
|
|
|
public Task<string> GetAsync(string lookup, CancellationToken cancellationToken = default)
|
2021-04-20 08:06:01 +00:00
|
|
|
{
|
|
|
|
return Task.FromResult(Get(lookup));
|
|
|
|
}
|
|
|
|
|
|
|
|
public Stream GetStream(string name)
|
|
|
|
{
|
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
public IEnumerable<string> GetAvailableResources()
|
|
|
|
{
|
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
public CultureInfo EffectiveCulture { get; }
|
|
|
|
}
|
|
|
|
}
|