mirror of
https://github.com/laurent22/joplin.git
synced 2024-12-24 10:27:10 +02:00
ts
This commit is contained in:
parent
9bb467a340
commit
7b5224b46c
@ -7,7 +7,7 @@ describe('applyTranslations', () => {
|
|||||||
{
|
{
|
||||||
html: '<div><span translate>Translate me</span></div>',
|
html: '<div><span translate>Translate me</span></div>',
|
||||||
translations: {
|
translations: {
|
||||||
'Translate me': 'Traduis moi',
|
'Translate me': ['Traduis moi'],
|
||||||
},
|
},
|
||||||
htmlTranslated: '<div>\n<span translate>\nTraduis moi\n</span>\n</div>',
|
htmlTranslated: '<div>\n<span translate>\nTraduis moi\n</span>\n</div>',
|
||||||
},
|
},
|
||||||
@ -19,14 +19,14 @@ describe('applyTranslations', () => {
|
|||||||
{
|
{
|
||||||
html: '<h1 translate class="text-center">\nFree your <span class="frame-bg frame-bg-blue">notes</span>\n</h1>',
|
html: '<h1 translate class="text-center">\nFree your <span class="frame-bg frame-bg-blue">notes</span>\n</h1>',
|
||||||
translations: {
|
translations: {
|
||||||
'Free your <span class="frame-bg frame-bg-blue">notes</span>': 'Libérez vos <span class="frame-bg frame-bg-blue">notes</span>',
|
'Free your <span class="frame-bg frame-bg-blue">notes</span>': ['Libérez vos <span class="frame-bg frame-bg-blue">notes</span>'],
|
||||||
},
|
},
|
||||||
htmlTranslated: '<h1 translate class="text-center">\nLibérez vos <span class="frame-bg frame-bg-blue">notes</span>\n</h1>',
|
htmlTranslated: '<h1 translate class="text-center">\nLibérez vos <span class="frame-bg frame-bg-blue">notes</span>\n</h1>',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
html: '<div translate>Save <span class="frame-bg frame-bg-blue">web pages</span> <br />as notes</div>',
|
html: '<div translate>Save <span class="frame-bg frame-bg-blue">web pages</span> <br />as notes</div>',
|
||||||
translations: {
|
translations: {
|
||||||
'Save <span class="frame-bg frame-bg-blue">web pages</span> <br>as notes': 'Sauvegardez vos <span class="frame-bg frame-bg-blue">pages web</span> <br>en notes',
|
'Save <span class="frame-bg frame-bg-blue">web pages</span> <br>as notes': ['Sauvegardez vos <span class="frame-bg frame-bg-blue">pages web</span> <br>en notes'],
|
||||||
},
|
},
|
||||||
htmlTranslated: '<div translate>\nSauvegardez vos <span class="frame-bg frame-bg-blue">pages web</span> <br>en notes\n</div>',
|
htmlTranslated: '<div translate>\nSauvegardez vos <span class="frame-bg frame-bg-blue">pages web</span> <br>en notes\n</div>',
|
||||||
},
|
},
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import { unique } from '@joplin/lib/ArrayUtils';
|
import { unique } from '@joplin/lib/ArrayUtils';
|
||||||
import { attributesHtml, isSelfClosingTag } from '@joplin/renderer/htmlUtils';
|
import { attributesHtml, isSelfClosingTag } from '@joplin/renderer/htmlUtils';
|
||||||
|
import { Translations } from '../../utils/translation';
|
||||||
const Entities = require('html-entities').AllHtmlEntities;
|
const Entities = require('html-entities').AllHtmlEntities;
|
||||||
const htmlentities = new Entities().encode;
|
const htmlentities = new Entities().encode;
|
||||||
const htmlparser2 = require('@joplin/fork-htmlparser2');
|
const htmlparser2 = require('@joplin/fork-htmlparser2');
|
||||||
@ -15,7 +16,7 @@ const trimHtml = (content: string) => {
|
|||||||
.replace(/\t+$/, '');
|
.replace(/\t+$/, '');
|
||||||
};
|
};
|
||||||
|
|
||||||
const findTranslation = (englishString: string, translations: Record<string, string>): string => {
|
const findTranslation = (englishString: string, translations: Translations): string => {
|
||||||
const stringsToTry = unique([
|
const stringsToTry = unique([
|
||||||
englishString,
|
englishString,
|
||||||
englishString.replace(/<br\/>/gi, '<br>'),
|
englishString.replace(/<br\/>/gi, '<br>'),
|
||||||
@ -26,7 +27,8 @@ const findTranslation = (englishString: string, translations: Record<string, str
|
|||||||
]) as string[];
|
]) as string[];
|
||||||
|
|
||||||
for (const stringToTry of stringsToTry) {
|
for (const stringToTry of stringsToTry) {
|
||||||
if (translations[stringToTry]) return translations[stringToTry];
|
// Note that we don't currently support plural forms for the website
|
||||||
|
if (translations[stringToTry] && translations[stringToTry].length) return translations[stringToTry][0];
|
||||||
}
|
}
|
||||||
|
|
||||||
return englishString;
|
return englishString;
|
||||||
@ -38,7 +40,7 @@ const encodeHtml = (decodedText: string): string => {
|
|||||||
.replace(/{{> /gi, '{{> '); // Don't break Mustache partials
|
.replace(/{{> /gi, '{{> '); // Don't break Mustache partials
|
||||||
};
|
};
|
||||||
|
|
||||||
export default (html: string, _languageCode: string, translations: Record<string, string>) => {
|
export default (html: string, _languageCode: string, translations: Translations) => {
|
||||||
const output: string[] = [];
|
const output: string[] = [];
|
||||||
|
|
||||||
interface State {
|
interface State {
|
||||||
|
@ -1,8 +1,9 @@
|
|||||||
import { mkdirp, readFile, writeFile } from 'fs-extra';
|
import { mkdirp, readFile, writeFile } from 'fs-extra';
|
||||||
import { dirname } from 'path';
|
import { dirname } from 'path';
|
||||||
|
import { Translations } from '../../utils/translation';
|
||||||
import applyTranslations from './applyTranslations';
|
import applyTranslations from './applyTranslations';
|
||||||
|
|
||||||
export default async (englishFilePath: string, translatedFilePath: string, languageCode: string, translations: Record<string, string>) => {
|
export default async (englishFilePath: string, translatedFilePath: string, languageCode: string, translations: Translations) => {
|
||||||
let content = await readFile(englishFilePath, 'utf8');
|
let content = await readFile(englishFilePath, 'utf8');
|
||||||
content = content.replace('<html lang="en-gb">', `<html lang="${languageCode}">`);
|
content = content.replace('<html lang="en-gb">', `<html lang="${languageCode}">`);
|
||||||
const translatedContent = await applyTranslations(content, languageCode, translations);
|
const translatedContent = await applyTranslations(content, languageCode, translations);
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import { Plan, StripePublicConfig } from '@joplin/lib/utils/joplinCloud';
|
import { Plan, StripePublicConfig } from '@joplin/lib/utils/joplinCloud';
|
||||||
import { Sponsors } from '../../utils/loadSponsors';
|
import { Sponsors } from '../../utils/loadSponsors';
|
||||||
|
import { Translations } from '../../utils/translation';
|
||||||
import { OpenGraphTags } from './openGraph';
|
import { OpenGraphTags } from './openGraph';
|
||||||
|
|
||||||
export enum Env {
|
export enum Env {
|
||||||
@ -8,7 +9,7 @@ export enum Env {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export interface Locale {
|
export interface Locale {
|
||||||
htmlTranslations: Record<string, string>;
|
htmlTranslations: Translations;
|
||||||
lang: string;
|
lang: string;
|
||||||
pathPrefix: string;
|
pathPrefix: string;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user