Exemple #1
0
        public IStringLocalizer Create(Type resourceSource)
        {
            if (resourceSource == null)
            {
                throw new ArgumentNullException(nameof(resourceSource));
            }

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

            var typeInfo         = resourceSource.GetTypeInfo();
            var assemblyName     = typeInfo.Assembly.GetName();
            var resourceBaseName = string.Empty;

            if (Assembly.GetEntryAssembly().FullName != typeInfo.Assembly.FullName)
            {
                // Different logic if coming from another assembly
                resourceBaseName = string.IsNullOrEmpty(_resourcesRelativePath)
                ? typeInfo.FullName
                : assemblyName.Name + "." + _resourcesRelativePath + "." + typeInfo.Name;
            }
            else
            {
                // Re-root the base name if a resources path is set.
                resourceBaseName = string.IsNullOrEmpty(_resourcesRelativePath)
                ? typeInfo.FullName
                : _applicationEnvironment.ApplicationName + "." + _resourcesRelativePath +
                                   LocalizerUtil.TrimPrefix(typeInfo.FullName, _applicationEnvironment.ApplicationName + ".");
            }
            _logger.LogTrace($"Localizer basename: {resourceBaseName}");

            return(_localizerCache.GetOrAdd(
                       resourceBaseName, new JsonStringLocalizer(resourceBaseName, _applicationEnvironment.ApplicationName, _logger, _options)));
        }
Exemple #2
0
        public IStringLocalizer Create(string baseName, string location)
        {
            if (baseName == null)
            {
                throw new ArgumentNullException(nameof(baseName));
            }

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

            location = location ?? _applicationEnvironment.ApplicationName;

            // Re-root base name if a resources path is set and strip the cshtml part.
            var resourceBaseName = location + "." + _resourcesRelativePath + LocalizerUtil.TrimPrefix(baseName, location + ".");

            var viewExtension = KnownViewExtensions.FirstOrDefault(extension => resourceBaseName.EndsWith(extension));

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

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

            return(_localizerCache.GetOrAdd(
                       resourceBaseName, new JsonStringLocalizer(resourceBaseName, _applicationEnvironment.ApplicationName, _logger, _options)));
        }
        public JsonStringLocalizer(string baseName, string applicationName, ILogger logger, IOptions <JsonLocalizationOptions> options)
        {
            if (baseName == null)
            {
                throw new ArgumentNullException(nameof(baseName));
            }
            if (applicationName == null)
            {
                throw new ArgumentNullException(nameof(applicationName));
            }
            if (logger == null)
            {
                throw new ArgumentNullException(nameof(logger));
            }
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            this._baseName        = baseName;
            this._applicationName = applicationName;
            this._logger          = logger;
            this._options         = options;

            // Get a list of possible resource file locations.
            _resourceFileLocations = LocalizerUtil.ExpandPaths(baseName, applicationName).ToList();
            foreach (var resFileLocation in _resourceFileLocations)
            {
                logger.LogTrace($"Resource file location base path: {resFileLocation}");
            }
        }