mirror of https://github.com/abpframework/abp.git
16 changed files with 232 additions and 60 deletions
@ -1,11 +0,0 @@ |
|||
namespace Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic.Themes.Basic.Components.Toolbar.LanguageSwitch |
|||
{ |
|||
public class LanguageInfo |
|||
{ |
|||
public string Name { get; set; } |
|||
|
|||
public string DisplayName { get; set; } |
|||
|
|||
public string Icon { get; set; } |
|||
} |
|||
} |
|||
@ -1,49 +1,44 @@ |
|||
using System.Globalization; |
|||
using System.Collections.Generic; |
|||
using System.Globalization; |
|||
using System.Linq; |
|||
using System.Threading.Tasks; |
|||
using Microsoft.AspNetCore.Builder; |
|||
using Microsoft.AspNetCore.Mvc; |
|||
using Microsoft.Extensions.Options; |
|||
using Volo.Abp.Localization; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic.Themes.Basic.Components.Toolbar.LanguageSwitch |
|||
{ |
|||
public class LanguageSwitchViewComponent : AbpViewComponent |
|||
{ |
|||
private readonly RequestLocalizationOptions _options; |
|||
private readonly ILanguageProvider _languageProvider; |
|||
|
|||
public LanguageSwitchViewComponent(IOptions<RequestLocalizationOptions> options) |
|||
public LanguageSwitchViewComponent(ILanguageProvider languageProvider) |
|||
{ |
|||
_options = options.Value; |
|||
_languageProvider = languageProvider; |
|||
} |
|||
|
|||
public IViewComponentResult Invoke() |
|||
public async Task<IViewComponentResult> InvokeAsync() |
|||
{ |
|||
//TODO: Better handle culture & uiculture separation!
|
|||
var languages = await _languageProvider.GetLanguagesAsync(); |
|||
var currentLanguage = FindCurrentLanguage(languages); |
|||
|
|||
var model = new LanguageSwitchViewComponentModel |
|||
{ |
|||
CurrentLanguage = new LanguageInfo |
|||
{ |
|||
Name = CultureInfo.CurrentUICulture.Name, |
|||
DisplayName = CultureInfo.CurrentUICulture.DisplayName, |
|||
Icon = null //TODO!
|
|||
} |
|||
CurrentLanguage = currentLanguage, |
|||
OtherLanguages = languages.Where(l => l != currentLanguage).ToList() |
|||
}; |
|||
|
|||
foreach (var supportedUiCulture in _options.SupportedUICultures) |
|||
{ |
|||
if (model.CurrentLanguage.Name == supportedUiCulture.Name) |
|||
{ |
|||
continue; |
|||
} |
|||
|
|||
model.OtherLanguages.Add(new LanguageInfo |
|||
{ |
|||
Name = supportedUiCulture.Name, |
|||
DisplayName = supportedUiCulture.DisplayName, |
|||
Icon = null //TODO!
|
|||
}); |
|||
} |
|||
|
|||
|
|||
return View("~/Themes/Basic/Components/Toolbar/LanguageSwitch/Default.cshtml", model); |
|||
} |
|||
|
|||
private LanguageInfo FindCurrentLanguage(IReadOnlyList<LanguageInfo> languages) |
|||
{ |
|||
return languages.FirstOrDefault(l => |
|||
l.CultureName == CultureInfo.CurrentCulture.Name && |
|||
l.UiCultureName == CultureInfo.CurrentUICulture.Name) |
|||
?? languages.FirstOrDefault(l => l.CultureName == CultureInfo.CurrentCulture.Name) |
|||
?? languages.FirstOrDefault(l => l.UiCultureName == CultureInfo.CurrentUICulture.Name); |
|||
} |
|||
} |
|||
} |
|||
|
|||
@ -0,0 +1,22 @@ |
|||
using System.Collections.Generic; |
|||
using System.Threading.Tasks; |
|||
using Microsoft.Extensions.Options; |
|||
using Volo.Abp.DependencyInjection; |
|||
|
|||
namespace Volo.Abp.Localization |
|||
{ |
|||
public class DefaultLanguageProvider : ILanguageProvider, ITransientDependency |
|||
{ |
|||
protected AbpLocalizationOptions Options { get; } |
|||
|
|||
public DefaultLanguageProvider(IOptions<AbpLocalizationOptions> options) |
|||
{ |
|||
Options = options.Value; |
|||
} |
|||
|
|||
public Task<IReadOnlyList<LanguageInfo>> GetLanguagesAsync() |
|||
{ |
|||
return Task.FromResult((IReadOnlyList<LanguageInfo>)Options.Languages); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,10 @@ |
|||
using System.Collections.Generic; |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace Volo.Abp.Localization |
|||
{ |
|||
public interface ILanguageProvider |
|||
{ |
|||
Task<IReadOnlyList<LanguageInfo>> GetLanguagesAsync(); |
|||
} |
|||
} |
|||
@ -0,0 +1,48 @@ |
|||
using System; |
|||
using JetBrains.Annotations; |
|||
|
|||
namespace Volo.Abp.Localization |
|||
{ |
|||
public class LanguageInfo |
|||
{ |
|||
[NotNull] |
|||
public virtual string CultureName { get; protected set; } |
|||
|
|||
[NotNull] |
|||
public virtual string UiCultureName { get; protected set; } |
|||
|
|||
[NotNull] |
|||
public virtual string DisplayName { get; protected set; } |
|||
|
|||
[CanBeNull] |
|||
public virtual string FlagIcon { get; set; } |
|||
|
|||
public LanguageInfo( |
|||
string cultureName, |
|||
string uiCultureName = null, |
|||
string displayName = null, |
|||
string flagIcon = null) |
|||
{ |
|||
ChangeCultureInternal(cultureName, uiCultureName, displayName); |
|||
FlagIcon = flagIcon; |
|||
} |
|||
|
|||
public virtual void ChangeCulture(string cultureName, string uiCultureName = null, string displayName = null) |
|||
{ |
|||
ChangeCultureInternal(cultureName, uiCultureName, displayName); |
|||
} |
|||
|
|||
private void ChangeCultureInternal(string cultureName, string uiCultureName, string displayName) |
|||
{ |
|||
CultureName = Check.NotNullOrWhiteSpace(cultureName, nameof(cultureName)); |
|||
|
|||
UiCultureName = !uiCultureName.IsNullOrWhiteSpace() |
|||
? uiCultureName |
|||
: cultureName; |
|||
|
|||
DisplayName = !displayName.IsNullOrWhiteSpace() |
|||
? displayName |
|||
: cultureName; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,13 @@ |
|||
using System.Collections.Generic; |
|||
using Volo.Abp.Threading; |
|||
|
|||
namespace Volo.Abp.Localization |
|||
{ |
|||
public static class LanguageProviderExtensions |
|||
{ |
|||
public static IReadOnlyList<LanguageInfo> GetLanguages(this ILanguageProvider languageProvider) |
|||
{ |
|||
return AsyncHelper.RunSync(() => languageProvider.GetLanguagesAsync()); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,7 @@ |
|||
namespace Volo.Abp.Localization |
|||
{ |
|||
public static class LocalizationSettingNames |
|||
{ |
|||
public const string DefaultLanguage = "Abp.Localization.DefaultLanguage"; |
|||
} |
|||
} |
|||
@ -0,0 +1,14 @@ |
|||
using Volo.Abp.Settings; |
|||
|
|||
namespace Volo.Abp.Localization |
|||
{ |
|||
public class LocalizationSettingProvider : SettingDefinitionProvider |
|||
{ |
|||
public override void Define(ISettingDefinitionContext context) |
|||
{ |
|||
context.Add( |
|||
new SettingDefinition(LocalizationSettingNames.DefaultLanguage, "en", isVisibleToClients: true) |
|||
); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue