private async Task TranslateItem(ItemObject item, string language, string translationLanguage) { if (item.Item == null) { throw new ArgumentNullException(nameof(item.Item)); } var languageVariant = await kontentRepository.RetrieveLanguageVariant(new RetrieveLanguageVariantParameters { ItemId = item.Item.Id, LanguageId = item.Language?.Id }); foreach (var element in languageVariant.Elements) { switch (element) { case RichTextElement richTextElement: var value = richTextElement.Value; if (value?.Length >= 5000) { var parts = textAnalyzer.SplitHtml(value); var result = ""; foreach (var part in parts) { var(partTranslated, partTranslation) = await Translate(part, translationLanguage); if (partTranslated) { result += partTranslation; } ; } if (!string.IsNullOrWhiteSpace(result)) { richTextElement.Value = result; } break; } var(translated, translation) = await Translate(richTextElement.Value, translationLanguage); if (translated) { richTextElement.Value = translation; } ; break; case UrlSlugElement urlSlugElement: (translated, translation) = await Translate(urlSlugElement.Value, translationLanguage); if (translated) { urlSlugElement.Value = translation.Replace(" ", "-"); } ; break; case TextElement textElement: (translated, translation) = await Translate(textElement.Value, translationLanguage); if (translated) { textElement.Value = translation; } ; break; } } await kontentRepository.UpsertLanguageVariant(new UpsertLanguageVariantParameters { ItemId = item.Item.Id, Language = language, Variant = languageVariant }); }
public async Task <IActionResult> Run( [HttpTrigger( "post", Route = Routes.GitHubCreatePullRequest )] string body, IDictionary <string, string> headers, string typeCodename, string baseName, string repositoryName, string headName ) { try { if (string.IsNullOrWhiteSpace(baseName)) { throw new ApiException("Base name is null or empty."); } if (string.IsNullOrWhiteSpace(repositoryName)) { throw new ApiException("Repository name is null or empty."); } if (string.IsNullOrWhiteSpace(headName)) { throw new ApiException("Head name is null or empty."); } var(valid, getWebhook) = webhookValidator.ValidateWebhook(body, headers); //if (!valid) return LogUnauthorized(); var(data, message) = getWebhook(); switch (message.Type) { case "content_item_variant": switch (message.Operation) { case "change_workflow_step": if (data.Items == null) { throw new ApiException("Webhook does not have items."); } foreach (var item in data.Items) { if (item.ItemReference == null) { throw new ApiException("Item not available."); } var contentItem = await kontentRepository.RetrieveContentItem(item.ItemReference); if (contentItem.TypeReference == null) { throw new ApiException("Item type not available."); } var contentType = await kontentRepository.RetrieveContentType(contentItem.TypeReference); if (contentType.Codename != typeCodename) { continue; } var languageVariant = await kontentRepository.RetrieveLanguageVariant(new RetrieveLanguageVariantParameters { ItemReference = item.ItemReference, LanguageReference = item.LanguageReference, TypeReference = new CodenameReference(typeCodename) }); if (languageVariant == null) { throw new ApiException("Variant could not be retrieved."); } if (languageVariant.Elements == null) { throw new ApiException("Variant does not have elements."); } if (contentType.Elements == null) { throw new ApiException("Type does not have elements."); } var(title, description, motivation, readmeUrl, categories, thumbnail, thumbnailType) = await ParseElements(languageVariant.Elements, contentType.Elements); var branch = ToKebabCase(title); await RunPullRequest( $"{baseName}/{repositoryName}", $"{headName}:{branch}", $"{headName}/{repositoryName}", branch, title, description, motivation, readmeUrl, categories, thumbnail, thumbnailType ); } break; } break; } return(LogOk()); } catch (ApiException ex) when(ex.Skip) { return(LogOkException(ex)); } catch (Exception ex) { return(LogException(ex)); } }
private async Task TranslateItem(Item item, string languageCodename, string translationLanguage) { if (item.ItemReference == null) { throw new ArgumentNullException(nameof(item.ItemReference)); } var languageVariant = await kontentRepository.RetrieveLanguageVariant(new RetrieveLanguageVariantParameters { ItemReference = item.ItemReference, LanguageReference = item.LanguageReference }); if (languageVariant == null) { throw new NotImplementedException("Variant could not be retrieved."); } if (languageVariant.Elements == null) { throw new NotImplementedException("Variant does not have elements."); } foreach (var element in languageVariant.Elements) { switch (element) { case RichTextElement richTextElement: { var value = richTextElement.Value; if (value?.Length >= 5000) { var parts = textAnalyzer.SplitHtml(value); var longResult = ""; foreach (var part in parts) { var(translated, translation) = await Translate(part, translationLanguage); if (translated) { longResult += translation; } ; } if (!string.IsNullOrWhiteSpace(longResult)) { richTextElement.Value = longResult; } break; } var result = await Translate(richTextElement.Value, translationLanguage); if (result.translated) { richTextElement.Value = result.translation; } ; break; } case UrlSlugElement urlSlugElement: { var(translated, translation) = await Translate(urlSlugElement.Value, translationLanguage); if (translated) { urlSlugElement.Value = translation.Replace(" ", "-"); } ; break; } case TextElement textElement: { var(translated, translation) = await Translate(textElement.Value, translationLanguage); if (translated) { textElement.Value = translation; } ; break; } } } await kontentRepository.UpsertLanguageVariant(new UpsertLanguageVariantParameters { LanguageReference = new CodenameReference(languageCodename), Variant = languageVariant }); }