MultiLang: Support multiple languages

This commit is contained in:
2018-03-18 21:30:04 +01:00
parent ca51d2fc08
commit 3b14f248ed
11 changed files with 200 additions and 52 deletions

View File

@@ -0,0 +1,82 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Web;
using VAR.Json;
namespace VAR.Focus.Web.Code
{
public class MultiLang
{
private static string GetLocalPath(string path)
{
string currentDir = Path.GetDirectoryName((new System.Uri(Assembly.GetExecutingAssembly().CodeBase)).AbsolutePath);
return string.Format("{0}/{1}", Directory.GetParent(currentDir), path);
}
private static Dictionary<string, Dictionary<string, object>> _literals = null;
private static void InitializeLiterals()
{
_literals = new Dictionary<string, Dictionary<string, object>>();
JsonParser jsonParser = new JsonParser();
foreach (string lang in new string[] { "en", "es" })
{
string filePath = GetLocalPath(string.Format("Resources/Literals.{0}.json", lang));
if (File.Exists(filePath) == false) { continue; }
string strJsonLiteralsLanguage = File.ReadAllText(filePath);
object result = jsonParser.Parse(strJsonLiteralsLanguage);
_literals.Add(lang, result as Dictionary<string, object>);
}
}
private const string _defaultLanguage = "en";
private static string GetUserLanguage()
{
HttpContext ctx = HttpContext.Current;
if(ctx != null)
{
if(ctx.Items["UserLang"] != null)
{
return (string)ctx.Items["UserLang"];
}
IEnumerable<string> userLanguages = ctx.Request.UserLanguages
.Select(lang =>
{
if (lang.Contains(";"))
{
lang = lang.Split(';')[0];
}
if (lang.Contains("-"))
{
lang = lang.Split('-')[0];
}
return lang.ToLower();
})
.Where(lang => _literals.ContainsKey(lang));
string userLang = userLanguages.FirstOrDefault() ?? _defaultLanguage;
ctx.Items["UserLang"] = userLang;
return userLang;
}
return _defaultLanguage;
}
public static string GetLiteral(string resource, string culture = null)
{
if (_literals == null) { InitializeLiterals(); }
if (culture == null) { culture = GetUserLanguage(); }
if (_literals == null || _literals.ContainsKey(culture) == false) { return resource; }
Dictionary<string, object> _literalCurrentCulture = _literals[culture];
if (_literalCurrentCulture == null || _literalCurrentCulture.ContainsKey(resource) == false) { return resource; }
return (_literalCurrentCulture[resource] as string) ?? resource;
}
}
}

View File

@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Text; using System.Text;
using System.Web.UI; using System.Web.UI;
using System.Web.UI.WebControls; using System.Web.UI.WebControls;
using VAR.Focus.Web.Code;
using VAR.Focus.Web.Pages; using VAR.Focus.Web.Pages;
using VAR.Json; using VAR.Json;
@@ -71,9 +72,7 @@ namespace VAR.Focus.Web.Controls
get { return _timeMoveAnimation; } get { return _timeMoveAnimation; }
set { _timeMoveAnimation = value; } set { _timeMoveAnimation = value; }
} }
public string ServiceUrl1 { get => _serviceUrl; set => _serviceUrl = value; }
#endregion Properties #endregion Properties
#region Control Life cycle #region Control Life cycle
@@ -97,7 +96,7 @@ namespace VAR.Focus.Web.Controls
Panel divBoard = new Panel { ID = "divBoard", CssClass = "divBoard" }; Panel divBoard = new Panel { ID = "divBoard", CssClass = "divBoard" };
Controls.Add(divBoard); Controls.Add(divBoard);
string strCfgName = string.Format("{0}_cfg", this.ClientID); string strCfgName = string.Format("{0}_cfg", ClientID);
Dictionary<string, object> cfg = new Dictionary<string, object> Dictionary<string, object> cfg = new Dictionary<string, object>
{ {
{"divBoard", divBoard.ClientID}, {"divBoard", divBoard.ClientID},
@@ -111,12 +110,12 @@ namespace VAR.Focus.Web.Controls
{"TimeRefreshDisconnected", _timeRefreshDisconnected}, {"TimeRefreshDisconnected", _timeRefreshDisconnected},
{"TimeMoveAnimation", _timeMoveAnimation}, {"TimeMoveAnimation", _timeMoveAnimation},
{"Texts", new Dictionary<string, object> { {"Texts", new Dictionary<string, object> {
{"Toolbox", "Toolbox"}, {"Toolbox",MultiLang.GetLiteral( "Toolbox")},
{"AddCard", "+ Add card"}, {"AddCard", MultiLang.GetLiteral("AddCard")},
{"EditBoard", "Config"}, {"EditBoard", MultiLang.GetLiteral("Config")},
{"Accept", "Accept"}, {"Accept", MultiLang.GetLiteral("Accept")},
{"Cancel", "Cancel"}, {"Cancel", MultiLang.GetLiteral("Cancel")},
{"ConfirmDelete", "Are you sure to delete?"}, {"ConfirmDelete", MultiLang.GetLiteral("ConfirmDelete")},
} }, } },
}; };
JsonWriter jsonWriter = new JsonWriter(); JsonWriter jsonWriter = new JsonWriter();

