@Test public void canTranslateHTML() { List<String> source = ImmutableList.of("hello"); List<Translation> expectedTrans = ImmutableList.of(translation); when(translate.translate(source, Translate.TranslateOption.targetLanguage("zh"), Translate.TranslateOption.format("html"))) .thenReturn(expectedTrans); List<AugmentedTranslation> translations = backend.translate(source, new BackendLocaleCodeImpl("en"), new BackendLocaleCodeImpl("zh"), StringType.HTML, Optional.empty()); assertThat(translations).hasSize(expectedTrans.size()); AugmentedTranslation augmentedTranslation = translations.get(0); assertThat(augmentedTranslation.getPlainTranslation()).isEqualTo(translation.getTranslatedText()); }
@Override public List<AugmentedTranslation> translate(List<String> contents, BackendLocaleCode srcLocale, BackendLocaleCode targetLocale, StringType stringType, Optional<String> category) throws MTException { String format; switch (stringType) { case HTML: case XML: format = "html"; break; case TEXT_PLAIN: format = "text"; break; default: throw new IllegalArgumentException(stringType.name()); } List<Translate.TranslateOption> options = Lists.newLinkedList(); options.add(Translate.TranslateOption .targetLanguage( targetLocale.getLocaleCode())); options.add(Translate.TranslateOption.format(format)); Translate.TranslateOption[] translateOptions = options .toArray(new Translate.TranslateOption[0]); if (!googleCredential.exists()) { throw new BadRequestException("Google Default Credential file is not setup"); } try { List<Translation> translations = new ArrayList<>(); int batchStart = 0; while (batchStart < contents.size()) { int batchEnd = Math.min(batchStart + BATCH_SIZE, contents.size()); translations.addAll(translate.translate( contents.subList(batchStart, batchEnd), translateOptions)); batchStart = batchEnd; } return translations.stream() .map(translation -> new AugmentedTranslation( translation.getTranslatedText(), dtoUtil.toJSON(translation))).collect( Collectors.toList()); } catch (Exception e) { throw new MTException( "Unable to get translations from Google API", e); } }
GoogleTranslatorBackend implements TranslatorBackend { @Override public List<AugmentedTranslation> translate(List<String> contents, BackendLocaleCode srcLocale, BackendLocaleCode targetLocale, StringType stringType, Optional<String> category) throws MTException { String format; switch (stringType) { case HTML: case XML: format = "html"; break; case TEXT_PLAIN: format = "text"; break; default: throw new IllegalArgumentException(stringType.name()); } List<Translate.TranslateOption> options = Lists.newLinkedList(); options.add(Translate.TranslateOption .targetLanguage( targetLocale.getLocaleCode())); options.add(Translate.TranslateOption.format(format)); Translate.TranslateOption[] translateOptions = options .toArray(new Translate.TranslateOption[0]); if (!googleCredential.exists()) { throw new BadRequestException("Google Default Credential file is not setup"); } try { List<Translation> translations = new ArrayList<>(); int batchStart = 0; while (batchStart < contents.size()) { int batchEnd = Math.min(batchStart + BATCH_SIZE, contents.size()); translations.addAll(translate.translate( contents.subList(batchStart, batchEnd), translateOptions)); batchStart = batchEnd; } return translations.stream() .map(translation -> new AugmentedTranslation( translation.getTranslatedText(), dtoUtil.toJSON(translation))).collect( Collectors.toList()); } catch (Exception e) { throw new MTException( "Unable to get translations from Google API", e); } } }
GoogleTranslatorBackend implements TranslatorBackend { @Override public List<AugmentedTranslation> translate(List<String> contents, BackendLocaleCode srcLocale, BackendLocaleCode targetLocale, StringType stringType, Optional<String> category) throws MTException { String format; switch (stringType) { case HTML: case XML: format = "html"; break; case TEXT_PLAIN: format = "text"; break; default: throw new IllegalArgumentException(stringType.name()); } List<Translate.TranslateOption> options = Lists.newLinkedList(); options.add(Translate.TranslateOption .targetLanguage( targetLocale.getLocaleCode())); options.add(Translate.TranslateOption.format(format)); Translate.TranslateOption[] translateOptions = options .toArray(new Translate.TranslateOption[0]); if (!googleCredential.exists()) { throw new BadRequestException("Google Default Credential file is not setup"); } try { List<Translation> translations = new ArrayList<>(); int batchStart = 0; while (batchStart < contents.size()) { int batchEnd = Math.min(batchStart + BATCH_SIZE, contents.size()); translations.addAll(translate.translate( contents.subList(batchStart, batchEnd), translateOptions)); batchStart = batchEnd; } return translations.stream() .map(translation -> new AugmentedTranslation( translation.getTranslatedText(), dtoUtil.toJSON(translation))).collect( Collectors.toList()); } catch (Exception e) { throw new MTException( "Unable to get translations from Google API", e); } } @Inject  GoogleTranslatorBackend(DTOUtil dtoUtil,
            @Credentials(BackendID.GOOGLE) GoogleCredential googleCredential); @VisibleForTesting protected  GoogleTranslatorBackend(Translate translate,
            DTOUtil dtoUtil, GoogleCredential googleCredential); }
