Localization in the works

This commit is contained in:
Kwoth 2017-02-13 10:29:34 +01:00
parent fb837235a7
commit 34c6924320

View File

@ -1,45 +1,83 @@
namespace NadekoBot.Services using Discord;
using Discord.Commands;
using NadekoBot.Extensions;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
using System;
using NadekoBot.Services.Database;
namespace NadekoBot.Services
{ {
public class Localization public class Localization
{ {
public string this[string key] => LoadCommandString(key); public ConcurrentDictionary<ulong, CultureInfo> GuildCultureInfos { get; }
public static string LoadCommandString(string key) private Localization() { }
public Localization(IDictionary<ulong, string> cultureInfoNames)
{ {
string toReturn = Resources.CommandStrings.ResourceManager.GetString(key); GuildCultureInfos = new ConcurrentDictionary<ulong, CultureInfo>(cultureInfoNames.ToDictionary(x => x.Key, x =>
return string.IsNullOrWhiteSpace(toReturn) ? key : toReturn; {
CultureInfo cultureInfo = null;
try
{
cultureInfo = new CultureInfo(x.Value);
}
catch
{
}
return cultureInfo;
}).Where(x => x.Value != null));
} }
//private static string GetCommandString(string key) public void SetGuildCulture(IGuild guild, CultureInfo ci) =>
//{ SetGuildCulture(guild.Id, ci);
// return key;
//var resx = new List<DictionaryEntry>(); public void SetGuildCulture(ulong guildId, CultureInfo ci)
//var fs = new StreamReader(File.OpenRead("./Strings.resx")); {
//Console.WriteLine(fs.ReadToEnd()); if (ci == DefaultCultureInfo)
//using (var reader = new ResourceReader(fs.BaseStream)) {
//{ RemoveGuildCulture(guildId);
// List<DictionaryEntry> existing = new List<DictionaryEntry>(); return;
// foreach (DictionaryEntry item in reader) }
// {
// existing.Add(item); using (var uow = DbHandler.UnitOfWork())
// } {
// var existingResource = resx.Where(r => r.Key.ToString() == key).FirstOrDefault(); var gc = uow.GuildConfigs.For(guildId, set => set);
// if (existingResource.Key == null) gc.Locale = ci.Name;
// { uow.Complete();
// resx.Add(new DictionaryEntry() { Key = key, Value = key }); }
// } }
// else
// return existingResource.Value.ToString(); public void RemoveGuildCulture(IGuild guild) =>
//} RemoveGuildCulture(guild.Id);
//using (var writer = new ResourceWriter(new FileStream("./Strings.resx", FileMode.OpenOrCreate)))
//{ public void RemoveGuildCulture(ulong guildId) {
// resx.ForEach(r =>
// { CultureInfo throwaway;
// writer.AddResource(r.Key.ToString(), r.Value.ToString()); if (GuildCultureInfos.TryRemove(guildId, out throwaway))
// }); {
// writer.Generate(); using (var uow = DbHandler.UnitOfWork())
//} {
//return key; var gc = uow.GuildConfigs.For(guildId, set => set);
//} gc.Locale = null;
uow.Complete();
}
}
}
public CultureInfo GetCultureInfo(IGuild guild) =>
GetCultureInfo(guild.Id);
public CultureInfo DefaultCultureInfo { get; } = CultureInfo.CurrentCulture;
public CultureInfo GetCultureInfo(ulong guildId)
{
CultureInfo info = null;
GuildCultureInfos.TryGetValue(guildId, out info);
return info ?? DefaultCultureInfo;
}
} }
} }