View File

@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Text; using System.Text;
using System.Web.UI; using System.Web.UI;
using System.Web.UI.WebControls; using System.Web.UI.WebControls;
using VAR.Focus.Web.Code;
using VAR.Json; using VAR.Json;
namespace VAR.Focus.Web.Controls namespace VAR.Focus.Web.Controls
@@ -128,7 +129,7 @@ namespace VAR.Focus.Web.Controls
txtText.Attributes.Add("onkeydown", string.Format("if(event.keyCode==13){{SendChat({0}); return false;}}", strCfgName)); txtText.Attributes.Add("onkeydown", string.Format("if(event.keyCode==13){{SendChat({0}); return false;}}", strCfgName));
divChatControls.Controls.Add(txtText); divChatControls.Controls.Add(txtText);
var btnSend = new Button { ID = "btnSend", Text = "Send", CssClass = "chatButton" }; var btnSend = new Button { ID = "btnSend", Text = MultiLang.GetLiteral("ChatSend"), CssClass = "chatButton" };
divChatControls.Controls.Add(btnSend); divChatControls.Controls.Add(btnSend);
btnSend.Attributes.Add("onclick", string.Format("SendChat({0}); return false;", strCfgName)); btnSend.Attributes.Add("onclick", string.Format("SendChat({0}); return false;", strCfgName));
@@ -149,10 +150,10 @@ namespace VAR.Focus.Web.Controls
{"ServiceUrl", _serviceUrl}, {"ServiceUrl", _serviceUrl},
{"TimePoolData", _timePoolData}, {"TimePoolData", _timePoolData},
{"Texts", new Dictionary<string, object> { {"Texts", new Dictionary<string, object> {
{"Chat", "Chat"}, {"Chat", MultiLang.GetLiteral("Chat")},
{"Close", "Close X"}, {"Close", MultiLang.GetLiteral("ChatClose")},
{"NewMessages", "New messages"}, {"NewMessages", MultiLang.GetLiteral("ChatNewMessages")},
{"Disconnected", "Disconnected"}, {"Disconnected", MultiLang.GetLiteral("Disconnected")},
} }, } },
}; };
JsonWriter jsonWriter = new JsonWriter(); JsonWriter jsonWriter = new JsonWriter();

View File

