in src/rosetta-reader.ts [188:238]
public translateSnippet(source: TypeScriptSnippet, targetLang: TargetLanguage): Translation | undefined {
// Look for it in loaded tablets (or previous conversions)
for (const tab of this.allTablets) {
const ret = tab.lookup(source, targetLang);
if (ret !== undefined) {
return this.prefixDisclaimer(ret, this._prefixDisclaimer);
}
}
if (this.unknownSnippets === UnknownSnippetMode.VERBATIM) {
return this.prefixDisclaimer(
{
language: targetLang,
source: source.visibleSource,
},
this._prefixDisclaimer,
);
}
if (this.unknownSnippets === UnknownSnippetMode.FAIL) {
const message = [
'The following snippet was not found in any of the loaded tablets:',
source.visibleSource,
`Location: ${JSON.stringify(source.location)}`,
`Language: ${targetLang}`,
].join('\n');
throw new Error(message);
}
if (this.options.targetLanguages && !this.options.targetLanguages.includes(targetLang)) {
throw new Error(
`Rosetta configured for live conversion to ${this.options.targetLanguages.join(
', ',
)}, but requested ${targetLang}`,
);
}
// See if we can find a fixturized version of this snippet. If so, use that do the live
// conversion.
const extracted = this.extractedSnippets.get(snippetKey(source));
if (extracted !== undefined) {
const snippet = this.translator.translate(extracted, this.options.targetLanguages);
this.liveTablet.addSnippet(snippet);
return this.prefixDisclaimer(snippet.get(targetLang), this._prefixDisclaimer);
}
// Try to live-convert it as-is.
const snippet = this.translator.translate(source, this.options.targetLanguages);
this.liveTablet.addSnippet(snippet);
return this.prefixDisclaimer(snippet.get(targetLang), this._prefixDisclaimer);
}