From 2c58f86524b168f30504110091150229094e4431 Mon Sep 17 00:00:00 2001 From: FlorianDahn Date: Sat, 25 Dec 2021 16:10:32 +0100 Subject: [PATCH] Adding localization selection step to BotBaseBuilder - Adding localization step - Updating test project --- TelegramBotBase/Builder/BotBaseBuilder.cs | 37 ++++++++++++++++--- .../Builder/Interfaces/ILanguageStage.cs | 36 ++++++++++++++++++ .../Interfaces/ISessionSerializationStage.cs | 10 ++--- TelegramBotBaseTest/Program.cs | 3 +- 4 files changed, 73 insertions(+), 13 deletions(-) create mode 100644 TelegramBotBase/Builder/Interfaces/ILanguageStage.cs diff --git a/TelegramBotBase/Builder/BotBaseBuilder.cs b/TelegramBotBase/Builder/BotBaseBuilder.cs index 0a91ea9..c8d41af 100644 --- a/TelegramBotBase/Builder/BotBaseBuilder.cs +++ b/TelegramBotBase/Builder/BotBaseBuilder.cs @@ -9,11 +9,12 @@ using TelegramBotBase.Builder.Interfaces; using TelegramBotBase.Commands; using TelegramBotBase.Form; using TelegramBotBase.Interfaces; +using TelegramBotBase.Localizations; using TelegramBotBase.States; namespace TelegramBotBase.Builder { - public class BotBaseBuilder : IAPIKeySelectionStage, IMessageLoopSelectionStage, IStartFormSelectionStage, IBuildingStage, INetworkingSelectionStage, IBotCommandsStage, ISessionSerializationStage + public class BotBaseBuilder : IAPIKeySelectionStage, IMessageLoopSelectionStage, IStartFormSelectionStage, IBuildingStage, INetworkingSelectionStage, IBotCommandsStage, ISessionSerializationStage, ILanguageSelectionStage { String _apiKey = null; @@ -156,36 +157,59 @@ namespace TelegramBotBase.Builder } - public IBuildingStage NoSerialization() + public ILanguageSelectionStage NoSerialization() { return this; } - public IBuildingStage UseSerialization(IStateMachine machine) + public ILanguageSelectionStage UseSerialization(IStateMachine machine) { this._statemachine = machine; return this; } - public IBuildingStage UseJSON(string path) + public ILanguageSelectionStage UseJSON(string path) { this._statemachine = new JSONStateMachine(path); return this; } - public IBuildingStage UseSimpleJSON(string path) + public ILanguageSelectionStage UseSimpleJSON(string path) { this._statemachine = new SimpleJSONStateMachine(path); return this; } - public IBuildingStage UseXML(string path) + public ILanguageSelectionStage UseXML(string path) { this._statemachine = new XMLStateMachine(path); return this; } + public IBuildingStage DefaultLanguage() + { + return this; + } + + public IBuildingStage UseEnglish() + { + Localizations.Default.Language = new Localizations.English(); + return this; + } + + public IBuildingStage UseGerman() + { + Localizations.Default.Language = new Localizations.German(); + return this; + } + + public IBuildingStage Custom(Localization language) + { + Localizations.Default.Language = language; + return this; + } + public BotBase Build() { var bb = new BotBase(); @@ -208,5 +232,6 @@ namespace TelegramBotBase.Builder return bb; } + } } diff --git a/TelegramBotBase/Builder/Interfaces/ILanguageStage.cs b/TelegramBotBase/Builder/Interfaces/ILanguageStage.cs new file mode 100644 index 0000000..2e5e01a --- /dev/null +++ b/TelegramBotBase/Builder/Interfaces/ILanguageStage.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using System.Text; +using TelegramBotBase.Localizations; + +namespace TelegramBotBase.Builder.Interfaces +{ + public interface ILanguageSelectionStage + { + + /// + /// Selects the default language for control usage. (English) + /// + /// + IBuildingStage DefaultLanguage(); + + /// + /// Selects english as the default language for control labels. + /// + /// + IBuildingStage UseEnglish(); + + /// + /// Selects german as the default language for control labels. + /// + /// + IBuildingStage UseGerman(); + + /// + /// Selects a custom language as the default language for control labels. + /// + /// + IBuildingStage Custom(Localization language); + + } +} diff --git a/TelegramBotBase/Builder/Interfaces/ISessionSerializationStage.cs b/TelegramBotBase/Builder/Interfaces/ISessionSerializationStage.cs index 7231c81..a729da2 100644 --- a/TelegramBotBase/Builder/Interfaces/ISessionSerializationStage.cs +++ b/TelegramBotBase/Builder/Interfaces/ISessionSerializationStage.cs @@ -11,7 +11,7 @@ namespace TelegramBotBase.Builder.Interfaces /// Do not uses serialization. /// /// - IBuildingStage NoSerialization(); + ILanguageSelectionStage NoSerialization(); /// @@ -19,7 +19,7 @@ namespace TelegramBotBase.Builder.Interfaces /// /// /// - IBuildingStage UseSerialization(IStateMachine machine); + ILanguageSelectionStage UseSerialization(IStateMachine machine); /// @@ -27,7 +27,7 @@ namespace TelegramBotBase.Builder.Interfaces /// /// /// - IBuildingStage UseJSON(String path); + ILanguageSelectionStage UseJSON(String path); /// @@ -35,7 +35,7 @@ namespace TelegramBotBase.Builder.Interfaces /// /// /// - IBuildingStage UseSimpleJSON(String path); + ILanguageSelectionStage UseSimpleJSON(String path); /// @@ -43,7 +43,7 @@ namespace TelegramBotBase.Builder.Interfaces /// /// /// - IBuildingStage UseXML(String path); + ILanguageSelectionStage UseXML(String path); } } diff --git a/TelegramBotBaseTest/Program.cs b/TelegramBotBaseTest/Program.cs index 50c9319..d457fa8 100644 --- a/TelegramBotBaseTest/Program.cs +++ b/TelegramBotBaseTest/Program.cs @@ -34,11 +34,10 @@ namespace TelegramBotBaseTest a.Add(new BotCommand() { Command = "params", Description = "Returns all send parameters as a message." }); }) .NoSerialization() + .UseEnglish() .Build(); - - bb.BotCommand += async (s, en) => { switch (en.Command)