context: using System;
  using System.Collections.Generic;
  using System.Data;
  using System.IO;
  using System.Linq;
  using System.Text;
  using System.Xml;
  using Nop.Core;
  using Nop.Core.Caching;
  using Nop.Core.Data;
  using Nop.Core.Domain.Common;
  using Nop.Core.Domain.Localization;
  using Nop.Data;
  using Nop.Services.Events;
  using Nop.Services.Logging;
  
  namespace Nop.Services.Localization
  {
      /// <summary>
      /// Provides information about localization
      /// </summary>
      public partial class LocalizationService : ILocalizationService
      {
          #region Constants
  
          /// <summary>
          /// Key for caching
          /// </summary>
          /// <remarks>
          /// {0} : language ID
          /// </remarks>
          private const string LOCALSTRINGRESOURCES_ALL_KEY = "Nop.lsr.all-{0}";
          /// <summary>
          /// Key for caching
          /// </summary>
          /// <remarks>
          /// {0} : language ID
          /// {1} : resource key
          /// </remarks>
          private const string LOCALSTRINGRESOURCES_BY_RESOURCENAME_KEY = "Nop.lsr.{0}-{1}";
          /// <summary>
          /// Key pattern to clear cache
          /// </summary>
          private const string LOCALSTRINGRESOURCES_PATTERN_KEY = "Nop.lsr.";
  
          #endregion
  
          #region Fields
  
          private readonly IRepository<LocaleStringResource> _lsrRepository;
          private readonly IWorkContext _workContext;
          private readonly ILogger _logger;
          private readonly ILanguageService _languageService;
          private readonly ICacheManager _cacheManager;
          private readonly IDataProvider _dataProvider;
          private readonly IDbContext _dbContext;
          private readonly CommonSettings _commonSettings;
          private readonly LocalizationSettings _localizationSettings;
          private readonly IEventPublisher _eventPublisher;
  
          #endregion
  
          #region Ctor
  
          /// <summary>
          /// Ctor
          /// </summary>
          /// <param name="cacheManager">Cache manager</param>
          /// <param name="logger">Logger</param>
          /// <param name="workContext">Work context</param>
          /// <param name="lsrRepository">Locale string resource repository</param>
          /// <param name="languageService">Language service</param>
          /// <param name="dataProvider">Data provider</param>
          /// <param name="dbContext">Database Context</param>
          /// <param name="commonSettings">Common settings</param>
          /// <param name="localizationSettings">Localization settings</param>
          /// <param name="eventPublisher">Event published</param>
          public LocalizationService(ICacheManager cacheManager,
              ILogger logger, IWorkContext workContext,
              IRepository<LocaleStringResource> lsrRepository, 
              ILanguageService languageService,
              IDataProvider dataProvider, IDbContext dbContext, CommonSettings commonSettings,
              LocalizationSettings localizationSettings, IEventPublisher eventPublisher)
          {
              this._cacheManager = cacheManager;
              this._logger = logger;
              this._workContext = workContext;
              this._lsrRepository = lsrRepository;
              this._languageService = languageService;
              this._dataProvider = dataProvider;
              this._dbContext = dbContext;
              this._commonSettings = commonSettings;
              this._localizationSettings = localizationSettings;
              this._eventPublisher = eventPublisher;
          }
  
          #endregion
  
          #region Methods
  
          /// <summary>
          /// Deletes a locale string resource
          /// </summary>
          /// <param name="localeStringResource">Locale string resource</param>
          public virtual void DeleteLocaleStringResource(LocaleStringResource localeStringResource)
          {
              if (localeStringResource == null)
                  throw new ArgumentNullException("localeStringResource");
  
              _lsrRepository.Delete(localeStringResource);
  
              //cache
              _cacheManager.RemoveByPattern(LOCALSTRINGRESOURCES_PATTERN_KEY);
  
              //event notification
              _eventPublisher.EntityDeleted(localeStringResource);
          }
  
          /// <summary>
          /// Gets a locale string resource
          /// </summary>
          /// <param name="localeStringResourceId">Locale string resource identifier</param>
          /// <returns>Locale string resource</returns>
          public virtual LocaleStringResource GetLocaleStringResourceById(int localeStringResourceId)
          {
              if (localeStringResourceId == 0)
                  return null;
  
              return _lsrRepository.GetById(localeStringResourceId);
          }
  
          /// <summary>
          /// Gets a locale string resource
          /// </summary>
          /// <param name="resourceName">A string representing a resource name</param>
          /// <returns>Locale string resource</returns>
          public virtual LocaleStringResource GetLocaleStringResourceByName(string resourceName)
          {
              if (_workContext.WorkingLanguage != null)
                  return GetLocaleStringResourceByName(resourceName, _workContext.WorkingLanguage.Id);
  
              return null;
          }
  
          /// <summary>
          /// Gets a locale string resource
          /// </summary>
          /// <param name="resourceName">A string representing a resource name</param>
          /// <param name="languageId">Language identifier</param>
          /// <param name="logIfNotFound">A value indicating whether to log error if locale string resource is not found</param>
          /// <returns>Locale string resource</returns>
          public virtual LocaleStringResource GetLocaleStringResourceByName(string resourceName, int languageId,
              bool logIfNotFound = true)
          {
              var query = from lsr in _lsrRepository.Table
                          orderby lsr.ResourceName
                          where lsr.LanguageId == languageId && lsr.ResourceName == resourceName
                          select lsr;
              var localeStringResource = query.FirstOrDefault();
  
              if (localeStringResource == null && logIfNotFound)
                  _logger.Warning(string.Format("Resource string ({0}) not found. Language ID = {1}", resourceName, languageId));
              return localeStringResource;
          }
  
          /// <summary>
          /// Gets all locale string resources by language identifier
          /// </summary>
          /// <param name="languageId">Language identifier</param>
          /// <returns>Locale string resources</returns>
          public virtual IList<LocaleStringResource> GetAllResources(int languageId)
          {
              var query = from l in _lsrRepository.Table
                          orderby l.ResourceName
                          where l.LanguageId == languageId
                          select l;
              var locales = query.ToList();
              return locales;
          }
  
          /// <summary>
          /// Inserts a locale string resource
          /// </summary>
          /// <param name="localeStringResource">Locale string resource</param>
          public virtual void InsertLocaleStringResource(LocaleStringResource localeStringResource)
          {
              if (localeStringResource == null)
                  throw new ArgumentNullException("localeStringResource");
              
              _lsrRepository.Insert(localeStringResource);
  
              //cache
              _cacheManager.RemoveByPattern(LOCALSTRINGRESOURCES_PATTERN_KEY);
  
              //event notification
              _eventPublisher.EntityInserted(localeStringResource);
          }
  
          /// <summary>
          /// Updates the locale string resource
          /// </summary>
          /// <param name="localeStringResource">Locale string resource</param>
          public virtual void UpdateLocaleStringResource(LocaleStringResource localeStringResource)
          {
              if (localeStringResource == null)
                  throw new ArgumentNullException("localeStringResource");
  
              _lsrRepository.Update(localeStringResource);
  
              //cache
              _cacheManager.RemoveByPattern(LOCALSTRINGRESOURCES_PATTERN_KEY);
  
              //event notification
              _eventPublisher.EntityUpdated(localeStringResource);
          }
  
          /// <summary>
          /// Gets all locale string resources by language identifier
          /// </summary>
          /// <param name="languageId">Language identifier</param>
          /// <returns>Locale string resources</returns>
          public virtual Dictionary<string, KeyValuePair<int,string>> GetAllResourceValues(int languageId)
          {
              string key = string.Format(LOCALSTRINGRESOURCES_ALL_KEY, languageId);
              return _cacheManager.Get(key, () =>
              {
                  //we use no tracking here for performance optimization
                  //anyway records are loaded only for read-only operations
                  var query = from l in _lsrRepository.TableNoTracking
                              orderby l.ResourceName
                              where l.LanguageId == languageId
                              select l;
                  var locales = query.ToList();
                  //format: <name, <id, value>>
                  var dictionary = new Dictionary<string, KeyValuePair<int, string>>();
                  foreach (var locale in locales)
                  {
  
gt:                     var resourceName = locale.ResourceName.ToLowerInvariant();