예제 #1
0
        async Task<IEnumerable<SelectListItem>> GetAvailableCulturesAsync()
        {
            // Build timezones 
            var locales = new List<SelectListItem>
            {
                new SelectListItem
                {
                    Text = S["-"],
                    Value = ""
                }
            };

            // From all locale descriptors get a unique list of supported culture cdes
            var uniqueLocales = new List<string>();
            var localeDescriptors = await _localeProvider.GetLocalesAsync();
            foreach (var localDescriptor in localeDescriptors)
            {
                if (!uniqueLocales.Contains(localDescriptor.Descriptor.Name))
                {
                    uniqueLocales.Add(localDescriptor.Descriptor.Name);
                }
            }

            foreach (var locale in uniqueLocales)
            {
                locales.Add(new SelectListItem
                {
                    Text = locale,
                    Value = locale
                });
            }

            return locales;
        }
예제 #2
0
        /// <summary>
        /// Returns all locale resource types matching the supplied culture.
        /// </summary>
        /// <param name="cultureCode"></param>
        /// <returns></returns>
        public async Task <IEnumerable <ComposedLocaleResource> > GetResourcesAsync(string cultureCode)
        {
            if (String.IsNullOrEmpty(cultureCode))
            {
                throw new ArgumentNullException(nameof(cultureCode));
            }

            var token = _cacheManager.GetOrCreateToken(typeof(ComposedLocaleResource), cultureCode);

            return(await _cacheManager.GetOrCreateAsync(token, async (cacheEntry) =>
            {
                var resources = new List <ComposedLocaleResource>();
                foreach (var locale in await _localeProvider.GetLocalesAsync())
                {
                    if (locale.Descriptor.Name.Equals(cultureCode, StringComparison.OrdinalIgnoreCase))
                    {
                        resources.AddRange(locale.Resources);
                    }
                }


                return resources;
            }));
        }
예제 #3
0
        public async Task WatchForChanges()
        {
            // Is watching enabled?
            if (!_localeOptions.Value.WatchForChanges)
            {
                return;
            }

            if (_watching == false)
            {
                var locales = await _localeProvider.GetLocalesAsync();

                if (locales != null)
                {
                    foreach (var locale in locales)
                    {
                        if (_logger.IsEnabled(LogLevel.Information))
                        {
                            _logger.LogInformation("Attempting to watch for changes to locale directory at '{0}'.", locale.Descriptor.DirectoryInfo.FullName);
                        }

                        var watcher = new FileSystemWatcher
                        {
                            Path = @locale.Descriptor.DirectoryInfo.FullName
                        };

                        watcher.Changed += async(sender, args) =>
                        {
                            _localeProvider.Dispose();
                            await _localeStore.DisposeAsync();
                        };

                        watcher.EnableRaisingEvents = true;

                        if (_logger.IsEnabled(LogLevel.Information))
                        {
                            _logger.LogInformation("Successfully watching for changes to locale directory at '{0}'.", watcher.Path);
                        }
                    }
                }
                _watching = true;
            }
        }