Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Provide support for translating plural forms #334

Merged
merged 37 commits into from
Apr 23, 2020
Merged
Changes from 1 commit
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
962f375
WIP - Adding support for passing object as a first argument for `t()`…
ma2ciek Apr 8, 2020
c14c653
Docs: Improved API docs.
ma2ciek Apr 17, 2020
a577cf6
Docs: Improved API docs.
ma2ciek Apr 17, 2020
05bb63a
Renamed `getFormIndex` to `getPluralForm`.
ma2ciek Apr 17, 2020
bf676ba
Changed: The message id used to determine the translation will be gen…
ma2ciek Apr 17, 2020
020072b
Tests: Added tests for the translation service.
ma2ciek Apr 17, 2020
2740664
Fix: Plural form will be set based on English rules if a function tha…
ma2ciek Apr 17, 2020
53f4639
Tests: Added tests for the locale.js.
ma2ciek Apr 17, 2020
7361eab
Merge branch 'master' into i/6526
ma2ciek Apr 17, 2020
0b01d9b
Internal: Fixed code style issues.
ma2ciek Apr 17, 2020
a36cad2
Docs: Improved `Locale` class API docs.
ma2ciek Apr 19, 2020
f116217
Docs: Improved `Locale` class API docs.
ma2ciek Apr 19, 2020
fc1c318
Docs: Improved `Locale` class and translation service API docs.
ma2ciek Apr 19, 2020
7515252
Fix: Simplified 'magic' code.
ma2ciek Apr 19, 2020
e36132d
Tests: Removed usage of deprecated API.
ma2ciek Apr 19, 2020
ea3ae82
Docs: Improved translation service API docs.
ma2ciek Apr 20, 2020
bf15d06
Other: Simplified the `Locale._t()` function.
ma2ciek Apr 20, 2020
e1c569b
Tests: Added missing test for the message context.
ma2ciek Apr 20, 2020
48d69e1
Docs: Improved translation service API docs.
ma2ciek Apr 20, 2020
30c31af
Docs: Improved `Local#t()` API docs.
ma2ciek Apr 20, 2020
aade538
Docs: Improved `Local#t()` API docs.
ma2ciek Apr 20, 2020
d0ddf47
Docs: Improved `Local#t()` API docs.
ma2ciek Apr 20, 2020
2acf51b
Added support for the shorthand `t` call signature: `t( message, valu…
ma2ciek Apr 21, 2020
7f0096d
Docs: Improved API docs for the `Locale#t()` call with a message and …
ma2ciek Apr 21, 2020
38db678
Docs: Fixed API docs issues.
ma2ciek Apr 21, 2020
db4f3c5
Docs: Fixed API docs issues in `translation-service`.
ma2ciek Apr 21, 2020
7797511
Apply suggestions from code review
ma2ciek Apr 21, 2020
33acb99
Other: Reworded amount to quantity in a few places.
ma2ciek Apr 21, 2020
358d8da
Applied suggestions from code review
ma2ciek Apr 21, 2020
11d5b8c
Other: Removed `message.context`, introduced the `message.id`.
ma2ciek Apr 22, 2020
9083b73
Other: Added support for plural rules that returns a boolean value.
ma2ciek Apr 22, 2020
da86205
Docs: Added a simpler scenario.
ma2ciek Apr 22, 2020
bf71980
Docs: Added a simpler scenario.
ma2ciek Apr 22, 2020
9fe6f52
Docs: Fixed API docs issue in `Locale`.
ma2ciek Apr 22, 2020
14d29a6
Docs: Added missing `module:` part.
ma2ciek Apr 22, 2020
cb5cd06
Applied suggestions from code review
ma2ciek Apr 22, 2020
3d8c423
Docs: Added docs for `translation-service#add() function`.
ma2ciek Apr 22, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 21 additions & 13 deletions src/translation-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,32 @@ if ( !window.CKEDITOR_TRANSLATIONS ) {
}

