Exemple #1
0
        /// <summary>
        /// Returns a string localizer
        /// </summary>
        /// <param name="baseName">Base Name</param>
        /// <param name="location">Location</param>
        /// <returns>String Localizer</returns>
        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.
            string resourceBaseName = location + "." + _resourcesRelativePath + LocalizerUtil.TrimPrefix(baseName, location + ".");

            string 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, _fallbackCulture)));
        }
Exemple #2
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="baseName">Base Name</param>
        /// <param name="applicationName">Application Name</param>
        /// <param name="logger">Logger</param>
        /// <param name="fallbackCulture">Fallback Culture</param>
        public JsonStringLocalizer(string baseName, string applicationName, ILogger logger, CultureInfo fallbackCulture)
        {
            if (baseName == null)
            {
                throw new ArgumentNullException(nameof(baseName));
            }
            if (applicationName == null)
            {
                throw new ArgumentNullException(nameof(applicationName));
            }
            if (logger == null)
            {
                throw new ArgumentNullException(nameof(logger));
            }

            this._baseName        = baseName;
            this._applicationName = applicationName;
            this._logger          = logger;
            this._fallbackCulture = fallbackCulture;

            // Get a list of possible resource file locations.
            _resourceFileLocations = LocalizerUtil.ExpandPaths(baseName, applicationName).ToList();
            foreach (string resFileLocation in _resourceFileLocations)
            {
                logger.LogTrace($"Resource file location base path: {resFileLocation}");
            }
        }
Exemple #3
0
        /// <summary>
        /// Returns a string localizer
        /// </summary>
        /// <param name="resourceSource">Resource Source</param>
        /// <returns>String Localizer</returns>
        public IStringLocalizer Create(Type resourceSource)
        {
            if (resourceSource == null)
            {
                throw new ArgumentNullException(nameof(resourceSource));
            }

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

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

            // Re-root the base name if a resources path is set.
            string 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, _fallbackCulture)));
        }