From 21914f50cc2ab875c9f11e3daa42029a6b36fa68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Fri, 27 Oct 2017 10:53:51 +0300 Subject: [PATCH] Implemented javascript side localization. --- .../AbpDesk.Web.Mvc/AbpDeskWebMvcModule.cs | 3 +- .../AbpApplicationConfigurationController.cs | 22 +++++++++ ...pplicationConfigurationScriptController.cs | 45 +++++++++++++++++++ .../ApplicationConfigurationsController.cs | 24 ---------- ....cs => AbpServiceProxyScriptController.cs} | 6 ++- .../Identity/Localization/Resource/en.json | 3 +- .../Identity/Localization/Resource/tr.json | 3 +- .../modules/identity/views/users/index.js | 8 +++- .../Newtonsoft/NewtonsoftJsonSerializer.cs | 14 +++++- 9 files changed, 96 insertions(+), 32 deletions(-) create mode 100644 src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ApplicationConfigurations/AbpApplicationConfigurationController.cs create mode 100644 src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ApplicationConfigurations/AbpApplicationConfigurationScriptController.cs delete mode 100644 src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ApplicationConfigurations/ApplicationConfigurationsController.cs rename src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ProxyScripting/{AbpServiceProxiesController.cs => AbpServiceProxyScriptController.cs} (73%) diff --git a/src/AbpDesk/AbpDesk.Web.Mvc/AbpDeskWebMvcModule.cs b/src/AbpDesk/AbpDesk.Web.Mvc/AbpDeskWebMvcModule.cs index 13adf1437f..f1424a416a 100644 --- a/src/AbpDesk/AbpDesk.Web.Mvc/AbpDeskWebMvcModule.cs +++ b/src/AbpDesk/AbpDesk.Web.Mvc/AbpDeskWebMvcModule.cs @@ -66,7 +66,8 @@ namespace AbpDesk.Web.Mvc { options.ScriptBundles.Add("GlobalScripts", new[] { - "/AbpServiceProxies/GetAll?_v=" + DateTime.Now.Ticks + "/Abp/ApplicationConfigurationScript?_v=" + DateTime.Now.Ticks, + "/Abp/ServiceProxyScript?_v=" + DateTime.Now.Ticks }); }); diff --git a/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ApplicationConfigurations/AbpApplicationConfigurationController.cs b/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ApplicationConfigurations/AbpApplicationConfigurationController.cs new file mode 100644 index 0000000000..adcb07242a --- /dev/null +++ b/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ApplicationConfigurations/AbpApplicationConfigurationController.cs @@ -0,0 +1,22 @@ +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; + +namespace Volo.Abp.AspNetCore.Mvc.ApplicationConfigurations +{ + [Route("api/abp/application-configuration")] + public class AbpApplicationConfigurationController : AbpController + { + private readonly IApplicationConfigurationBuilder _configurationBuilder; + + public AbpApplicationConfigurationController(IApplicationConfigurationBuilder configurationBuilder) + { + _configurationBuilder = configurationBuilder; + } + + [HttpGet] + public Task Get() + { + return _configurationBuilder.Get(); + } + } +} \ No newline at end of file diff --git a/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ApplicationConfigurations/AbpApplicationConfigurationScriptController.cs b/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ApplicationConfigurations/AbpApplicationConfigurationScriptController.cs new file mode 100644 index 0000000000..51906199d4 --- /dev/null +++ b/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ApplicationConfigurations/AbpApplicationConfigurationScriptController.cs @@ -0,0 +1,45 @@ +using System.Diagnostics; +using System.Text; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; +using Volo.Abp.Json; + +namespace Volo.Abp.AspNetCore.Mvc.ApplicationConfigurations +{ + [Area("Abp")] + [Route("Abp/ApplicationConfigurationScript")] + public class AbpApplicationConfigurationScriptController : AbpController + { + private readonly IApplicationConfigurationBuilder _configurationBuilder; + private readonly IJsonSerializer _jsonSerializer; + + public AbpApplicationConfigurationScriptController( + IApplicationConfigurationBuilder configurationBuilder, + IJsonSerializer jsonSerializer) + { + _configurationBuilder = configurationBuilder; + _jsonSerializer = jsonSerializer; + } + + [HttpGet] + [Produces("text/javascript", "text/plain")] + public async Task Get() + { + var config = await _configurationBuilder.Get(); + return CreateAbpExtendScript(config); + } + + private string CreateAbpExtendScript(ApplicationConfigurationDto config) + { + var script = new StringBuilder(); + + script.AppendLine("(function(){"); + + script.AppendLine($"$.extend(true, abp, {_jsonSerializer.Serialize(config, indented: Debugger.IsAttached)})"); + + script.Append("})();"); + + return script.ToString(); + } + } +} diff --git a/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ApplicationConfigurations/ApplicationConfigurationsController.cs b/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ApplicationConfigurations/ApplicationConfigurationsController.cs deleted file mode 100644 index 29606f6dcc..0000000000 --- a/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ApplicationConfigurations/ApplicationConfigurationsController.cs +++ /dev/null @@ -1,24 +0,0 @@ -using System.Threading.Tasks; -using Microsoft.AspNetCore.Mvc; - -namespace Volo.Abp.AspNetCore.Mvc.ApplicationConfigurations -{ - public class ApplicationConfigurationController : AbpController - { - private readonly IApplicationConfigurationBuilder _configurationBuilder; - - public ApplicationConfigurationController(IApplicationConfigurationBuilder configurationBuilder) - { - _configurationBuilder = configurationBuilder; - } - - public async Task GetAll() - { - //TODO: 1. Make an API - //TODO: 2. Make a script generation controller - - var userConfig = await _configurationBuilder.Get(); - return Json(userConfig); - } - } -} diff --git a/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ProxyScripting/AbpServiceProxiesController.cs b/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ProxyScripting/AbpServiceProxyScriptController.cs similarity index 73% rename from src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ProxyScripting/AbpServiceProxiesController.cs rename to src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ProxyScripting/AbpServiceProxyScriptController.cs index 4edfeaa9fa..0ebee057bf 100644 --- a/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ProxyScripting/AbpServiceProxiesController.cs +++ b/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ProxyScripting/AbpServiceProxyScriptController.cs @@ -5,11 +5,13 @@ namespace Volo.Abp.AspNetCore.Mvc.ProxyScripting { //TODO: abp area? //TODO: [DisableAuditing] - public class AbpServiceProxiesController : AbpController + [Area("Abp")] + [Route("Abp/ServiceProxyScript")] + public class AbpServiceProxyScriptController : AbpController { private readonly IProxyScriptManager _proxyScriptManager; - public AbpServiceProxiesController(IProxyScriptManager proxyScriptManager) + public AbpServiceProxyScriptController(IProxyScriptManager proxyScriptManager) { _proxyScriptManager = proxyScriptManager; } diff --git a/src/Volo.Abp.Identity.Web/Areas/Identity/Localization/Resource/en.json b/src/Volo.Abp.Identity.Web/Areas/Identity/Localization/Resource/en.json index d9583007e9..d8f532143a 100644 --- a/src/Volo.Abp.Identity.Web/Areas/Identity/Localization/Resource/en.json +++ b/src/Volo.Abp.Identity.Web/Areas/Identity/Localization/Resource/en.json @@ -11,6 +11,7 @@ "Roles": "Roles", "Close": "Close", "Save": "Save", - "Password": "Password" + "Password": "Password", + "UserDeletionConfirmationMessage": "User '{0}' will be deleted. Do you confirm that?" } } \ No newline at end of file diff --git a/src/Volo.Abp.Identity.Web/Areas/Identity/Localization/Resource/tr.json b/src/Volo.Abp.Identity.Web/Areas/Identity/Localization/Resource/tr.json index e6d7964ca3..09be81e3d6 100644 --- a/src/Volo.Abp.Identity.Web/Areas/Identity/Localization/Resource/tr.json +++ b/src/Volo.Abp.Identity.Web/Areas/Identity/Localization/Resource/tr.json @@ -11,6 +11,7 @@ "Roles": "Roller", "Close": "Kapat", "Save": "Kaydet", - "Password": "Şifre" + "Password": "Şifre", + "UserDeletionConfirmationMessage": "{0} kullanıcısı silinecektir. Onaylıyor musunuz?" } } \ No newline at end of file diff --git a/src/Volo.Abp.Identity.Web/wwwroot/modules/identity/views/users/index.js b/src/Volo.Abp.Identity.Web/wwwroot/modules/identity/views/users/index.js index 139679c609..bf314ed1c9 100644 --- a/src/Volo.Abp.Identity.Web/wwwroot/modules/identity/views/users/index.js +++ b/src/Volo.Abp.Identity.Web/wwwroot/modules/identity/views/users/index.js @@ -1,5 +1,6 @@ $(function () { var _identityUserAppService = volo.abp.identity.identityUser; + var _localize = abp.localization.getResource('AbpIdentityWeb'); var dataTable = $('#IdentityUsersTable').DataTable({ order: [[1, "asc"]], @@ -82,8 +83,9 @@ $('#IdentityUsersTable').on('click', '.delete-user', function () { var id = $(this).data('id'); + var userName = $(this).data('userName'); - if (confirm('Are you sure you want to delete?')) { + if (confirm(_localize('UserDeletionConfirmationMessage', userName))) { _identityUserAppService.delete(id).done(function () { dataTable.ajax.reload(); }); @@ -160,9 +162,11 @@ $.fn.serializeFormToObject = function () { return; } + var currentLanguage = 'English'; //TODO: Get from current culture! + $.extend(true, $.fn.dataTable.defaults, { language: { - url: '/modules/identity/libs/datatables/localizations/' + abp.localization.currentCulture.displayNameEnglish + '.json' + url: '/modules/identity/libs/datatables/localizations/' + currentLanguage + '.json' }, lengthMenu: [5, 10, 25, 50, 100, 250, 500], pageLength: 10, diff --git a/src/Volo.Abp/Volo/Abp/Json/Newtonsoft/NewtonsoftJsonSerializer.cs b/src/Volo.Abp/Volo/Abp/Json/Newtonsoft/NewtonsoftJsonSerializer.cs index 2d4b403a0e..262c2b04d7 100644 --- a/src/Volo.Abp/Volo/Abp/Json/Newtonsoft/NewtonsoftJsonSerializer.cs +++ b/src/Volo.Abp/Volo/Abp/Json/Newtonsoft/NewtonsoftJsonSerializer.cs @@ -37,7 +37,7 @@ namespace Volo.Abp.Json.Newtonsoft if (camelCase) { - settings.ContractResolver = new CamelCasePropertyNamesContractResolver(); + settings.ContractResolver = new CamelCaseExceptDictionaryKeysResolver(); } if (indented) @@ -47,5 +47,17 @@ namespace Volo.Abp.Json.Newtonsoft return settings; } + + private class CamelCaseExceptDictionaryKeysResolver : CamelCasePropertyNamesContractResolver + { + protected override JsonDictionaryContract CreateDictionaryContract(Type objectType) + { + var contract = base.CreateDictionaryContract(objectType); + + contract.DictionaryKeyResolver = propertyName => propertyName; + + return contract; + } + } } } \ No newline at end of file