mirror of
https://github.com/Jetsparrow/karmabot.git
synced 2026-01-21 09:06:09 +03:00
Allow adding of common names of locales to locale files
This commit is contained in:
parent
d20cc89f28
commit
51dce7f588
@ -23,7 +23,12 @@ namespace JetKarmaBot.Commands
|
|||||||
replyToMessageId: args.Message.MessageId);
|
replyToMessageId: args.Message.MessageId);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
db.Chats.Find(args.Message.Chat.Id).Locale = cmd.Parameters[0];
|
string localeId;
|
||||||
|
if (Locale.ContainsLocale(cmd.Parameters[0]))
|
||||||
|
localeId = cmd.Parameters[0];
|
||||||
|
else
|
||||||
|
localeId = Locale.FindByCommonName(cmd.Parameters[0]).Name;
|
||||||
|
db.Chats.Find(args.Message.Chat.Id).Locale = localeId;
|
||||||
db.SaveChanges();
|
db.SaveChanges();
|
||||||
|
|
||||||
currentLocale = Locale[db.Chats.Find(args.Message.Chat.Id).Locale];
|
currentLocale = Locale[db.Chats.Find(args.Message.Chat.Id).Locale];
|
||||||
|
|||||||
@ -9,7 +9,7 @@ namespace JetKarmaBot
|
|||||||
{
|
{
|
||||||
public class Localization
|
public class Localization
|
||||||
{
|
{
|
||||||
private Dictionary<string, Dictionary<string, string>> locales = new Dictionary<string, Dictionary<string, string>>();
|
private Dictionary<string, Locale> locales = new Dictionary<string, Locale>();
|
||||||
|
|
||||||
[Inject]
|
[Inject]
|
||||||
public Localization(Config cfg)
|
public Localization(Config cfg)
|
||||||
@ -25,7 +25,7 @@ namespace JetKarmaBot
|
|||||||
{
|
{
|
||||||
string langName = Path.GetFileNameWithoutExtension(langFilePath);
|
string langName = Path.GetFileNameWithoutExtension(langFilePath);
|
||||||
string langKey = langName.ToLowerInvariant();
|
string langKey = langName.ToLowerInvariant();
|
||||||
locales[langKey] = JObject.Parse(File.ReadAllText(langFilePath)).ToObject<Dictionary<string, string>>();
|
locales[langKey] = new Locale(JObject.Parse(File.ReadAllText(langFilePath)), langKey);
|
||||||
Log("Found " + langName);
|
Log("Found " + langName);
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
@ -46,9 +46,20 @@ namespace JetKarmaBot
|
|||||||
get
|
get
|
||||||
{
|
{
|
||||||
locale = locale.ToLowerInvariant();
|
locale = locale.ToLowerInvariant();
|
||||||
return new Locale(locales[locale], locale);
|
return locales[locale];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
public Locale FindByCommonName(string name)
|
||||||
|
{
|
||||||
|
foreach (Locale l in locales.Values)
|
||||||
|
{
|
||||||
|
if (l.CommonNames.Contains(name))
|
||||||
|
{
|
||||||
|
return l;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
public bool ContainsLocale(string locale)
|
public bool ContainsLocale(string locale)
|
||||||
{
|
{
|
||||||
locale = locale.ToLowerInvariant();
|
locale = locale.ToLowerInvariant();
|
||||||
@ -64,11 +75,16 @@ namespace JetKarmaBot
|
|||||||
{
|
{
|
||||||
private Dictionary<string, string> locale;
|
private Dictionary<string, string> locale;
|
||||||
private string localeName;
|
private string localeName;
|
||||||
public Locale(Dictionary<string, string> locale, string localeName)
|
private string[] commonNames;
|
||||||
|
|
||||||
|
public Locale(JObject locale, string localeName)
|
||||||
{
|
{
|
||||||
this.locale = locale;
|
this.locale = locale.Property("strings").Value.ToObject<Dictionary<string, string>>();
|
||||||
this.localeName = localeName;
|
this.localeName = localeName;
|
||||||
|
this.commonNames = locale.Property("names").Value.ToObject<string[]>();
|
||||||
}
|
}
|
||||||
|
public string[] CommonNames => commonNames;
|
||||||
|
public string Name => localeName;
|
||||||
public string this[string name] => locale.ContainsKey(name) ? locale[name] : "unknown";
|
public string this[string name] => locale.ContainsKey(name) ? locale[name] : "unknown";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,4 +1,8 @@
|
|||||||
{
|
{
|
||||||
|
"names": [
|
||||||
|
"english"
|
||||||
|
],
|
||||||
|
"strings": {
|
||||||
"jetkarmabot.award.errawardnoreply": "Please use this command in reply to another user.",
|
"jetkarmabot.award.errawardnoreply": "Please use this command in reply to another user.",
|
||||||
"jetkarmabot.award.errawardself": "Please stop playing with yourself.",
|
"jetkarmabot.award.errawardself": "Please stop playing with yourself.",
|
||||||
"jetkarmabot.award.errawardbot": "I am a bot, and have no use for your foolish fake internet points.",
|
"jetkarmabot.award.errawardbot": "I am a bot, and have no use for your foolish fake internet points.",
|
||||||
@ -11,3 +15,4 @@
|
|||||||
"jetkarmabot.changelocale.justchanged": "Roger that.",
|
"jetkarmabot.changelocale.justchanged": "Roger that.",
|
||||||
"jetkarmabot.changelocale.getlocale": "I'm currently speaking English."
|
"jetkarmabot.changelocale.getlocale": "I'm currently speaking English."
|
||||||
}
|
}
|
||||||
|
}
|
||||||
@ -1,4 +1,9 @@
|
|||||||
{
|
{
|
||||||
|
"names": [
|
||||||
|
"russian",
|
||||||
|
"русский"
|
||||||
|
],
|
||||||
|
"strings": {
|
||||||
"jetkarmabot.award.errawardnoreply": "Пожалуйста используйте эту команду в ответе другому пользователю.",
|
"jetkarmabot.award.errawardnoreply": "Пожалуйста используйте эту команду в ответе другому пользователю.",
|
||||||
"jetkarmabot.award.errawardself": "Хватит с собой играться.",
|
"jetkarmabot.award.errawardself": "Хватит с собой играться.",
|
||||||
"jetkarmabot.award.errawardbot": "Я бот, и меня ваши жалкие пузомерки не прельщают.",
|
"jetkarmabot.award.errawardbot": "Я бот, и меня ваши жалкие пузомерки не прельщают.",
|
||||||
@ -11,3 +16,4 @@
|
|||||||
"jetkarmabot.changelocale.justchanged": "Так точно.",
|
"jetkarmabot.changelocale.justchanged": "Так точно.",
|
||||||
"jetkarmabot.changelocale.getlocale": "Я сейчас говорю по-русски."
|
"jetkarmabot.changelocale.getlocale": "Я сейчас говорю по-русски."
|
||||||
}
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user