Skip to content

feat: add i18n translation#565

Open
strictKraken wants to merge 2 commits into
josdejong:developfrom
strictKraken:feat-564-add-support-i18n
Open

feat: add i18n translation#565
strictKraken wants to merge 2 commits into
josdejong:developfrom
strictKraken:feat-564-add-support-i18n

Conversation

@strictKraken

Copy link
Copy Markdown

#564
Apologies for the formatting, I couldn't find a contribution guide.
I wasn't sure if it's okay to use third-party libraries for translation, so I added a simple translation implementation.

I don't know Chinese very well, but AI-generated translations could be added if someone can review them.

@josdejong

Copy link
Copy Markdown
Owner

Awesome Stephan, thanks! I'll review your PR next week.

@steve02081504

Copy link
Copy Markdown

maybe add an function that allows to set i18n data?
like SetI18nData({xxx:xxx})

@strictKraken

Copy link
Copy Markdown
Author

maybe add an function that allows to set i18n data? like SetI18nData({xxx:xxx})

Great idea, i will add it

@josdejong josdejong left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First: awesome job Stephan, this is quite an undertaking. It looks solid and well taken care of. Thanks!

I made quite some inline comments. Most are small, some require some thinking though. Can you have a look at those please?

{value.length}
{value.length === 1 ? 'item' : 'items'}
</Tag>
{value.length === 1 ? t('item') : t('items')}

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this line should stay inside <Tag>...</Tag>, like it was?

Comment thread src/lib/i18n/locales.ts Outdated
}
}

export const russia: Language = {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think russia should be russian

Comment thread src/lib/i18n/types.ts Outdated

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you remove this empty file i18n/types.ts?

{
type: 'button',
icon: faUndo,
title: 'Undo (Ctrl+Z)',

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you translate all items of the TableMenu?

text: 'Cut',
title: 'Cut selected contents, formatted with indentation (Ctrl+X)',
text: t('cut'),
title: t('cutSelectedContentFormattedWithIndentation') + ' (Ctrl+X)',

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you give all title translations in the createTableContextMenuItems a shorter name, like:

  • cutSelectedContentFormattedWithIndentation -> cutFormattedTitle
  • cutSelectedContentWithoutIndent-> cutCompactedTitle
  • pastClipboardContent -> pasteTitle
  • etc

Comment thread src/lib/i18n/index.ts Outdated

if (params) {
for (const param in params) {
translation = translation.replace(new RegExp(`{{${param}}}`, 'g'), params[param])

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use a replaceAll here? In case we use the same param twice.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@josdejong
replaceAll was introduced only in es2021. Currently, the lib uses es2020, and TypeScript complains about it. What's the best way to fix this behavior? Can I change compileOptions?

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

O that is a good point! I think it's best to write a little replaceAll function ourselves instead of using the built-in version.

Comment thread src/lib/i18n/index.ts Outdated
@@ -0,0 +1,42 @@
import { getContext, hasContext, setContext } from 'svelte'

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you run the tests and update the snapshots that are failing right now?

(this comment is not related to this piece of code)

Comment thread src/lib/i18n/index.ts Outdated
@@ -0,0 +1,42 @@
import { getContext, hasContext, setContext } from 'svelte'
import type { Language, TranslationKey } from '$lib/types'

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you write documentation in the README.md about the new language feature?

(this comment is not related to this piece of code)

Comment thread src/lib/i18n/index.ts Outdated

export function t(key: keyof TranslationKey, params?: Record<string, string>): string {
const ctx = getI18nContext()
let translation: string = get(ctx.values)?.[key] ?? key

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be good to add an extra fallback to the default language, English. When we add new language keys for new features, I think it can easily happen that languages are a step behind, so it may be good to cater for that.

So then it can become:

let translation: string = get(ctx.values)?.[key] ?? defaultLanguage?.[key] ?? key

What do you think?

Comment thread src/lib/i18n/index.ts Outdated
}

export function getI18nContext() {
return getContext<I18nContext>(I18N_CONTEXT_KEY)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When running in a develop environment, I get errors like:

Uncaught Svelte error: lifecycle_outside_component `getContext(...)` can only be used during component initialisation

Did you encounter that too?

@josdejong

Copy link
Copy Markdown
Owner

@strictKraken did you see my feedback from last week?

@strictKraken

strictKraken commented Nov 19, 2025

Copy link
Copy Markdown
Author

@strictKraken did you see my feedback from last week?

@josdejong

Thanks for the detailed review! I was a bit busy last week, so I'll take a closer look at the review and make some fixes this week !!

@josdejong

Copy link
Copy Markdown
Owner

Thanks! No problem at all, no need to rush or anything.

@fevrax

fevrax commented Nov 23, 2025

Copy link
Copy Markdown

This PR is excellent, keep it up.

@josdejong

Copy link
Copy Markdown
Owner

@strictKraken any chance of picking this PR up again?

@Estheisia

Copy link
Copy Markdown

@strictKraken Are you still planning to work on this ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants