Example #1
0
            /// <summary>
            /// Place where query handling happens
            /// </summary>
            /// <param name="query">This is the query instance</param>
            /// <returns>
            /// You have to return something from the query execution. Of course you can return <c>null</c> as well if you
            /// will.
            /// </returns>
            public string Execute(Query query)
            {
                if (!_configurationContext.EnableLocalization())
                {
                    return(query.Key);
                }

                var key = query.Key;

                if (_configurationContext.BaseCacheManager.AreKnownKeysStored() && !_configurationContext.BaseCacheManager.IsKeyKnown(key))
                {
                    // we are here because of couple of reasons:
                    //  * someone is asking for non-existing resource (known keys are synced and key does not exist)
                    //  * someone has programmatically created resource and query is made on different cluster node (cache is still cold for this resource)
                    //
                    // if this resource is not yet found in cache
                    // we can try to lookup resource once more in database and if not found - then we can short-break the circuit

                    GetCachedResourceOrReadFromStorage(query);
                }

                if (_configurationContext.DiagnosticsEnabled)
                {
                    _logger.Debug($"Executing query for resource key `{key}` (lang: `{query.Language.Name})..");
                }

                var localizationResource = GetCachedResourceOrReadFromStorage(query);

                return(query.FallbackToInvariant
                    ? localizationResource.Translations.ByLanguage(query.Language, true)
                    : localizationResource.Translations.GetValueWithFallback(
                           query.Language,
                           _configurationContext.FallbackList.GetFallbackLanguages(query.Language)));
            }
Example #2
0
            /// <summary>
            /// Place where query handling happens
            /// </summary>
            /// <param name="query">This is the query instance</param>
            /// <returns>
            /// You have to return something from the query execution. Of course you can return <c>null</c> as well if you
            /// will.
            /// </returns>
            public string Execute(GetTranslation.Query query)
            {
                if (!_configurationContext.EnableLocalization())
                {
                    return(query.Key);
                }

                var key = query.Key;

                // we can check whether we know this resource at all
                // if not - we can break circuit here
                if (!_configurationContext.BaseCacheManager.IsKeyKnown(key))
                {
                    return(null);
                }

                var cacheKey = CacheKeyHelper.BuildKey(key);

                if (_configurationContext.DiagnosticsEnabled)
                {
                    _configurationContext.Logger?.Debug($"Executing query for resource key `{query.Key}` (lang: `{query.Language.Name})..");
                }

                var localizationResource = _configurationContext.CacheManager.Get(cacheKey) as LocalizationResource;

                if (localizationResource == null)
                {
                    if (_configurationContext.DiagnosticsEnabled)
                    {
                        _configurationContext.Logger?.Info(
                            $"MISSING: Resource Key (culture: {query.Language.Name}): {query.Key}. Probably class is not decorated with either [LocalizedModel] or [LocalizedResource] attribute.");
                    }

                    // resource is not found in the cache, let's check database
                    localizationResource = GetResourceFromDb(key) ?? LocalizationResource.CreateNonExisting(key);
                    _configurationContext.CacheManager.Insert(cacheKey, localizationResource, true);
                }

                return(localizationResource.Translations.GetValueWithFallback(
                           query.Language,
                           _configurationContext.FallbackList.GetFallbackLanguages(query.Language)));
            }
        internal string GetTranslation(string resourceKey)
        {
            var result = resourceKey;

            if (!_configurationContext.EnableLocalization())
            {
                return(result);
            }

            var localizedDisplayName = _localizationProvider.GetString(resourceKey);

            result = localizedDisplayName;

            // for the legacy purposes - we need to look for this resource translation using display name
            // once again - this will make sure that existing XPath resources are still working
            if (localizedDisplayName != null && !_configurationContext.ShouldLookupResource(localizedDisplayName))
            {
                result = _localizationProvider.GetString(localizedDisplayName);
            }

            // If other data annotations exists except for [Display], an exception is thrown when display name is ""
            // It should be null to avoid exception as ModelMetadata.GetDisplayName only checks for null and not String.Empty
            return(string.IsNullOrWhiteSpace(localizedDisplayName) ? null : result);
        }