GoogleTranslatorBackend implements TranslatorBackend { @Override public List<AugmentedTranslation> translate(List<String> contents, BackendLocaleCode srcLocale, BackendLocaleCode targetLocale, StringType stringType, Optional<String> category) throws MTException { String format; switch (stringType) { case HTML: case XML: format = "html"; break; case TEXT_PLAIN: format = "text"; break; default: throw new IllegalArgumentException(stringType.name()); } List<Translate.TranslateOption> options = Lists.newLinkedList(); options.add(Translate.TranslateOption .targetLanguage( targetLocale.getLocaleCode())); options.add(Translate.TranslateOption.format(format)); Translate.TranslateOption[] translateOptions = options .toArray(new Translate.TranslateOption[0]); if (!googleCredential.exists()) { throw new BadRequestException("Google Default Credential file is not setup"); } try { List<Translation> translations = new ArrayList<>(); int batchStart = 0; while (batchStart < contents.size()) { int batchEnd = Math.min(batchStart + BATCH_SIZE, contents.size()); translations.addAll(translate.translate( contents.subList(batchStart, batchEnd), translateOptions)); batchStart = batchEnd; } return translations.stream() .map(translation -> new AugmentedTranslation( translation.getTranslatedText(), dtoUtil.toJSON(translation))).collect( Collectors.toList()); } catch (Exception e) { throw new MTException( "Unable to get translations from Google API", e); } } @Inject  GoogleTranslatorBackend(DTOUtil dtoUtil,
            @Credentials(BackendID.GOOGLE) GoogleCredential googleCredential); @VisibleForTesting protected  GoogleTranslatorBackend(Translate translate,
            DTOUtil dtoUtil, GoogleCredential googleCredential); @Override List<AugmentedTranslation> translate(List<String> contents,
            BackendLocaleCode srcLocale, BackendLocaleCode targetLocale,
            StringType stringType, Optional<String> category); @Override BackendLocaleCode getMappedLocale(LocaleCode localeCode); @Override int getCharLimitPerRequest(); @Override BackendID getId(); }
GoogleTranslatorBackend implements TranslatorBackend { @Override public List<AugmentedTranslation> translate(List<String> contents, BackendLocaleCode srcLocale, BackendLocaleCode targetLocale, StringType stringType, Optional<String> category) throws MTException { String format; switch (stringType) { case HTML: case XML: format = "html"; break; case TEXT_PLAIN: format = "text"; break; default: throw new IllegalArgumentException(stringType.name()); } List<Translate.TranslateOption> options = Lists.newLinkedList(); options.add(Translate.TranslateOption .targetLanguage( targetLocale.getLocaleCode())); options.add(Translate.TranslateOption.format(format)); Translate.TranslateOption[] translateOptions = options .toArray(new Translate.TranslateOption[0]); if (!googleCredential.exists()) { throw new BadRequestException("Google Default Credential file is not setup"); } try { List<Translation> translations = new ArrayList<>(); int batchStart = 0; while (batchStart < contents.size()) { int batchEnd = Math.min(batchStart + BATCH_SIZE, contents.size()); translations.addAll(translate.translate( contents.subList(batchStart, batchEnd), translateOptions)); batchStart = batchEnd; } return translations.stream() .map(translation -> new AugmentedTranslation( translation.getTranslatedText(), dtoUtil.toJSON(translation))).collect( Collectors.toList()); } catch (Exception e) { throw new MTException( "Unable to get translations from Google API", e); } } @Inject  GoogleTranslatorBackend(DTOUtil dtoUtil,
            @Credentials(BackendID.GOOGLE) GoogleCredential googleCredential); @VisibleForTesting protected  GoogleTranslatorBackend(Translate translate,
            DTOUtil dtoUtil, GoogleCredential googleCredential); @Override List<AugmentedTranslation> translate(List<String> contents,
            BackendLocaleCode srcLocale, BackendLocaleCode targetLocale,
            StringType stringType, Optional<String> category); @Override BackendLocaleCode getMappedLocale(LocaleCode localeCode); @Override int getCharLimitPerRequest(); @Override BackendID getId();  }