diff --git a/apps/readest-app/src/__tests__/utils/ssml.test.ts b/apps/readest-app/src/__tests__/utils/ssml.test.ts
index ead8f54c4..f3ea11d54 100644
--- a/apps/readest-app/src/__tests__/utils/ssml.test.ts
+++ b/apps/readest-app/src/__tests__/utils/ssml.test.ts
@@ -105,6 +105,30 @@ describe('a stray ideograph in an English paragraph', () => {
const { marks } = parseSSMLMarks(ssml, 'en');
expect(marks.map((mark) => mark.language)).toEqual(['en', 'en', 'en']);
});
+
+ // A tag splits a sentence into fragments; one holding only the ideograph
+ // must not take a voice of its own.
+ it('should keep a fragment holding only the ideograph in English', () => {
+ const { marks } = parseSSMLMarks(
+ ssmlWithLang('en', 'He paused一and went on.'),
+ 'en',
+ );
+ expect(marks.map((mark) => mark.language)).toEqual(['en', 'en', 'en']);
+ });
+
+ it('should keep a word split by an inline tag whole', () => {
+ expect(
+ parseSSMLLang(ssmlWithLang('en', '你好 iPhone')),
+ ).toBe('zh');
+ });
+
+ it('should keep an explicit language inside the sentence', () => {
+ const { marks } = parseSSMLMarks(
+ ssmlWithLang('en', 'She said bonjour and left.'),
+ 'en',
+ );
+ expect(marks.map((mark) => mark.language)).toEqual(['en', 'fr', 'en']);
+ });
});
describe('parseSSMLMarks', () => {
diff --git a/apps/readest-app/src/utils/ssml.ts b/apps/readest-app/src/utils/ssml.ts
index 734f2cb9c..f867a217d 100644
--- a/apps/readest-app/src/utils/ssml.ts
+++ b/apps/readest-app/src/utils/ssml.ts
@@ -85,7 +85,7 @@ export const parseSSMLLang = (ssml: string, primaryLang?: string): string => {
if (isDefaultEnglish && primaryLang && !isSameLang(lang, primaryLang)) {
lang = primaryLang.split('-')[0]!.toLowerCase();
}
- const text = removeLanguageBlocks(ssml, () => false).replace(/<[^>]*>/g, ' ');
+ const text = removeLanguageBlocks(ssml, () => false).replace(/<[^>]*>/g, '');
return inferLangFromScript(text, lang);
};
@@ -121,12 +121,7 @@ export const parseSSMLMarks = (ssml: string, primaryLang?: string) => {
if (text && activeMark && isValidMark(text)) {
const offset = plainText.length;
plainText += text;
- marks.push({
- offset,
- name: activeMark,
- text,
- language: inferLangFromScript(text, currentLang) || currentLang,
- });
+ marks.push({ offset, name: activeMark, text, language: currentLang });
} else {
plainText += cleanTextContent(rawText);
}
@@ -154,6 +149,18 @@ export const parseSSMLMarks = (ssml: string, primaryLang?: string) => {
}
}
+ // A tag cuts a sentence into fragments. The language comes from all of the
+ // sentence's text in one language scope, so a fragment holding only a stray
+ // ideograph (between two s, say) doesn't get a voice of its own.
+ const scopes = marks.map((mark) => `${mark.name}\u0000${mark.language}`);
+ const scopeText = new Map();
+ marks.forEach((mark, i) =>
+ scopeText.set(scopes[i]!, (scopeText.get(scopes[i]!) ?? '') + mark.text),
+ );
+ marks.forEach((mark, i) => {
+ mark.language = inferLangFromScript(scopeText.get(scopes[i]!)!, mark.language) || mark.language;
+ });
+
return { plainText, marks };
};