@@ -115,7 +115,7 @@ namespace VAR.Focus.Web.Pages
var btnView = new CButton var btnView = new CButton
{ {
ID = string.Format("btnView{0}", board.IDBoard), ID = string.Format("btnView{0}", board.IDBoard),
Text = "View", Text = MultiLang.GetLiteral("View"),
}; };
btnView.CommandArgument = Convert.ToString(board.IDBoard); btnView.CommandArgument = Convert.ToString(board.IDBoard);
btnView.Click += BtnView_Click; btnView.Click += BtnView_Click;
@@ -123,7 +123,7 @@ namespace VAR.Focus.Web.Pages
var btnEdit = new CButton var btnEdit = new CButton
{ {
ID = string.Format("btnEdit{0}", board.IDBoard), ID = string.Format("btnEdit{0}", board.IDBoard),
Text = "Edit", Text = MultiLang.GetLiteral("Edit"),
}; };
btnEdit.CommandArgument = Convert.ToString(board.IDBoard); btnEdit.CommandArgument = Convert.ToString(board.IDBoard);
btnEdit.Click += BtnEdit_Click; btnEdit.Click += BtnEdit_Click;
@@ -131,11 +131,11 @@ namespace VAR.Focus.Web.Pages
var btnDelete = new CButton var btnDelete = new CButton
{ {
ID = string.Format("btnDelete{0}", board.IDBoard), ID = string.Format("btnDelete{0}", board.IDBoard),
Text = "Delete", Text = MultiLang.GetLiteral("Delete"),
}; };
btnDelete.CommandArgument = Convert.ToString(board.IDBoard); btnDelete.CommandArgument = Convert.ToString(board.IDBoard);
btnDelete.Click += BtnDelete_Click; btnDelete.Click += BtnDelete_Click;
btnDelete.Attributes.Add("onclick", string.Format("return confirm('{0}');", "¿Are you sure to delete?")); btnDelete.Attributes.Add("onclick", string.Format("return confirm('{0}');", MultiLang.GetLiteral("ConfirmDelete")));
pnlButtons.Controls.Add(btnDelete); pnlButtons.Controls.Add(btnDelete);
pnlBoardSelector.Controls.Add(pnlButtons); pnlBoardSelector.Controls.Add(pnlButtons);
@@ -155,12 +155,12 @@ namespace VAR.Focus.Web.Pages
// Board creator // Board creator
var pnlBoardAdd = new Panel { CssClass = "boardBanner" }; var pnlBoardAdd = new Panel { CssClass = "boardBanner" };
var btnAddBoard = new CButton { ID = "btnAddBoard", Text = "AddBoard" }; var btnAddBoard = new CButton { ID = "btnAddBoard", Text = MultiLang.GetLiteral("AddBoard") };
btnAddBoard.Click += btnAddBoard_Click; btnAddBoard.Click += btnAddBoard_Click;
pnlBoardAdd.Controls.Add(FormUtils.CreatePanel("formRow", _txtTitle)); pnlBoardAdd.Controls.Add(FormUtils.CreatePanel("formRow", _txtTitle));
_txtTitle.PlaceHolder = "Title"; _txtTitle.PlaceHolder = MultiLang.GetLiteral("Title");
pnlBoardAdd.Controls.Add(FormUtils.CreatePanel("formRow", _txtDescription)); pnlBoardAdd.Controls.Add(FormUtils.CreatePanel("formRow", _txtDescription));
_txtDescription.PlaceHolder = "Description"; _txtDescription.PlaceHolder = MultiLang.GetLiteral("Description");
pnlBoardAdd.Controls.Add(FormUtils.CreatePanel("formRow", btnAddBoard)); pnlBoardAdd.Controls.Add(FormUtils.CreatePanel("formRow", btnAddBoard));
Controls.Add(pnlBoardAdd); Controls.Add(pnlBoardAdd);
} }

View File

