-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Localize Code Block copy feature #4431
Comments
I've managed to translate the "Copied" text, but I still can't overwrite the "Copy Code" label per locale |
What I've tried so far is to add a script in the head of each locale that queries all button.copy elements and overwrites the title attribute to a localized string. But it needs to wait for the window to have finished loading and then it only executes when directly landing on the page with code blocks or refreshing it, but not when navigating to a page with code blocks. |
I also considered the |
config(md) {
const fence = md.renderer.rules.fence!
md.renderer.rules.fence = function (tokens, idx, options, env, self) {
const { localeIndex = 'root' } = env
const codeCopyButtonTitle = (() => {
switch (localeIndex) {
case 'es':
return 'Copiar código'
case 'fa':
return 'کپی کد'
case 'ko':
return '코드 복사'
case 'pt':
return 'Copiar código'
case 'ru':
return 'Скопировать код'
case 'zh':
return '复制代码'
default:
return 'Copy code'
}
})()
return fence(tokens, idx, options, env, self).replace(
'<button title="Copy Code" class="copy"></button>',
`<button title="${codeCopyButtonTitle}" class="copy"></button>`
)
}
} |
This works! Thank you for the help! It would be nice to have this logic built-in, so that I only have to provide the localized string in my config and above logic is handled by VitePress. |
Is your feature request related to a problem? Please describe.
I want to be able to localize the Code Block feature.
Describe the solution you'd like
I've found an option to overwrite the copy button tooltip, but it's not present in the
LocaleConfig<ThemeConfig>
type, so I can't overwrite it per locale. I couldn't find an existing option to overwrite the "Copied" text. I would like to be able to overwrite both strings per locale.Describe alternatives you've considered
No response
Additional context
Existing option to overwrite the copy button tooltip:
vitepress/src/node/markdown/markdown.ts
Lines 113 to 117 in 730fc08
Strings to overwrite:
Validations
The text was updated successfully, but these errors were encountered: