Example #1
0
        private LocalizedString GetLocalizedString(string name, params object[] arguments)
        {
            var _targetCulture     = CultureInfo.CurrentCulture.Name;
            var _targetEqualSource = _transCulture.Equals(_targetCulture, StringComparison.OrdinalIgnoreCase);

            // Option 0: Skip localization if:
            // LocalizeDefaultCulture == false and currentCulture == _transCulture
            if (!_options.LocalizeDefaultCulture && _targetEqualSource)
            {
                return(new LocalizedString(name, string.Format(name, arguments), true, string.Empty));
            }

            // Option 1: Look in the cache
            bool availableInCache = _cache.TryGetValue <TResource>(name, out string value);

            if (availableInCache)
            {
                return(new LocalizedString(name, string.Format(value, arguments), false, string.Empty));
            }

            // Option 2: Look in source file
            bool availableInSource = _provider.TryGetValue <TResource>(name, out value);

            if (availableInSource)
            {
                // Add to cache
                _cache.Set <TResource>(name, value);

                return(new LocalizedString(name, string.Format(value, arguments), false, string.Empty));
            }

            // Option 3: Try online translation service
            //           and don't do online translation if target == source culture,
            //           because online tranlsation services requires two different cultures.
            var availableInTranslate = false;

            if (_options.AutoTranslate && !_targetEqualSource)
            {
                availableInTranslate = _translator.TryTranslate(_transCulture, _targetCulture, name, out value);
                if (availableInTranslate)
                {
                    // Add to cache
                    _cache.Set <TResource>(name, value);
                }
            }

            // Save to source file when AutoAdd is anebled and:
            // A:translation success or, B: AutoTranslate is off or, C: Target and source cultures are same
            // option C: useful when we use code keys to localize defatul culture as well
            if (_options.AutoAddKeys && (availableInTranslate || !_options.AutoTranslate || _targetEqualSource))
            {
                bool savedToResource = _provider.TrySetValue <TResource>(name, value ?? name, "Created by XLocalizer");
                _logger.LogInformation($"Save resource to file, status: '{savedToResource}', key: '{name}', value: '{value ?? name}'");
            }

            return(new LocalizedString(name, string.Format(value ?? name, arguments), !availableInTranslate, typeof(TResource).FullName));
        }
        private LocalizedString GetLocalizedString(string name, params object[] arguments)
        {
            var availableInTranslate = false;

            // Option 1: Look in the cache
            bool availableInCache = _cache.TryGetValue(name, out string value);

            if (!availableInCache)
            {
                var culture = CultureInfo.CurrentCulture.Name;

                // Option 2: Look in resource
                bool availableInSource = _provider.TryGetValue <TResource>(name, out value);

                if (!availableInSource && _options.AutoTranslate)
                {
                    if (_transCulture != culture)
                    {
                        // Option 3: Online translate
                        availableInTranslate = _translator.TryTranslate(_transCulture, culture, name, out value);
                    }
                }

                // add a resource if it is not available in source and auto add is enabled
                if (!availableInSource && _options.AutoAddKeys && _transCulture != culture)
                {
                    // Add a resource only if auto translate is off or if available in translation
                    if (!_options.AutoTranslate || availableInTranslate)
                    {
                        bool savedToResource = _provider.TrySetValue <TResource>(name, value ?? name, "Created by XLocalizer");
                        _logger.LogInformation($"Save resource to file, status: '{savedToResource}', key: '{name}', value: '{value ?? name}'");
                    }
                }

                if (availableInSource || availableInTranslate)
                {
                    // Save to cache
                    _cache.Set(name, value);

                    // Set availability to true
                    availableInCache = true;
                }
            }

            var val = string.Format(value ?? name, arguments);

            return(new LocalizedString(name, val, resourceNotFound: !availableInCache, searchedLocation: typeof(TResource).FullName));
        }