Skip to content

Commit

Permalink
fix delimiter problem
Browse files Browse the repository at this point in the history
  • Loading branch information
while1618 committed Aug 29, 2024
1 parent 8820451 commit 953e44a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
8 changes: 6 additions & 2 deletions src/translate/translate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,12 @@ export abstract class Translate {
private cleanUpTranslations = (value: string): string => {
// This is needed because of this weird bug:
// https://github.com/while1618/i18n-auto-translation/issues/12
const translations = replaceAll(value, '#__ #', '#__#');
return replaceAll(translations, '# __#', '#__#');
const patterns: [string, string][] = [
['#__ #', '#__#'],
['# __#', '#__#'],
['# __ #', '#__#'],
];
return replaceAll(value, patterns);
};

protected abstract callTranslateAPI: (valuesForTranslation: string[]) => Promise<string>;
Expand Down
8 changes: 6 additions & 2 deletions src/translate/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,9 @@ export const addCustomCert = (certificatePath: string): https.Agent => {

const escapeRegExp = (string: string): string => string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');

export const replaceAll = (str: string, find: string, replace: string): string =>
str.replace(new RegExp(escapeRegExp(find), 'g'), replace);
export const replaceAll = (str: string, patterns: [string, string][]): string => {
return patterns.reduce(
(acc, [find, replace]) => acc.replace(new RegExp(escapeRegExp(find), 'g'), replace),
str,
);
};

0 comments on commit 953e44a

Please sign in to comment.