Skip to content

Commit

Permalink
Translator: Echo empty string
Browse files Browse the repository at this point in the history
If the an empty string is passed to AITranslator.translate, then echo it
back. This does two things:
- Aligns with the spec more since it wants us to echo back any
  non-translatable content.
- Fixes an issue where passing an empty string to translate causes us to
  reject the translate promise.

Fixed: 392057205, 395120677
Change-Id: I05bda0c7b8b75145d9b9fd58183bdaf174e8dd95
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6245038
Commit-Queue: Nathan Memmott <memmott@chromium.org>
Reviewed-by: Ming-Ying Chung <mych@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1421657}
  • Loading branch information
Nathan Memmott authored and chromium-wpt-export-bot committed Feb 18, 2025
1 parent 91f54fa commit e20e41e
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions ai/translator/ai_translator_translate.tentative.https.any.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,34 @@ promise_test(async t => {
assert_equals(progressEvent.total, 1);
}
}, 'AITranslatorFactory.create() monitor option is called correctly.');

promise_test(async t => {
const translator =
await ai.translator.create({sourceLanguage: 'en', targetLanguage: 'ja'});

// Strings containing only white space are not translatable.
const nonTranslatableStrings = ['', ' ', ' ', ' \r\n\t\f'];

// Strings containing only control characters are not translatable.
for (let c = 0; c < 0x1F; c++) {
nonTranslatableStrings.push(String.fromCharCode(c));
}

const translatedNonTranslatableString = await Promise.all(
nonTranslatableStrings.map(str => translator.translate(str)));

// Non translatable strings should be echoed back
assert_array_equals(translatedNonTranslatableString, nonTranslatableStrings);

// Adding translatable text makes it translatable.
const translatableStrings =
nonTranslatableStrings.map(str => `Hello ${str} world`);

const translatedTranslatableString = await Promise.all(
translatableStrings.map(str => translator.translate(str)));

// All the strings should have been translated in some way.
for (let i = 0; i < translatableStrings.length; i++) {
assert_not_equals(translatedTranslatableString[i], translatableStrings[i]);
}
}, 'AITranslator.translate() echos non-translatable content');

0 comments on commit e20e41e

Please sign in to comment.