Skip to content
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

Feature/add translations #441

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open

Feature/add translations #441

wants to merge 15 commits into from

Conversation

Darginec05
Copy link
Collaborator

@Darginec05 Darginec05 commented Jan 27, 2025

Description

I18n is being developed here in collaboration with @gloaysa

How it works

First of all, it should be noted that a new method editor.getLabelText has appeared in the editor instance.
This method processes all kinds of labels that are available in Yoopta packages (in the core package, in tools, in plugins).

Here are usage examples:
Text labels for BlockOptions in the package @yoopta/editor- source code
In tools - source code
In plugins - (the example will be ready later)

As we can see, when calling editor.getLabelText, we pass string. This string is the path to the object, which we will then pass using the withTranslation extension from the @yoopta/i18n package:

import { I18nYooEditor } from '../types';

type TranslationOptions = {
  language: string;
  defaultLanguage: string;
  translations: I18nYooEditor['translations'];
};

function getNestedValue(obj: any, path: string[]): string | undefined {
  return path.reduce((acc, part) => {
    if (acc && typeof acc === 'object' && part in acc) {
      return acc[part];
    }
    return undefined;
  }, obj);
}

export function withTranslations(editor: I18nYooEditor, options: TranslationOptions): I18nYooEditor {
  const { translations, defaultLanguage, language } = options;

  const { getLabelText } = editor;
  const languages = Object.keys(translations);

  editor.getLabelText = (key) => {
    const keyParts = key.split('.');

    const currentLangValue = getNestedValue(translations[editor.language], keyParts);
    if (typeof currentLangValue === 'string') {
      return currentLangValue;
    }

    const defaultLangValue = getNestedValue(translations[editor.defaultLanguage], keyParts);

    if (typeof defaultLangValue === 'string') {
      return defaultLangValue;
    }

    return getLabelText(key);
  };

  editor.languages = languages;

  editor.setLanguage = (lang: string) => {
    if (translations[lang]) {
      editor.language = lang;
      // editor.emit
    }
  };

  editor.translations = translations;
  editor.defaultLanguage = defaultLanguage;
  editor.language = language;

  return editor;
}

In withTranslation, we extend the capabilities of getLabelText, where we take from the passed translation object for the current language the value from the path that we passed to editor.getLabelText earlier

Copy link

vercel bot commented Jan 27, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
yoopta-editor ✅ Ready (Inspect) Visit Preview 💬 Add feedback Feb 1, 2025 2:12pm

};

// [TODO] - add types
export type LabelKeys = {};
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

add types

@@ -0,0 +1,43 @@
import { RecursiveDotNotation } from '../types/i18n';

export const DEFAULT_LABEL_TEXT_MAP = {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

remove

@@ -0,0 +1,126 @@
# Exports
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

add docs

Copy link
Contributor

Choose a reason for hiding this comment

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

done

packages/core/i18n/rollup.config.js Outdated Show resolved Hide resolved
@@ -0,0 +1 @@
export {};
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

remove

Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of removed, added the Provider

editor.setLanguage = (lang: string) => {
if (translations[lang]) {
editor.language = lang;
// editor.emit
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

add event emit

Copy link
Contributor

Choose a reason for hiding this comment

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

done

@@ -25,6 +25,7 @@ import withShadcnUILibrary from '@/components/examples/withShadcnUILibrary';
import withEditorHistory from '@/components/examples/withEditorHistory';
import withEditorOperations from '@/components/examples/withEditorOperations';
import withEmailBuilder from '@/components/examples/withEmailBuilder';
import withTranslations from '@/components/examples/withTranslations';
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

update examples after publishing patch version

* feat: emit language-change event on setLanguage

* fix: make editor i18n agnostic

* chore: improve dev example and docs

* feat: create I18nYooEditorProvider and use hook in example
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.

2 participants