diff --git a/CHANGELOG.md b/CHANGELOG.md index 6aaa6c95..559c4a0b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ * ConverseJS v10.1.6 (instead of v10.0.0). * New polish translation (Thanks [ewm](https://weblate.framasoft.org/user/ewm/)). +* Links to documentation are now using the front-end language to point to the translated documentation page (except for some links generated from the backend, in the diagnostic tool for example). ## 7.2.2 diff --git a/build-languages.js b/build-languages.js index 73b30992..f98c9906 100644 --- a/build-languages.js +++ b/build-languages.js @@ -121,6 +121,14 @@ class BuildLanguages { console.log('Writing JSON files...') for (const l of this.langs) { const filePath = path.resolve(this.destinationDir, l + '.json') + // Waiting for this to be implemented: + // https://github.com/Chocobozzz/PeerTube/issues/5904 + // (also see https://github.com/JohnXLivingston/peertube-plugin-livechat/issues/224) + // we are adding a special entry with the language key, so we can use frontend + // translation function to get the path for localized documentation. + // See frontend getLangCode and localizedHelpUrl functions. + const content = this.translationsStrings[l] + content['_language'] = l await fs.promises.writeFile(filePath, JSON.stringify(this.translationsStrings[l])) } } diff --git a/client/utils/help.ts b/client/utils/help.ts new file mode 100644 index 00000000..1f0890bb --- /dev/null +++ b/client/utils/help.ts @@ -0,0 +1,37 @@ +import type { RegisterClientOptions } from '@peertube/peertube-types/client' +import { helpUrl } from 'shared/lib/help' + +/** + * Returns the current language code. + * This is the code that correspond to the path for localized documentation + * (in other words: 'fr', not 'fr-FR') + * @param clientOptions client options + * @returns language code + */ +async function getLangCode ({ peertubeHelpers }: RegisterClientOptions): Promise { + // See the build-languages.js script for more information about this trick. + const s = await peertubeHelpers.translate('_language') + if (s === '_language') { + return 'en' + } + return s +} + +/** + * Returns the url of the documentation, in the current user language. + * @param clientOptions client options + */ +async function localizedHelpUrl ( + clientOptions: RegisterClientOptions, + helpUrlOptions: Parameters[0] +): Promise { + const lang = (await getLangCode(clientOptions)).toLowerCase() // lowercase is needed for zh-hans for example + if (lang === 'en') { + return helpUrl(helpUrlOptions) + } + return helpUrl(Object.assign({}, helpUrlOptions, { lang })) +} + +export { + localizedHelpUrl +} diff --git a/client/videowatch-client-plugin.ts b/client/videowatch-client-plugin.ts index 224c1ca5..25e21326 100644 --- a/client/videowatch-client-plugin.ts +++ b/client/videowatch-client-plugin.ts @@ -1,7 +1,7 @@ import type { Video } from '@peertube/peertube-types' import type { RegisterClientOptions } from '@peertube/peertube-types/client' import { videoHasWebchat, videoHasRemoteWebchat } from 'shared/lib/video' -import { helpUrl } from 'shared/lib/help' +import { localizedHelpUrl } from './utils/help' import { logger } from './videowatch/logger' import { closeSVG, openBlankChatSVG, openChatSVG, shareChatUrlSVG, helpButtonSVG } from './videowatch/buttons' import { displayButton, displayButtonOptions } from './videowatch/button' @@ -74,6 +74,9 @@ function register (registerOptions: RegisterClientOptions): void { container: HTMLElement, video: Video, showOpenBlank: boolean, showShareUrlButton: boolean ): Promise { logger.log('Adding livechat in the DOM...') + const viewersDocumentationHelpUrl = await localizedHelpUrl(registerOptions, { + page: 'documentation/user/viewers' + }) const p = new Promise((resolve, reject) => { // eslint-disable-next-line @typescript-eslint/no-floating-promises Promise.all([ @@ -137,9 +140,7 @@ function register (registerOptions: RegisterClientOptions): void { buttonContainer, name: 'help', label: labelHelp, - href: helpUrl({ - page: 'documentation/user/viewers' - }), + href: viewersDocumentationHelpUrl, targetBlank: true, icon: helpButtonSVG, additionalClasses: [] diff --git a/client/videowatch/share.ts b/client/videowatch/share.ts index b858943f..e0db979c 100644 --- a/client/videowatch/share.ts +++ b/client/videowatch/share.ts @@ -4,7 +4,7 @@ import { helpButtonSVG } from './buttons' import { logger } from './logger' import { getIframeUri, getXMPPAddr, UriOptions } from './uri' import { isAutoColorsAvailable } from 'shared/lib/autocolors' -import { helpUrl } from 'shared/lib/help' +import { localizedHelpUrl } from '../utils/help' interface ShareForm { shareString: HTMLInputElement @@ -25,6 +25,10 @@ interface ShareForm { async function shareChatUrl (registerOptions: RegisterClientOptions, settings: any, video: Video): Promise { const peertubeHelpers = registerOptions.peertubeHelpers + const streamersHelpUrl = await localizedHelpUrl(registerOptions, { + page: 'documentation/user/streamers' + }) + const [ labelShare, labelWeb, @@ -92,9 +96,7 @@ async function shareChatUrl (registerOptions: RegisterClientOptions, settings: a divShareString.append(openButton) const helpButton = document.createElement('a') - helpButton.href = helpUrl({ - page: 'documentation/user/streamers' - }) + helpButton.href = streamersHelpUrl helpButton.target = '_blank' helpButton.innerHTML = helpButtonSVG() helpButton.title = labelHelp