front / gutenberg-js

JavaScript only version of the WordPress Gutenberg editor (DEPRECATED)
GNU General Public License v2.0
390 stars 42 forks source link

Replacing wordpress i18n package? #10

Closed vonloxx closed 5 years ago

vonloxx commented 5 years ago

I'm wondering if we could replace wordpress i18n package for something custom. I'm thinking about the Drupal implementation: on browser side, Drupal uses a javascript method Drupal.t() for translations. It would be nice if we could override wp's __() translation function before the editor is initialized.

Qoraiche commented 5 years ago

Hi @vonloxx, I'm sorry that I don't have an answer to your question. but, can I ask you how you implement WordPress i18n package in gutenberg-js?, I want to change the UI to another language.

Yassine Qr, thanks.

SofiaSousa commented 5 years ago

@Qoraiche, I think this explanation by @eliocro might help you:

Gutenberg does not use .po files directly. Instead translations are loaded from json (in JED format). The i18n package provides a method to load those translations: setLocaleData()

So there is no need to override, just set the translations (before the editor is initialised but after your globals).

import { i18n } from '@frontkom/gutenberg-js'; import translations from './translations';

const { setLocaleData, __, _n, sprintf } = i18n; setLocaleData(translations);

console.log(('User')); console.log(('Action'));

const num = 3; console.log(sprintf(_n('%d result', '%d results', num), num));

The format of the JSON (./translations) file is:

{ "": { "lang": "pt", "plural_forms": "nplurals=2; plural=(n != 1)" }, "User": [ "Utilizador" ], "Action": [ "Ação" ], "%d result": [ "Um resultado", "%d resultados" ] }

These objects have to generated from whatever system the backend uses. If it's .po files (as in WP) there are several tools to convert them.

Some references: http://messageformat.github.io/Jed/ https://www.npmjs.com/package/tannin https://github.com/WordPress/gutenberg/tree/master/packages/i18n

Qoraiche commented 5 years ago

thanks, @SofiaSousa, I will try this soon!

QiShaoXuan commented 5 years ago

@SofiaSousa @Qoraiche Is there any demo to view the detail code ? I really didn't try any thing about it. Thank you very much

SofiaSousa commented 5 years ago

Hi @QiShaoXuan,

I think we don't have it yet. I've created https://github.com/front/g-editor/issues/5 suggesting the translations implementation in g-editor.

QiShaoXuan commented 5 years ago

Hi @QiShaoXuan,

I think we don't have it yet. I've created front/g-editor#5 suggesting the translations implementation in g-editor.

it is so sad but thank you , and one more question, do you have any thinking to solve it?

SofiaSousa commented 5 years ago

do you have any thinking to solve it?

Sorry, I didn't understand, to solve what?

KuzNet commented 1 year ago

import { i18n } from '@frontkom/gutenberg-js';

import { i18n } from '@frontkom/gutenberg-js'; import translations from './translations';

const { setLocaleData, __, _n, sprintf } = i18n; setLocaleData(translations);

console.log(('User')); console.log(('Action'));

const num = 3; console.log(sprintf(_n('%d result', '%d results', num), num));

@eliocro