@@ -88,21 +88,21 @@ namespace VAR.Focus.Web.Pages
private void InitializeComponents() private void InitializeComponents()
{ {
Title = "Register"; Title = MultiLang.GetLiteral("EditBoard");
var lblTitle = new CLabel { Text = "Register", Tag = "h2" }; var lblTitle = new CLabel { Text = Title, Tag = "h2" };
Controls.Add(lblTitle); Controls.Add(lblTitle);
Controls.Add(FormUtils.CreateField("Title", _txtTitle)); Controls.Add(FormUtils.CreateField(MultiLang.GetLiteral("Title"), _txtTitle));
_txtTitle.NextFocusOnEnter = _txtTitle; _txtTitle.NextFocusOnEnter = _txtTitle;
_txtTitle.PlaceHolder = "Title"; _txtTitle.PlaceHolder = MultiLang.GetLiteral("Title");
Controls.Add(FormUtils.CreateField("Description", _txtDescription)); Controls.Add(FormUtils.CreateField(MultiLang.GetLiteral("Description"), _txtDescription));
_txtDescription.PlaceHolder = "Description"; _txtDescription.PlaceHolder = MultiLang.GetLiteral("Description");
_btnSave.Text = "Save"; _btnSave.Text = MultiLang.GetLiteral("Save");
_btnSave.Click += btnSave_Click; _btnSave.Click += btnSave_Click;
_btnExit.Text = "Exit"; _btnExit.Text = MultiLang.GetLiteral("Exit");
_btnExit.Click += btnExit_Click; _btnExit.Click += btnExit_Click;
Panel pnlButtons = new Panel(); Panel pnlButtons = new Panel();

View File

@@ -53,23 +53,23 @@ namespace VAR.Focus.Web.Pages
private void InitializeControls() private void InitializeControls()
{ {
Title = "Login"; Title = MultiLang.GetLiteral("Login");
var lblTitle = new CLabel { Text = "Login", Tag = "h2" }; var lblTitle = new CLabel { Text = Title, Tag = "h2" };
Controls.Add(lblTitle); Controls.Add(lblTitle);
Controls.Add(FormUtils.CreateField("Name/Mail", _txtNameEmail)); Controls.Add(FormUtils.CreateField(MultiLang.GetLiteral("NameOrMail"), _txtNameEmail));
_txtNameEmail.NextFocusOnEnter = _txtPassword; _txtNameEmail.NextFocusOnEnter = _txtPassword;
_txtNameEmail.PlaceHolder = "Name/Mail"; _txtNameEmail.PlaceHolder = MultiLang.GetLiteral("NameOrMail");
Controls.Add(FormUtils.CreateField("Password", _txtPassword)); Controls.Add(FormUtils.CreateField(MultiLang.GetLiteral("Password"), _txtPassword));
_txtPassword.NextFocusOnEnter = _btnLogin; _txtPassword.NextFocusOnEnter = _btnLogin;
_txtPassword.PlaceHolder = "Password"; _txtPassword.PlaceHolder = MultiLang.GetLiteral("Password");
Controls.Add(FormUtils.CreateField(string.Empty, _btnLogin)); Controls.Add(FormUtils.CreateField(string.Empty, _btnLogin));
_btnLogin.Text = "Login"; _btnLogin.Text = MultiLang.GetLiteral("Login");
_btnLogin.Click += btnLogin_Click; _btnLogin.Click += btnLogin_Click;
Controls.Add(FormUtils.CreateField(string.Empty, new HyperLink { Text = "Register user", NavigateUrl = "FrmRegister" })); Controls.Add(FormUtils.CreateField(string.Empty, new HyperLink { Text = MultiLang.GetLiteral("RegisterUser"), NavigateUrl = "FrmRegister" }));
} }
#endregion Private methods #endregion Private methods

View File

@@ -2,6 +2,7 @@
using System.Web.UI.WebControls; using System.Web.UI.WebControls;
using VAR.Focus.BusinessLogic; using VAR.Focus.BusinessLogic;
using VAR.Focus.BusinessLogic.Entities; using VAR.Focus.BusinessLogic.Entities;
using VAR.Focus.Web.Code;
using VAR.Focus.Web.Controls; using VAR.Focus.Web.Controls;
namespace VAR.Focus.Web.Pages namespace VAR.Focus.Web.Pages
@@ -74,32 +75,32 @@ namespace VAR.Focus.Web.Pages
private void InitializeComponents() private void InitializeComponents()
{ {
Title = "Register"; Title = MultiLang.GetLiteral("RegisterUser");
var lblTitle = new CLabel { Text = "Register", Tag = "h2" }; var lblTitle = new CLabel { Text = Title, Tag = "h2" };
Controls.Add(lblTitle); Controls.Add(lblTitle);
Controls.Add(_pnlRegister); Controls.Add(_pnlRegister);
_pnlRegister.Controls.Add(FormUtils.CreateField("Name", _txtName)); _pnlRegister.Controls.Add(FormUtils.CreateField(MultiLang.GetLiteral("Name"), _txtName));
_txtName.NextFocusOnEnter = _txtEmail; _txtName.NextFocusOnEnter = _txtEmail;
_txtName.PlaceHolder = "Name"; _txtName.PlaceHolder = MultiLang.GetLiteral("Name");
_pnlRegister.Controls.Add(FormUtils.CreateField("Email", _txtEmail)); _pnlRegister.Controls.Add(FormUtils.CreateField(MultiLang.GetLiteral("Email"), _txtEmail));
_txtEmail.NextFocusOnEnter = _txtPassword1; _txtEmail.NextFocusOnEnter = _txtPassword1;
_txtEmail.PlaceHolder = "Email"; _txtEmail.PlaceHolder = MultiLang.GetLiteral("Email");
_pnlRegister.Controls.Add(FormUtils.CreateField("Password", _txtPassword1)); _pnlRegister.Controls.Add(FormUtils.CreateField(MultiLang.GetLiteral("Password"), _txtPassword1));
_txtPassword1.NextFocusOnEnter = _txtPassword2; _txtPassword1.NextFocusOnEnter = _txtPassword2;
_txtPassword1.PlaceHolder = "Password"; _txtPassword1.PlaceHolder = MultiLang.GetLiteral("Password");
_pnlRegister.Controls.Add(FormUtils.CreateField(string.Empty, _txtPassword2)); _pnlRegister.Controls.Add(FormUtils.CreateField(string.Empty, _txtPassword2));
_txtPassword2.NextFocusOnEnter = _btnRegister; _txtPassword2.NextFocusOnEnter = _btnRegister;
_txtPassword2.PlaceHolder = "Password"; _txtPassword2.PlaceHolder = MultiLang.GetLiteral("Password");
_btnRegister.Text = "Register"; _btnRegister.Text = MultiLang.GetLiteral("Register");
_btnRegister.Click += btnRegister_Click; _btnRegister.Click += btnRegister_Click;
_btnExit.Text = "Exit"; _btnExit.Text = MultiLang.GetLiteral("Exit");
_btnExit.Click += btnExit_Click; _btnExit.Click += btnExit_Click;
Panel pnlButtons = new Panel(); Panel pnlButtons = new Panel();
@@ -112,7 +113,7 @@ namespace VAR.Focus.Web.Pages
_pnlSuccess.Controls.Add(_lblSuccess); _pnlSuccess.Controls.Add(_lblSuccess);
_btnExitSuccess.Text = "Exit"; _btnExitSuccess.Text = MultiLang.GetLiteral("Exit");
_btnExitSuccess.Click += btnExit_Click; _btnExitSuccess.Click += btnExit_Click;
_pnlSuccess.Controls.Add(FormUtils.CreateField(string.Empty, _btnExitSuccess)); _pnlSuccess.Controls.Add(FormUtils.CreateField(string.Empty, _btnExitSuccess));
} }

View File

@@ -149,9 +149,9 @@ namespace VAR.Focus.Web.Pages
pnlHeader.Controls.Add(pnlUserInfo); pnlHeader.Controls.Add(pnlUserInfo);
_btnLogout.ID = "btnLogout"; _btnLogout.ID = "btnLogout";
_btnLogout.Text = "Logout"; _btnLogout.Text = MultiLang.GetLiteral("Logout");
_btnLogout.Click += btnLogout_Click; _btnLogout.Click += btnLogout_Click;
_btnLogout.Attributes.Add("onclick", string.Format("return confirm('{0}');", "¿Are you sure to exit?")); _btnLogout.Attributes.Add("onclick", string.Format("return confirm('{0}');", MultiLang.GetLiteral("ConfirmExit")));
pnlUserInfo.Controls.Add(_btnLogout); pnlUserInfo.Controls.Add(_btnLogout);
_pnlContainer.CssClass = "divContent"; _pnlContainer.CssClass = "divContent";

View File

@@ -0,0 +1,31 @@
{
"AddBoard": "Add board",
"EditBoard": "Edit board",
"Title": "Title",
"Description": "Description",
"Save": "Save",
"Exit": "Exit",
"Toolbox": "Toolbox",
"AddCard": "+ Card",
"Accept": "Accept",
"Cancel": "Cancel",
"ConfirmDelete": "Are you sure to delete?",
"ConfirmExit": "Are you sure to exit?",
"Config": "Config",
"Login": "Login",
"Logout": "Logout",
"NameOrMail": "Name/Mail",
"Password": "Password",
"RegisterUser": "RegisterUser",
"Name": "Name",
"Email": "Email",
"Register": "Register",
"View": "View",
"Edit": "Edit",
"Delete": "Delete",
"Chat": "Chat",
"ChatClose": "Close X",
"ChatNewMessages": "New messages",
"ChatDisconnected": "Disconnected",
"ChatSend": "Send"
}

View File

@@ -0,0 +1,31 @@
{
"AddBoard": "Añadir panel",
"EditBoard": "Editar panel",
"Title": "Titulo",
"Description": "Descripción",
"Save": "Guardar",
"Exit": "Salir",
"Toolbox": "Herramientas",
"AddCard": "+ Tarejeta",
"Accept": "Aceptar",
"Cancel": "Cancelar",
"ConfirmDelete": "¿Estas seguro de eliminar?",
"ConfirmExit": "¿Estas seguro de salir?",
"Config": "Config.",
"Login": "Login",
"Logout": "Cerrar sesion",
"NameOrMail": "Nombre/Mail",
"Password": "Password",
"RegisterUser": "Registrar usuario",
"Name": "Nombre",
"Email": "Email",
"Register": "Registrar",
"View": "Ver",
"Edit": "Editar",
"Delete": "Eliminar",
"Chat": "Chat",
"ChatClose": "Cerrar X",
"ChatNewMessages": "Nuevos mensages",
"ChatDisconnected": "Desconectado",
"ChatSend": "Envia"
}

View File

@@ -56,6 +56,8 @@
<Content Include="Scripts\01. Base.js" /> <Content Include="Scripts\01. Base.js" />
<None Include="packages.config" /> <None Include="packages.config" />
<None Include="Properties\PublishProfiles\VARFocus - Web Deploy.pubxml" /> <None Include="Properties\PublishProfiles\VARFocus - Web Deploy.pubxml" />
<Content Include="Resources\Literals.en.json" />
<Content Include="Resources\Literals.es.json" />
<None Include="web.Debug.config"> <None Include="web.Debug.config">
<DependentUpon>web.config</DependentUpon> <DependentUpon>web.config</DependentUpon>
<SubType>Designer</SubType> <SubType>Designer</SubType>
@@ -82,6 +84,7 @@
<ItemGroup> <ItemGroup>
<Compile Include="Code\Bundler.cs" /> <Compile Include="Code\Bundler.cs" />
<Compile Include="Code\ExtensionMethods.cs" /> <Compile Include="Code\ExtensionMethods.cs" />
<Compile Include="Code\MultiLang.cs" />
<Compile Include="Code\StaticFileHelper.cs" /> <Compile Include="Code\StaticFileHelper.cs" />
<Compile Include="Code\WebSessions.cs" /> <Compile Include="Code\WebSessions.cs" />
<Compile Include="Controls\CtrCardBoard.cs" /> <Compile Include="Controls\CtrCardBoard.cs" />