Ejemplo n.º 1
0
        public IStringLocalizer Create(Type resourceSource)
        {
            if (resourceSource == null)
            {
                throw new ArgumentNullException(nameof(resourceSource));
            }

            _logger.Trace($"Getting localizer for type {resourceSource}");

            var typeInfo = resourceSource.GetTypeInfo();
            var assembly = typeInfo.Assembly;

            var typeName = typeInfo.Name;

            if (typeName.IndexOf('`') > 0)
            {
                typeName = typeName.Substring(0, typeName.IndexOf('`'));
            }
            typeName = $"{typeInfo.Namespace}.{typeName}";

            var resourceBaseName = string.IsNullOrWhiteSpace(ResourceRelativePath)
                ? typeInfo.FullName
                : _applicationEnvironment.ApplicationName + "." + ResourceRelativePath + "."
                                   + LocalizerUtil.TrimPrefix(typeName, _applicationEnvironment.ApplicationName + ".");

            _logger.Trace($"Localizer basename: {resourceBaseName}");

            // making generic type to creating instance using resourceSource as generic argument (IStringLocalizer<...>: JsonStringLocalizer<...>)
            var type       = typeof(JsonStringLocalizer <>).MakeGenericType(resourceSource);
            var ctor       = type.GetConstructor(new[] { typeof(string), typeof(string), typeof(ILogger), typeof(IStringLocalizerFactory) });
            var invokeArgs = new object[] { resourceBaseName, _applicationEnvironment.ApplicationName, _logger, this };

            // if cached instance not found, calling the ctor and passing the calculated arguments
            return(_localizerCache.GetOrAdd(resourceBaseName, (JsonStringLocalizer)ctor.Invoke(invokeArgs)));
        }
Ejemplo n.º 2
0
        public IStringLocalizer Create(string baseName, string location)
        {
            if (baseName == null)
            {
                throw new ArgumentNullException(nameof(baseName));
            }

            _logger.Trace($"Getting localizer for baseName {baseName} and location {location}");

            location = location ?? _applicationEnvironment.ApplicationName;

            var resourceBaseName = location + "." + ResourceRelativePath + LocalizerUtil.TrimPrefix(baseName, location + ".");

            var viewExtension = knownViewExtensions.FirstOrDefault(extension => resourceBaseName.EndsWith(extension, StringComparison.OrdinalIgnoreCase));

            if (viewExtension != null)
            {
                resourceBaseName = resourceBaseName.Substring(0, resourceBaseName.Length - viewExtension.Length);
            }

            _logger.Trace($"Localizer basename: {resourceBaseName}");

            return(_localizerCache.GetOrAdd(resourceBaseName, new JsonStringLocalizer(resourceBaseName, _applicationEnvironment.ApplicationName, _logger, this)));
        }