Skip to content

Commit

Permalink
Merge pull request #4713 from abpframework/maliming/patch-1
Browse files Browse the repository at this point in the history
Refatoring BundleContributor Make it support LocalizationMapOptions
  • Loading branch information
hikalkan authored Jul 17, 2020
2 parents 1e86edd + 0077d15 commit 1d07c69
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 65 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Volo.Abp.AspNetCore.Mvc.UI.Bundling;
using Volo.Abp.AspNetCore.Mvc.UI.Packages.BootstrapDatepicker;
using Volo.Abp.AspNetCore.Mvc.UI.Packages.JQueryValidation;
using Volo.Abp.AspNetCore.Mvc.UI.Packages.Timeago;
using Volo.Abp.Localization;
using Volo.Abp.Modularity;

Expand All @@ -12,13 +14,24 @@ public override void ConfigureServices(ServiceConfigurationContext context)
{
Configure<AbpLocalizationOptions>(options =>
{
//BootstrapDatepicker
options.AddLanguagesMapOrUpdate(BootstrapDatepickerScriptContributor.PackageName,
new NameValue("zh-Hans", "zh-CN"),
new NameValue("zh-Hant", "zh-TW"));

options.AddLanguageFilesMapOrUpdate(BootstrapDatepickerScriptContributor.PackageName,
new NameValue("zh-Hans", "zh-CN"),
new NameValue("zh-Hant", "zh-TW"));

//Timeago
options.AddLanguageFilesMapOrUpdate(TimeagoScriptContributor.PackageName,
new NameValue("zh-Hans", "zh-CN"),
new NameValue("zh-Hant", "zh-TW"));

//JQueryValidation
options.AddLanguageFilesMapOrUpdate(JQueryValidationScriptContributor.PackageName,
new NameValue("zh-Hans", "zh"),
new NameValue("zh-Hant", "zh_TW"));
});
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections.Generic;
using System.Globalization;
using Volo.Abp.AspNetCore.Mvc.UI.Bundling;
using Volo.Abp.AspNetCore.Mvc.UI.Packages.JQuery;
using Volo.Abp.Localization;
Expand All @@ -19,26 +18,12 @@ public override void ConfigureBundle(BundleConfigurationContext context)

public override void ConfigureDynamicResources(BundleConfigurationContext context)
{
var cultureName = CultureInfo.CurrentUICulture.DateTimeFormat.Calendar.AlgorithmType ==
CalendarAlgorithmType.LunarCalendar
? "en"
: CultureInfo.CurrentUICulture.Name;

TryAddCultureFile(context, cultureName);
}

protected virtual bool TryAddCultureFile(BundleConfigurationContext context, string cultureName)
{
var fileName = context.LocalizationOptions.GetLanguageFilesMap(PackageName, cultureName);
var fileName = context.LocalizationOptions.GetCurrentUICultureLanguageFilesMap(PackageName);
var filePath = $"/libs/bootstrap-datepicker/locales/bootstrap-datepicker.{fileName}.min.js";

if (!context.FileProvider.GetFileInfo(filePath).Exists)
if (context.FileProvider.GetFileInfo(filePath).Exists)
{
return false;
context.Files.AddIfNotContains(filePath);
}

context.Files.AddIfNotContains(filePath);
return true;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections.Generic;
using System.Globalization;
using Volo.Abp.AspNetCore.Mvc.UI.Bundling;
using Volo.Abp.AspNetCore.Mvc.UI.Packages.JQuery;
using Volo.Abp.Localization;
Expand All @@ -10,8 +9,6 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Packages.JQueryValidation
[DependsOn(typeof(JQueryScriptContributor))]
public class JQueryValidationScriptContributor : BundleContributor
{
public const string DefaultLocalizationFolder = "/libs/jquery-validation/localization/";

public const string PackageName = "jquery-validation";

public override void ConfigureBundle(BundleConfigurationContext context)
Expand All @@ -21,41 +18,12 @@ public override void ConfigureBundle(BundleConfigurationContext context)

public override void ConfigureDynamicResources(BundleConfigurationContext context)
{
//TODO: Can we optimize these points:
// - Can we get rid of context.FileProvider.GetFileInfo call?
// - What if the same Contributor is used twice for a page.
// Duplication is prevented by the bundle manager, however the logic below will execute twice

var cultureName = CultureInfo.CurrentUICulture.TwoLetterISOLanguageName.Replace('-', '_');

var fileName = context.LocalizationOptions.GetLanguageFilesMap(PackageName, cultureName);
if (TryAddCultureFile(context, fileName))
{
return;
}

if (!cultureName.Contains("_"))
{
return;
}

fileName = context.LocalizationOptions.GetLanguageFilesMap(PackageName,
cultureName.Substring(0, cultureName.IndexOf('_')));
TryAddCultureFile(context, fileName);
}

protected virtual bool TryAddCultureFile(BundleConfigurationContext context, string cultureName)
{
var filePath = DefaultLocalizationFolder + "messages_" + cultureName + ".js";
var fileInfo = context.FileProvider.GetFileInfo(filePath);

if (!fileInfo.Exists)
var fileName = context.LocalizationOptions.GetCurrentUICultureLanguageFilesMap(PackageName);
var filePath = $"/libs/jquery-validation/localization/messages_{fileName}.js";
if (context.FileProvider.GetFileInfo(filePath).Exists)
{
return false;
context.Files.AddIfNotContains(filePath);
}

context.Files.AddIfNotContains(filePath);
return true;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,32 +1,28 @@
using System.Collections.Generic;
using System.Globalization;
using Volo.Abp.AspNetCore.Mvc.UI.Bundling;
using Volo.Abp.AspNetCore.Mvc.UI.Packages.JQuery;
using Volo.Abp.Localization;
using Volo.Abp.Modularity;

namespace Volo.Abp.AspNetCore.Mvc.UI.Packages.Timeago
{
[DependsOn(typeof(JQueryScriptContributor))]
public class TimeagoScriptContributor : BundleContributor
{
public const string PackageName = "jquery.timeago";

public override void ConfigureBundle(BundleConfigurationContext context)
{
context.Files.AddIfNotContains("/libs/timeago/jquery.timeago.js");
}

public override void ConfigureDynamicResources(BundleConfigurationContext context)
{
var cultureName = CultureInfo.CurrentUICulture.TwoLetterISOLanguageName;
if (cultureName.StartsWith("en"))
{
return;
}

var cultureFileName = $"/libs/timeago/locales/jquery.timeago.{cultureName}.js";

if (context.FileProvider.GetFileInfo(cultureFileName).Exists)
var fileName = context.LocalizationOptions.GetCurrentUICultureLanguageFilesMap(PackageName);
var filePath = $"/libs/timeago/locales/jquery.timeago.{fileName}.js";
if (context.FileProvider.GetFileInfo(filePath).Exists)
{
context.Files.Add(cultureFileName);
context.Files.Add(filePath);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Globalization;
using System.Linq;

namespace Volo.Abp.Localization
Expand All @@ -24,6 +25,11 @@ public static string GetLanguagesMap(this AbpLocalizationOptions localizationOpt
: language;
}

public static string GetCurrentUICultureLanguagesMap(this AbpLocalizationOptions localizationOptions, string packageName)
{
return GetLanguagesMap(localizationOptions, packageName, CultureInfo.CurrentUICulture.Name);
}

public static AbpLocalizationOptions AddLanguageFilesMapOrUpdate(this AbpLocalizationOptions localizationOptions,
string packageName, params NameValue[] maps)
{
Expand All @@ -43,6 +49,11 @@ public static string GetLanguageFilesMap(this AbpLocalizationOptions localizatio
: language;
}

public static string GetCurrentUICultureLanguageFilesMap(this AbpLocalizationOptions localizationOptions, string packageName)
{
return GetLanguageFilesMap(localizationOptions, packageName, CultureInfo.CurrentUICulture.Name);
}

private static void AddOrUpdate(IDictionary<string, List<NameValue>> maps, string packageName, NameValue value)
{
if (maps.TryGetValue(packageName, out var existMaps))
Expand Down

0 comments on commit 1d07c69

Please sign in to comment.