Try your search with a different keyword or use * as a wildcard.
using System.Globalization;
using Nop.Core.Caching;
using Nop.Core.Domain.Localization;
using Nop.Data;
using Nop.Services.Configuration;
using Nop.Services.Stores;
namespace Nop.Services.Localization;
///
/// Language service
///
public partial class LanguageService : ILanguageService
{
#region Fields
protected readonly IRepository _languageRepository;
protected readonly ISettingService _settingService;
protected readonly IStaticCacheManager _staticCacheManager;
protected readonly IStoreMappingService _storeMappingService;
protected readonly LocalizationSettings _localizationSettings;
#endregion
#region Ctor
public LanguageService(IRepository languageRepository,
ISettingService settingService,
IStaticCacheManager staticCacheManager,
IStoreMappingService storeMappingService,
LocalizationSettings localizationSettings)
{
_languageRepository = languageRepository;
_settingService = settingService;
_staticCacheManager = staticCacheManager;
_storeMappingService = storeMappingService;
_localizationSettings = localizationSettings;
}
#endregion
#region Methods
///
/// Deletes a language
///
/// Language
/// A task that represents the asynchronous operation
public virtual async Task DeleteLanguageAsync(Language language)
{
ArgumentNullException.ThrowIfNull(language);
//update default admin area language (if required)
if (_localizationSettings.DefaultAdminLanguageId == language.Id)
foreach (var activeLanguage in await GetAllLanguagesAsync())
{
if (activeLanguage.Id == language.Id)
continue;
_localizationSettings.DefaultAdminLanguageId = activeLanguage.Id;
await _settingService.SaveSettingAsync(_localizationSettings);
break;
}
await _languageRepository.DeleteAsync(language);
}
///
/// Gets all languages
///
/// Load records allowed only in a specified store; pass 0 to load all records
/// A value indicating whether to show hidden records
///
/// A task that represents the asynchronous operation
/// The task result contains the languages
///
public virtual async Task> GetAllLanguagesAsync(bool showHidden = false, int storeId = 0)
{
//cacheable copy
var key = _staticCacheManager.PrepareKeyForDefaultCache(NopLocalizationDefaults.LanguagesAllCacheKey, storeId, showHidden);
var languages = await _staticCacheManager.GetAsync(key, async () =>
{
var allLanguages = await _languageRepository.GetAllAsync(query =>
{
if (!showHidden)
query = query.Where(l => l.Published);
query = query.OrderBy(l => l.DisplayOrder).ThenBy(l => l.Id);
return query;
});
//store mapping
if (storeId > 0)
allLanguages = await allLanguages
.WhereAwait(async l => await _storeMappingService.AuthorizeAsync(l, storeId))
.ToListAsync();
return allLanguages;
});
return languages;
}
///
/// Gets all languages
///
/// Load records allowed only in a specified store; pass 0 to load all records
/// A value indicating whether to show hidden records
///
/// The languages
///
public virtual IList GetAllLanguages(bool showHidden = false, int storeId = 0)
{
//cacheable copy
var key = _staticCacheManager.PrepareKeyForDefaultCache(NopLocalizationDefaults.LanguagesAllCacheKey, storeId, showHidden);
var languages = _staticCacheManager.Get(key, () =>
{
var allLanguages = _languageRepository.GetAll(query =>
{
if (!showHidden)
query = query.Where(l => l.Published);
query = query.OrderBy(l => l.DisplayOrder).ThenBy(l => l.Id);
return query;
});
//store mapping
if (storeId > 0)
allLanguages = allLanguages
.Where(l => _storeMappingService.Authorize(l, storeId))
.ToList();
return allLanguages;
});
return languages;
}
///
/// Gets a language
///
/// Language identifier
///
/// A task that represents the asynchronous operation
/// The task result contains the language
///
public virtual async Task GetLanguageByIdAsync(int languageId)
{
return await _languageRepository.GetByIdAsync(languageId, cache => default);
}
///
/// Inserts a language
///
/// Language
/// A task that represents the asynchronous operation
public virtual async Task InsertLanguageAsync(Language language)
{
await _languageRepository.InsertAsync(language);
}
///
/// Updates a language
///
/// Language
/// A task that represents the asynchronous operation
public virtual async Task UpdateLanguageAsync(Language language)
{
//update language
await _languageRepository.UpdateAsync(language);
}
///
/// Get 2 letter ISO language code
///
/// Language
/// ISO language code
public virtual string GetTwoLetterIsoLanguageName(Language language)
{
ArgumentNullException.ThrowIfNull(language);
if (string.IsNullOrEmpty(language.LanguageCulture))
return "en";
var culture = new CultureInfo(language.LanguageCulture);
var code = culture.TwoLetterISOLanguageName;
return string.IsNullOrEmpty(code) ? "en" : code;
}
#endregion
}