#I'm getting an error where the
1 messages · Page 1 of 1 (latest)
The token in this sample is only for the Prompt API in Chrome Extension Origin Trial.
For the Translator API, you'll need your own Origin Trial Token from: https://developer.chrome.com/origintrials/#/view_trial/4445615782168100865
Chrome origin trials allow developers to safely experiment with web platform features
Noted, thanks Kenji!
@ruby belfry Even after adding my own trial_token, I still get the same issue
Is it possible to use more than one trial token at the same time? I'm using the other one still to ensure the prompt API is still working
Yes, that's possible. What's the error you're getting?
Getting the same error as before where the translator API is not supported
Can you please confirm that the extension's key matches the one for which you've registered the origin trial?
These are the keys I've used for the sample extension when testing this:
"key": "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvf0O/bR3JULoj6dOpG7sDif4BNVgootUIfSybh2a7jX47BglfZFNH/aRUgDjNtcTBPinXdGbljMVIudQ7w6LiwVq9b1Ht6ZXFVtHTKOsDWtVh/rVKE/AGue9eQ7xCncHFl4zLJUaDRUIRqe5zvjHtaMr8p92I3c/6k43LmTUp1QHz0NooDJRYKRPLS77YVDX8hZc2yopIH5NIY25Ned3wxZ/NWV70GZkYqFRN+UzvMS8bJUEY23L1AMSX7YQjMThY0BCZ/MBLo8UBLs8vN11EphMpLxnBhF2Zwwj2sCPR0jn0ev8HYCtKmGx8nzOl79oK24RFIsW8YWFB2fd28fBLwIDAQAB",
"trial_tokens": ["AtnV2DsKHgyQnmjqSUeZF4t0ahWN3VHNlY7ikMXZkFaEvMwXQaOm/hgrO5BwUzuxwD3LrrcVMgqz8oiDNPbJHgkAAABveyJvcmlnaW4iOiJjaHJvbWUtZXh0ZW5zaW9uOi8vYWhpaWZrb2RnbWlmcGNnbmRja3BwaW1lY25wa3BkbGwiLCJmZWF0dXJlIjoiVHJhbnNsYXRpb25BUEkiLCJleHBpcnkiOjE3NTMxNDI0MDB9",
"Aozzz6KfHYqh8q5x+Khse27nSp8YM7Tftv6XZhNO7lgYcP5uQxxBEpMfRhiFbYJV+yJl1fDNzvtao7FswtZGIgQAAAB4eyJvcmlnaW4iOiJjaHJvbWUtZXh0ZW5zaW9uOi8vYWhpaWZrb2RnbWlmcGNnbmRja3BwaW1lY25wa3BkbGwiLCJmZWF0dXJlIjoiQUlQcm9tcHRBUElGb3JFeHRlbnNpb24iLCJleHBpcnkiOjE3NjA0ODYzOTl9"
],
yup I confirm they match but still no luck
const browserSupportsAPI = (util, apiName) => {
if (util in self && apiName in self.ai) {
return true;
} else {
console.log(
`${apiName} not supported on browser. Unable to run ${apiName} API`
);
return false;
}
};
if (!browserSupportsAPI('translation', 'createTranslator'))
return noTranslation;
It fails at this part ^^
You're checking for the wrong namespace:
const canTranslate = await translation.canTranslate({
Oh my, you are correct, I tried making all my code completely reusable across other APIs and missed a spot, thanks a bunch Sebastian!
With the correction, I still seem to be getting the same issue, is there another step I possibly missed? I confirm I have the same key value and it's enabled in google canary
This is my code:
if ('translation' in self && 'createTranslator' in self.translation) {
console.log('translating...');
const canTranslate = await translation.canTranslate({
sourceLanguage: 'en',
targetLanguage: 'es',
});
console.log('translation status: ', canTranslate);
const translator = await self.translation.createTranslator({
sourceLanguage: 'en',
targetLanguage: 'es',
});
response = await translator.translate('Where is the next bus stop, please?');
}
I have similar code, maybe it's a config issue on my side
Do you run it in a service worker?
yup I do
Also, I just tried copying your code to test it and get a different error so I must have a typo on my side, working on spotting it
that's it. It's not available there (I've been running it in a side panel). Try running it in an offscreen document.
Ah I see! Interesting, I never thought of that, I run all my API calls in a service worker to ensure the extension is persistent
Will give it a shot for running it in an offscreen document
thanks again Sebastian, life saver 🙂
Yeah, that's a bit annoying. The language specific APIs still need to be ported to service workers. We should document this.
Gotcha, I learned something 😎 Going to give it a shot after work today, posting this documentation here so I don't forget haha: https://developer.chrome.com/docs/extensions/reference/api/offscreen
@woven wing To give some more feedback on the documentation: https://developer.chrome.com/docs/ai/translator-api
- It doesn't work with the French language (fr) for me
- Also in this screenshot, there's a typo, I believe it should be translation.ondownloadprogress and not translator.ondownloadprogress
Other than that, I got it working through trial and error, thanks a bunch for your help