public void TranslateDictionaryItem(DictionaryApiInstruction apiInstruction, string subscriptionKey, string uriBase, IDictionaryItem dictionaryItem, ILanguage defaultLanguage, IEnumerable <ILanguage> allLanguages)
        {
            if (dictionaryItem != null)
            {
                var valueToTranslate = dictionaryItem.Translations.FirstOrDefault(x => x.LanguageId == defaultLanguage.Id)?.Value;

                if (valueToTranslate == null || (string.IsNullOrEmpty(valueToTranslate) && apiInstruction.FallbackToKey))
                {
                    valueToTranslate = dictionaryItem.ItemKey;
                }

                if (valueToTranslate != null && !string.IsNullOrWhiteSpace(valueToTranslate))
                {
                    if (dictionaryItem.Translations == null || !dictionaryItem.Translations.Any())
                    {
                        AddDictionaryTranslationsForAllLanguages(subscriptionKey, uriBase, dictionaryItem, defaultLanguage, allLanguages, valueToTranslate);
                    }
                    else
                    {
                        UpdateDictionaryTranslations(apiInstruction, subscriptionKey, uriBase, dictionaryItem, defaultLanguage, allLanguages, valueToTranslate);
                    }
                    _localizationService.Save(dictionaryItem);
                }
            }
        }
        public void UpdateDictionaryTranslations(DictionaryApiInstruction apiInstruction, string subscriptionKey, string uriBase, IDictionaryItem dictionaryItem, ILanguage defaultLanguage, IEnumerable <ILanguage> allLanguages, string valueToTranslate)
        {
            var allLanguagesList = allLanguages.ToList();

            foreach (var translation in dictionaryItem.Translations)
            {
                var cultureToTranslateTo = allLanguagesList.FirstOrDefault(x => x.Id == translation.LanguageId)?.IsoCode;

                if (string.IsNullOrWhiteSpace(translation.Value) || apiInstruction.OverwriteExistingValues)
                {
                    var    result          = _textService.MakeTranslationRequestAsync(valueToTranslate, subscriptionKey, uriBase, new[] { cultureToTranslateTo }, defaultLanguage.IsoCode);
                    JToken translatedValue = CommonHelpers.GetTranslatedValue(result);
                    translation.Value = translatedValue.ToString();
                }
            }
        }
Пример #3
0
        public bool SubmitTranslateDictionary(DictionaryApiInstruction apiInstruction)
        {
            var dictionaryItem   = _localizationService.GetDictionaryItemById(apiInstruction.NodeId);
            var allLanguages     = _localizationService.GetAllLanguages();
            var allLanguagesList = allLanguages.ToList();
            var defaultLanguage  = allLanguagesList.FirstOrDefault(x => x.IsoCode == DefaultLanguageCode);

            _dictionaryTranslationService.TranslateDictionaryItem(apiInstruction, SubscriptionKey, UriBase, dictionaryItem, defaultLanguage, allLanguagesList);

            if (apiInstruction.IncludeDescendants)
            {
                var dictionaryDescendants     = _localizationService.GetDictionaryItemDescendants(dictionaryItem.Key);
                var descendantDictionaryItems = dictionaryDescendants.ToList();
                if (descendantDictionaryItems.Any())
                {
                    foreach (var descendantDictionaryItem in descendantDictionaryItems)
                    {
                        _dictionaryTranslationService.TranslateDictionaryItem(apiInstruction, SubscriptionKey, UriBase, descendantDictionaryItem, defaultLanguage, allLanguagesList);
                    }
                }
            }

            return(true);
        }