Skip to content

Commit

Permalink
docs: Update transformation result of macros in reference
Browse files Browse the repository at this point in the history
  • Loading branch information
orangain committed Jul 23, 2021
1 parent 67b4147 commit 06e507a
Showing 1 changed file with 45 additions and 39 deletions.
84 changes: 45 additions & 39 deletions docs/ref/macro.rst
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ transformed into ``i18n._`` call.
// ↓ ↓ ↓ ↓ ↓ ↓
import { i18n } from "@lingui/core"
i18n._(/*i18n*/{ id: "Attachment {name} saved", values: { name }})
/*i18n*/
i18n._("Attachment {name} saved", { name })
.. note::

Expand Down Expand Up @@ -111,22 +112,19 @@ Examples of JS macros
+=============================================================+====================================================================+
| .. code-block:: js | .. code-block:: js |
| | |
| t`Refresh inbox` | i18n._(/*i18n*/{ |
| | id: "Refresh inbox" |
| | }) |
| t`Refresh inbox` | /*i18n*/ |
| | i18n._("Refresh inbox") |
+-------------------------------------------------------------+--------------------------------------------------------------------+
| .. code-block:: js | .. code-block:: js |
| | |
| t`Attachment ${name} saved` | i18n._(/*i18n*/{ |
| | id: "Attachment {name} saved", |
| | values: { name } |
| | }) |
| t`Attachment ${name} saved` | /*i18n*/ |
| | i18n._("Attachment {name} saved", { name }) |
+-------------------------------------------------------------+--------------------------------------------------------------------+
| .. code-block:: js | .. code-block:: js |
| | |
| plural(count, { | i18n._(/*i18n*/{ |
| one: "Message", | id: "{count, plural, one {Message} other {Messages}}", |
| other: "Messages" | values: { count } |
| plural(count, { | /*i18n*/ |
| one: "Message", | i18n._("{count, plural, one {Message} other {Messages}}", { |
| other: "Messages" | count |
| }) | }) |
+-------------------------------------------------------------+--------------------------------------------------------------------+
| .. code-block:: js | .. code-block:: js |
Expand Down Expand Up @@ -161,7 +159,7 @@ Examples of JSX macros
| | |
| <Plural | <Trans |
| value={count} | id="{count, plural, one { Message} other {Messages}}" |
| one="Message" | values={{ name }} |
| one="Message" | values={{ count }} |
| other="Messages" | /> |
| /> | |
+-------------------------------------------------------------+--------------------------------------------------------------------+
Expand Down Expand Up @@ -228,9 +226,9 @@ in ICU MessageFormat:
// ↓ ↓ ↓ ↓ ↓ ↓
import { i18n } from "@lingui/core"
const message = i18n._(/*i18n*/{
id: 'Hello World',
})
const message =
/*i18n*/
i18n._("Hello World")
Message variables are supported:

Expand All @@ -242,9 +240,10 @@ Message variables are supported:
// ↓ ↓ ↓ ↓ ↓ ↓
import { i18n } from "@lingui/core"
const message = i18n._(/*i18n*/{
id: 'My name is {name}',
values: { name }
const message =
/*i18n*/
i18n._("My name is {name}", {
name
})
In fact, any expression can be used inside template literal. However, only
Expand Down Expand Up @@ -284,7 +283,8 @@ calling ``t`` macro with a message descriptor:
const message = i18n._(/*i18n*/{
id: 'msg.hello',
comment: 'Greetings at the homepage',
message: 'Hello {name}'
message: 'Hello {name}',
values: { name }
})
In this case the ``message`` is used as a default message and it's transformed
Expand All @@ -303,7 +303,8 @@ as if it were wrapped in ``t`` macro. ``message`` also accepts any other macros:
import { i18n } from "@lingui/core"
const message = i18n._(/*i18n*/{
id: 'msg.plural',
message: '{value, plural, one {...} other {...}}'
message: '{value, plural, one {...} other {...}}',
values: { value }
})
plural
Expand Down Expand Up @@ -332,9 +333,10 @@ used in the source code depends on your source locale (e.g. English has only
// ↓ ↓ ↓ ↓ ↓ ↓
import { i18n } from "@lingui/core"
const message = i18n._(/*i18n*/{
id: '{count, plural, one {# Book} other {# Books}}',
values: { count }
const message =
/*i18n*/
i18n._('{count, plural, one {# Book} other {# Books}}', {
count
})
If you need to add variables to plural form, you can use template string literals.
Expand All @@ -352,9 +354,10 @@ are transformed automatically:
// ↓ ↓ ↓ ↓ ↓ ↓
import { i18n } from "@lingui/core"
const message = i18n._(/*i18n*/{
id: '{count, plural, one {{name} has # friend} other {{name} has # friends}}',
values: { count }
const message =
/*i18n*/
i18n._('{count, plural, one {{name} has # friend} other {{name} has # friends}}', {
count, name
})
Plurals can also be nested to form complex messages. Here's an example using
Expand All @@ -378,19 +381,20 @@ two counters:
// Generated message was wrapped for better readability
import { i18n } from "@lingui/core"
const message = i18n._(/*i18n*/{
id: `{count, plural,
const message =
/*i18n*/
i18n._(`{numBooks, plural,
one {{numArticles, plural,
one {1 book and 1 article}
other {1 book and {numArticles} articles}
}
}}
other {{numArticles, plural,
one {{numBooks} books and 1 article}
other {{numBooks} books and {numArticles} articles
}
other {{numBooks} books and {numArticles} articles}
}}
}`,
values: { numBooks numArticles }
})
{ numBooks, numArticles }
)
.. note::

Expand Down Expand Up @@ -428,9 +432,10 @@ cardinal plural forms it uses ordinal forms:
// ↓ ↓ ↓ ↓ ↓ ↓
import { i18n } from "@lingui/core"
const message = i18n._(/*i18n*/{
id: '{count, selectOrdinal, one {1st} two {2nd} few {3rd} other {#th}}',
values: { count }
const message =
/*i18n*/
i18n._('{count, selectOrdinal, one {1st} two {2nd} few {3rd} other {#th}}', {
count
})
.. important::
Expand Down Expand Up @@ -462,9 +467,10 @@ provided in ``options`` object which key matches exactly ``value``:
// ↓ ↓ ↓ ↓ ↓ ↓
import { i18n } from "@lingui/core"
const message = i18n._(/*i18n*/{
id: '{gender, select, male {he} female {she} other {they}}',
values: { gender }
const message =
/*i18n*/
i18n._('{gender, select, male {he} female {she} other {they}}', {
gender
})
.. important::
Expand Down

0 comments on commit 06e507a

Please sign in to comment.