Merge pull request #13061 from overleaf/mj-cm6-translations

[web] Make translations accessible to CM6

GitOrigin-RevId: ef617638419597548e6e0545bac3ac94216177de
This commit is contained in:
Mathias Jakobsen 2023-05-15 10:17:48 +01:00 committed by Copybot
parent 31190b967b
commit 5fd4504f51
7 changed files with 52 additions and 15 deletions

View file

@ -769,6 +769,7 @@
"select_tag": "", "select_tag": "",
"select_user": "", "select_user": "",
"selected": "", "selected": "",
"selection_deleted": "",
"send": "", "send": "",
"send_first_message": "", "send_first_message": "",
"sending": "", "sending": "",

View file

@ -141,7 +141,7 @@ function deleteBy(target: CommandTarget, by: (start: number) => number) {
userEvent: event, userEvent: event,
effects: effects:
event == 'delete.selection' event == 'delete.selection'
? EditorView.announce.of(state.phrase('Selection deleted')) ? EditorView.announce.of(state.phrase('selection_deleted'))
: undefined, : undefined,
}) })
) )

View file

@ -5,7 +5,7 @@ export class EndDocumentWidget extends WidgetType {
toDOM(view: EditorView): HTMLElement { toDOM(view: EditorView): HTMLElement {
const element = document.createElement('div') const element = document.createElement('div')
element.classList.add('ol-cm-end-document-widget') element.classList.add('ol-cm-end-document-widget')
element.textContent = view.state.phrase('End of document') element.textContent = view.state.phrase('end_of_document')
element.addEventListener('mouseup', event => { element.addEventListener('mouseup', event => {
event.preventDefault() event.preventDefault()
view.dispatch(placeSelectionInsideBlock(view, event as MouseEvent)) view.dispatch(placeSelectionInsideBlock(view, event as MouseEvent))

View file

@ -26,7 +26,7 @@ export class PreambleWidget extends WidgetType {
helpLink.target = '_blank' helpLink.target = '_blank'
const icon = document.createElement('i') const icon = document.createElement('i')
icon.classList.add('fa', 'fa-question-circle') icon.classList.add('fa', 'fa-question-circle')
icon.title = view.state.phrase('Learn more') icon.title = view.state.phrase('learn_more')
helpLink.appendChild(icon) helpLink.appendChild(icon)
const textNode = document.createElement('span') const textNode = document.createElement('span')
textNode.classList.add('ol-cm-preamble-text') textNode.classList.add('ol-cm-preamble-text')
@ -72,8 +72,8 @@ export class PreambleWidget extends WidgetType {
getToggleText(view: EditorView) { getToggleText(view: EditorView) {
if (this.expanded) { if (this.expanded) {
return view.state.phrase(`Hide document preamble`) return view.state.phrase(`hide_document_preamble`)
} }
return view.state.phrase(`Show document preamble`) return view.state.phrase(`show_document_preamble`)
} }
} }

View file

@ -2,16 +2,51 @@ import { useTranslation } from 'react-i18next'
import { useMemo } from 'react' import { useMemo } from 'react'
export const usePhrases = (): Record<string, string> => { export const usePhrases = (): Record<string, string> => {
const { t } = useTranslation() const { t, i18n } = useTranslation()
return useMemo(() => { const codemirrorBuiltinsOverrides = useMemo(
return { () => ({
'Fold line': t('fold_line'), 'Fold line': t('fold_line'),
'Unfold line': t('unfold_line'), 'Unfold line': t('unfold_line'),
'Learn more': t('learn_more'), }),
'Hide document preamble': t('hide_document_preamble'), [t]
'Show document preamble': t('show_document_preamble'), )
'End of document': t('end_of_document'),
} const translationProxy = useMemo(
}, [t]) () => ({
getOwnPropertyDescriptor(target: Record<string, string>, prop: string) {
// If we've added an override
if (Object.prototype.hasOwnProperty.call(target, prop)) {
return Object.getOwnPropertyDescriptor(target, prop)
}
// If the translation exists, report a property:
// non-enumerable: it won't show up in enumerating the keys of the target
// configurable: we have to report it as configurable since it doesn't
// exist in the base object
// writable: an override can be added
if (i18n.exists(prop)) {
return { enumerable: false, configurable: true, writable: true }
}
return Object.getOwnPropertyDescriptor(target, prop)
},
get(target: Record<string, string>, prop: string) {
// If we've specifically added an override
if (Object.prototype.hasOwnProperty.call(target, prop)) {
return target[prop]
}
if (i18n.exists(prop)) {
return t(prop)
}
return target[prop]
},
}),
[t, i18n]
)
const phrases = useMemo(
() => new Proxy(codemirrorBuiltinsOverrides, translationProxy),
[translationProxy, codemirrorBuiltinsOverrides]
)
return phrases
} }

View file

@ -12,7 +12,7 @@ module.exports = {
options: { options: {
sort: true, sort: true,
func: { func: {
list: ['t'], list: ['t', 'phrase'],
extensions: ['.js', '.jsx'], extensions: ['.js', '.jsx'],
}, },
trans: { trans: {

View file

@ -1319,6 +1319,7 @@
"select_tag": "Select tag __tagName__", "select_tag": "Select tag __tagName__",
"select_user": "Select user", "select_user": "Select user",
"selected": "Selected", "selected": "Selected",
"selection_deleted": "Selection deleted",
"send": "Send", "send": "Send",
"send_first_message": "Send your first message to your collaborators", "send_first_message": "Send your first message to your collaborators",
"send_test_email": "Send a test email", "send_test_email": "Send a test email",