Skip to content

Commit 13cb96d

Browse files
committed
a perfect readme
1 parent 99f8d4f commit 13cb96d

1 file changed

Lines changed: 41 additions & 36 deletions

File tree

README.md

Lines changed: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,23 @@
77

88
![banner](https://banners.beyondco.de/Jigsaw%20Localization.png?theme=dark&packageManager=composer+require&packageName=elaborate-code%2Fjigsaw-localization&pattern=jigsaw&style=style_1&description=Brings+localization+feature+to+%22tightenco%2Fjigsaw%22+using+JSON+files&md=1&showWatermark=0&fontSize=100px&images=globe)
99

10-
Brings localization feature to [tightenco/jigsaw](https://jigsaw.tighten.com/) using JSON files.
10+
This package is built on top of [PHP JSON tongue](https://github.com/elaborate-code/php-json-tongue) to bring localization feature to [tightenco/jigsaw](https://jigsaw.tighten.com/) using JSON files.
1111

1212
## Get started
1313

1414
### Requirements
1515

16-
- PHP 8.0 or higher.
16+
- PHP 8.0 or higher.
1717

1818
### Setup
1919

20-
Bring [jigsaw-localization](https://packagist.org/packages/elaborate-code/jigsaw-localization) to your `Jigsaw` project.
20+
Install the package using composer:
2121

2222
```text
2323
composer require elaborate-code/jigsaw-localization
2424
```
2525

26-
Plug `LoadLocalization` to the builder by registering it in `bootstrap.php`.
26+
Plug `LoadLocalization` to the builder by registering it in `bootstrap.php`:
2727

2828
```php
2929
<?php
@@ -40,8 +40,8 @@ $events->beforeBuild([LoadLocalization::class]);
4040
#### Defining Translation Strings
4141

4242
1. Create a `lang` folder in the root of your project.
43-
2. Create subfolders for each language supported by the application.
44-
3. Populate the subfolders with JSON files that hold translations using translation strings as keys (as much as you want).
43+
2. Create subfolders for each language/locale.
44+
3. Populate the subfolders with JSON files that hold translations using the `original text` as a `key`, and the `translation` as a `value`.
4545

4646
File structure example:
4747

@@ -73,6 +73,9 @@ The output:
7373

7474
`two or three lowercase letters` for the language code + **optionally** `a dash (-) with two uppercase letters` for the region code. For example, all the following codes `ar`, `es`, `fr-CA`, `haw-US` are considered valid.
7575

76+
- [ISO 639-1](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes) or [ISO 639-2](https://www.loc.gov/standards/iso639-2/php/English_list.php) for language codes.
77+
- [ISO 3166](https://www.iso.org/obp/ui/#search) for region codes.
78+
7679
## The multi folder
7780

7881
For organizational purpose you can group internationalized translations in one JSON using many `locale` keys.
@@ -81,39 +84,33 @@ For organizational purpose you can group internationalized translations in one J
8184
/lang
8285
...
8386
/multi
84-
salutations.json
87+
greetings.json
8588
projects_short_descriptions.json
8689
...
8790
```
8891

89-
`salutations.json` example:
92+
`greetings.json` example:
9093

9194
```json
9295
{
93-
"fr":{
96+
"fr": {
9497
"Hello": "Salut",
9598
"Goodbye": "Au revoir"
9699
},
97-
"es":{
100+
"es": {
98101
"Hello": "Hola",
99102
"Goodbye": "Adiós"
100103
}
101104
}
102105
```
103106

104-
> First level keys must be lang codes
107+
> First level keys must be locale codes!
105108
106109
## Using folder structure for locale code prefix
107110

108-
This section explains how to dump the `__` helper second argument `current_locale` for a more intuitive approach.
109-
110-
```php
111-
echo __($page, $text);
112-
```
113-
114-
### The default lang
111+
### The default locale
115112

116-
First you need to define `defaultLocale` in `config.php`. If not set, the package with take `en` as a default.
113+
First you need to define `defaultLocale` in `config.php`. If not set, the package will take `en` as a default.
117114

118115
```php
119116
<?php
@@ -127,11 +124,19 @@ return [
127124
];
128125
```
129126

127+
### The translation helper
128+
129+
If you call the `__` helper without providing a `locale` parameter, it will try to resolve it from the page path.
130+
131+
```php
132+
echo __($page, $text);
133+
```
134+
130135
> If you provide the `__` helper with the `locale` parameter it will proceed with it and ignore the folder structure.
131136
132137
### The folder structure
133138

134-
> example.com/{lang}
139+
> domain.com/{locale}/path
135140
136141
Pages that reside in the web root folder `source` are assumed to be rendered using the `defaultLocale`. Other pages that reside in **subfolders named after a locale code** have their **locale** set to the **subfolder name**
137142

@@ -156,12 +161,12 @@ Pages that reside in the web root folder `source` are assumed to be rendered usi
156161

157162
## The included page trick
158163

159-
You may find your self creating a fully coded `source/index.blade.php` and repeating the same code in `source/fr/index.blade.php` and for other languages. To avoid that we suggest the following approach:
164+
You may find your self creating a fully coded `source/index.blade.php` and repeating the same code in `source/fr/index.blade.php` and for other locales. To avoid that we suggest the following approach:
160165

161166
1. Create a `source/_pages` directory which will contain the master pages.
162-
2. A master page will look like any other ordinary page, *it will have the HTML structure and calls to `__` but no hardcoded `$current_locale` value* .For example You may directly copy the content of `source/index.blade.php` to `source/_pages/index.blade.php`.
163-
3. **Include** the master page into other pages that are language aware.
164-
4. The included content will be able to know which **language** to apply on the translation helper `__` calls as a `$current_locale`.
167+
2. A master page will look like any other ordinary page, _it will have the HTML structure and calls to `__` but no hardcoded `$current_locale` value_ .For example You may directly copy the content of `source/index.blade.php` to `source/_pages/index.blade.php`.
168+
3. **Include** the master page into other pages that are locale aware.
169+
4. The included content will be able to know which **locale** to apply on the translation helper `__` calls as a `$current_locale`.
165170

166171
```text
167172
/source
@@ -185,40 +190,40 @@ You may find your self creating a fully coded `source/index.blade.php` and repea
185190

186191
## Helpers
187192

188-
> IMPORTANT: The following helpers require that you respect the locale prefix folder structure!
193+
> IMPORTANT: All the following helpers will try to resolve the locale code from the path if needed!
189194
190-
> Setting `baseUrl` in **config** is essential if your site root URL isn't 'example.com/index.html'
195+
> Setting `baseUrl` in the **config** is essential if your site root URL isn't 'domain.com/index.html'
191196
192197
### current_path_locale
193198

194-
Returns the `current_locale` *deduced from the lang prefix folder structure*.
199+
Returns the **current page locale** deduced from its path.
195200

196201
```php
197-
current_path_locale() // ar | es | fr-CA | haw-US
202+
current_path_locale($page) // ar | es | fr-CA | haw-US
198203
```
199204

200205
Usage example:
201206

202207
```php
203208
<!DOCTYPE html>
204-
<html lang="{{ current_path_locale() }}">
209+
<html lang="{{ current_path_locale($page) }}">
205210
<head>
206211
<!-- ... -->
207212
```
208213

209214
### translate_path
210215

211-
When you have a page that is available in many languages. `translate_path` helps you get the equivalent translated `path`.
216+
When you have a page that is available in many locales. `translate_path` helps you get the equivalent translated `path`.
212217

213218
```php
214-
translate_path($target_locale)
219+
translate_path($page, $target_locale)
215220
```
216221

217222
input/output examples:
218223

219224
| current path | translated path | current_locale to target_locale |
220225
| ------------- | ---------------- | ------------------------------- |
221-
| "" | "/fr" | default -> fr |
226+
| "/" | "/fr" | default -> fr |
222227
| "/contact" | "/fr/contact" | default -> fr |
223228
| "/fr" | "/" | fr -> default |
224229
| "/fr/contact" | "/contact" | fr -> default |
@@ -230,17 +235,17 @@ Usage example:
230235
```php
231236
<nav>
232237
@foreach(['en', 'es', 'fr'] as $locale)
233-
<a href="{{ translate_path($locale) }}"> {{ $locale }} </a>
238+
<a href="{{ translate_path($page, $locale) }}"> {{ $locale }} </a>
234239
@endforeach
235240
</nav>
236241
```
237242

238243
### locale_path
239244

240-
To avoid hard coding the `current_locale` into `paths`, input only the partial path that comes after the `locale code` part into this helper and it will handle the rest for you.
245+
To avoid hard coding the `current_locale` into `paths`, input only the partial path that comes after the `locale code` part into this helper and it will handle the rest for you.
241246

242247
```php
243-
$href = locale_path($partial_path)
248+
$href = locale_path($page, $partial_path)
244249
```
245250

246251
| $partial_path | current_locale | href |
@@ -255,7 +260,7 @@ $href = locale_path($partial_path)
255260
Just like the `locale_path` helper, but it prepends the `baseUrl` if set in the config.
256261

257262
```php
258-
$href = locale_url($partial_path)
263+
$href = locale_url($page, $partial_path)
259264
```
260265

261266
## Live test

0 commit comments

Comments
 (0)