/**
* Adds translations to existing ones or overrides the existing translations.
* Adds translations to existing ones or overrides the existing translations. these translations will later
ma2ciek marked this conversation as resolved.
Show resolved Hide resolved
* be available for the {@link module:utils/locale~Locale#t `t()`} function.
*
* These translations will later be available for the {@link module:utils/locale~Locale#t `t()`} function.
* The `translations` is an object which consists of a `message id - translation` pairs. Note that message id can be
ma2ciek marked this conversation as resolved.
Show resolved Hide resolved
* either constructed either from the message string or from the message string and message context in the following form:
ma2ciek marked this conversation as resolved.
Show resolved Hide resolved
* `<messageString>_<messageContext>` (this happens rarely and mostly for short messages or messages with placeholders).
* Since the editor displays only the message string the message context can be found either in the source code or in the
ma2ciek marked this conversation as resolved.
Show resolved Hide resolved
* built translations for another language.
*
* add( 'pl', {
* 'Cancel': 'Anuluj',
* 'Heading': 'Nagłówek'
* }, n => n == 1 ? 0 : n % 10 >= 2 && n % 10 <= 4 && ( n % 100 < 10 || n % 100 >= 20 ) ? 1 : 2 );
* 'image_Insert image': 'obraz', // Note that the `Insert image` comes from the message context.
* } );
*
* If a message is supposed to support various plural forms, make sure to provide an array with the single form and all plural forms:
* If the message is supposed to support various plural forms, make sure to provide an array with the single form and all plural forms:
ma2ciek marked this conversation as resolved.
Show resolved Hide resolved
*
* add( 'pl', {
* 'Add space': [ 'Dodaj spację', 'Dodaj %0 spacje', 'Dodaj %0 spacji' ]
ma2ciek marked this conversation as resolved.
Show resolved Hide resolved
* } );
*
* You should also specify the third argument (the `getPluralForm` function) that will be used to determine the plural form if the
* language is not supported by CKEditor 5 yet.
* You should also specify the third argument (the `getPluralForm` function) that will be used to determine the plural form if no
* language file was loaded for that language. All language files coming from CKEditor 5 sources will have this option set, so
* these plural form rules will be reused by other translations added to the registered languages.
*
* add( 'pl', {
* 'Add space': [ 'Dodaj spację', 'Dodaj %0 spacje', 'Dodaj %0 spacji' ]
* // Translations.
ma2ciek marked this conversation as resolved.
Show resolved Hide resolved
* }, n => n == 1 ? 0 : n % 10 >= 2 && n % 10 <= 4 && ( n % 100 < 10 || n % 100 >= 20 ) ? 1 : 2 );
ma2ciek marked this conversation as resolved.
Show resolved Hide resolved
*
* If you cannot import this function from this module (e.g. because you use a CKEditor 5 build), then you can
Expand Down Expand Up @@ -99,10 +105,12 @@ export function add( language, translations, getPluralForm ) {
* translate( 'en', { string: 'Add a space', plural: 'Add %0 spaces' }, 1 ); // 'Add a space'
* translate( 'en', { string: 'Add a space', plural: 'Add %0 spaces' }, 3 ); // 'Add %0 spaces'
*
* A Message can provide a context using the `context` property when the message string may be not enough unique
* across all messages. When the `context` property is set the message id will be constructed in
* The message can provide a context using the `context` property when the message ids created from message strings
* are not unique. When the `context` property is set the message id will be constructed in
* the following way: `${ message.string }_${ message.context }`. This context will be also used
* by translators later as a context for the message that should be translated.
* by translators later as an additional context for the translated message.
*
* translate( 'en', { string: 'image', context: 'Add/Remove image' } );
*
* @protected
* @param {String} language Target language.
Expand Down Expand Up @@ -184,8 +192,8 @@ function getNumberOfLanguages() {
*
* @typedef {Object} Message
*
* @property {String} string The message string. It becomes the message id when no context is provided. When the message is supposed
* to support plural forms then the string should be the English singular form of the message.
* @property {String} string The message string to translate. Acts as a default translation if the translation for given language
* is not defined. When the message is supposed to support plural forms then the string should be the English singular form of the message.
* @property {String} [context] The message context. If passed then the message id is constructed form both,
* the message string and the message string in the following format: `<messageString>_<messageContext>`. This property is useful when
* various messages can share the same message string, when omitting a context would result in a broken translation.
Expand Down