Skip to content

Commit 9a100aa

Browse files
committed
docs: update README with examples
1 parent b92397c commit 9a100aa

1 file changed

Lines changed: 118 additions & 1 deletion

File tree

README.md

Lines changed: 118 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,124 @@ composer require skillshare/formatphp
4949

5050
## Usage
5151

52-
TBA
52+
The following example shows a complete working implementation of FormatPHP.
53+
54+
```php
55+
use FormatPHP\Config;
56+
use FormatPHP\FormatPHP;
57+
use FormatPHP\Intl;
58+
use FormatPHP\Message;
59+
use FormatPHP\MessageCollection;
60+
61+
// Translated messages in Spanish with matching IDs to what you declared.
62+
$messagesInSpanish = new MessageCollection([
63+
new Message('hello', '¡Hola {name}! Hoy es {ts, date, ::yyyyMMdd}.'),
64+
]);
65+
66+
$config = new Config(
67+
// Locale of the application (or of the user using the application).
68+
locale: new Intl\Locale('es'),
69+
);
70+
71+
$formatphp = new FormatPHP(
72+
config: $config,
73+
messages: $messagesInSpanish,
74+
);
75+
76+
echo $formatphp->formatMessage([
77+
'id' => 'hello',
78+
'defaultMessage' => 'Hello, {name}! Today is {ts, date, ::yyyyMMdd}.',
79+
], [
80+
'name' => 'Arwen',
81+
'ts' => time(),
82+
]);
83+
```
84+
85+
### Using MessageLoader to Load Messages
86+
87+
We also provide a message loader to load translation strings from locale files
88+
that have been generated by your translation management system.
89+
90+
```php
91+
use FormatPHP\MessageLoader;
92+
93+
$messageLoader = new MessageLoader(
94+
// The path to your locale JSON files (i.e., en.json, es.json, etc.).
95+
messagesDirectory: '/path/to/app/locales',
96+
// The configuration object created earlier.
97+
config: $config,
98+
);
99+
100+
$messagesInSpanish = $messageLoader->loadMessages();
101+
```
102+
103+
This example assumes a directory of locale JSON files located at
104+
`/path/to/app/locales`, which includes an `en.json` file with these
105+
contents:
106+
107+
```json
108+
{
109+
"hello": {
110+
"defaultMessage": "Hello, {name}! Today is {ts, date, ::yyyyMMdd}."
111+
}
112+
}
113+
```
114+
115+
and an `es.json` file with these contents:
116+
117+
```json
118+
{
119+
"hello": {
120+
"defaultMessage": "¡Hola {name}! Hoy es {ts, date, ::yyyyMMdd}."
121+
}
122+
}
123+
```
124+
125+
The message loader uses a fallback method to choose locales. In this example,
126+
if the user's locale is `es-419` (i.e., Spanish appropriate for the Latin
127+
America and Caribbean region), the fallback method will choose `es.json` for the
128+
messages.
129+
130+
### Using the Console Command To Extract Messages
131+
132+
The `formatphp extract` console command helps you extract messages from your
133+
application source code, saving them to JSON files that your translation
134+
management system can use.
135+
136+
```shell
137+
./vendor/bin/formatphp extract --out-file=locales/en.json 'src/**/*.php' 'src/**/*.phtml'
138+
```
139+
140+
In order for message extraction to function properly, we have a few rules that
141+
must be followed (see comments inline in the following example):
142+
143+
```php
144+
// The method name must be exactly `formatMessage`. (see note below)
145+
// The name of the variable does not matter.
146+
$formatphp->formatMessage(
147+
// The message descriptor should be an array literal.
148+
[
149+
'id' => 'hello', // ID (if provided) should be a string literal.
150+
'description' => 'Message to translators', // Description should be a string literal.
151+
'defaultMessage' => 'My name is {name}', // Message should be a string literal.
152+
],
153+
[
154+
'name' => $userName,
155+
],
156+
);
157+
```
158+
159+
At least one of `id` or `defaultMessage` must be present.
160+
161+
> ℹ️ If you wish to use a different function name (e.g., maybe you wish to wrap
162+
> this method call in a Closure, etc.), you may do so, but you must provide the
163+
> `--additional-function-names` option to the `formatphp extract` console
164+
> command. This option takes a comma-separated list of function names for the
165+
> extractor to parse.
166+
>
167+
> ```
168+
> --additional-function-names='formatMessage, myCustomFormattingFunction'
169+
> ```
53170
54171
## Contributing
55172

0 commit comments

Comments
 (0)