Ejemplo n.º 1
0
        public virtual async Task <string> GetCurrentLanguageCodeAsync(CancellationToken cancellationToken = default, bool throwExceptionIfNotExists = true)
        {
            //foreach (var languageResolver in _languageResolvers)
            //{
            //    var languageCode = languageResolver.GetCurrentLanguageCode();
            //    if (languageCode.HasValue())
            //    {
            //        if (validateLanguageExists)
            //        {
            //            var exist = (await _languageRepository.GetAllCachedAsync(cancellationToken))
            //                .Any(p => p.LanguageCode.Equals(languageCode, StringComparison.OrdinalIgnoreCase));
            //            if (!exist) throw new Exception($"Current lagnuage '{languageCode}' dose not exists in languages table.");
            //        }
            //        return languageCode;
            //    }
            //}
            //throw new InvalidOperationException("Current language not found!");

            //TODO: return default language [OR] throw exception when language not found ?
            var languageCode = _languageResolver.GetCurrentLanguageCode();

            if (throwExceptionIfNotExists)
            {
                var exists = (await _languageRepository.GetAllCachedAsync(cancellationToken))
                             .Any(p => p.LanguageCode.Equals(languageCode, StringComparison.OrdinalIgnoreCase));

                if (!exists)
                {
                    throw new Exception($"Current lagnuage '{languageCode}' dose not exists in languages table.");
                }
            }
            return(languageCode);
        }