You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
11
11
12
12
## Get started
13
13
14
14
### Requirements
15
15
16
-
- PHP 8.0 or higher.
16
+
-PHP 8.0 or higher.
17
17
18
18
### Setup
19
19
20
-
Bring [jigsaw-localization](https://packagist.org/packages/elaborate-code/jigsaw-localization) to your `Jigsaw` project.
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`.
45
45
46
46
File structure example:
47
47
@@ -73,6 +73,9 @@ The output:
73
73
74
74
`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.
75
75
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
+
76
79
## The multi folder
77
80
78
81
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
81
84
/lang
82
85
...
83
86
/multi
84
-
salutations.json
87
+
greetings.json
85
88
projects_short_descriptions.json
86
89
...
87
90
```
88
91
89
-
`salutations.json` example:
92
+
`greetings.json` example:
90
93
91
94
```json
92
95
{
93
-
"fr":{
96
+
"fr":{
94
97
"Hello": "Salut",
95
98
"Goodbye": "Au revoir"
96
99
},
97
-
"es":{
100
+
"es":{
98
101
"Hello": "Hola",
99
102
"Goodbye": "Adiós"
100
103
}
101
104
}
102
105
```
103
106
104
-
> First level keys must be lang codes
107
+
> First level keys must be locale codes!
105
108
106
109
## Using folder structure for locale code prefix
107
110
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
115
112
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.
117
114
118
115
```php
119
116
<?php
@@ -127,11 +124,19 @@ return [
127
124
];
128
125
```
129
126
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
+
130
135
> If you provide the `__` helper with the `locale` parameter it will proceed with it and ignore the folder structure.
131
136
132
137
### The folder structure
133
138
134
-
> example.com/{lang}
139
+
> domain.com/{locale}/path
135
140
136
141
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**
137
142
@@ -156,12 +161,12 @@ Pages that reside in the web root folder `source` are assumed to be rendered usi
156
161
157
162
## The included page trick
158
163
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:
160
165
161
166
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`.
165
170
166
171
```text
167
172
/source
@@ -185,40 +190,40 @@ You may find your self creating a fully coded `source/index.blade.php` and repea
185
190
186
191
## Helpers
187
192
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!
189
194
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'
191
196
192
197
### current_path_locale
193
198
194
-
Returns the `current_locale`*deduced from the lang prefix folder structure*.
199
+
Returns the **current page locale**deduced from its path.
195
200
196
201
```php
197
-
current_path_locale() // ar | es | fr-CA | haw-US
202
+
current_path_locale($page) // ar | es | fr-CA | haw-US
198
203
```
199
204
200
205
Usage example:
201
206
202
207
```php
203
208
<!DOCTYPE html>
204
-
<htmllang="{{ current_path_locale() }}">
209
+
<htmllang="{{ current_path_locale($page) }}">
205
210
<head>
206
211
<!-- ... -->
207
212
```
208
213
209
214
### translate_path
210
215
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`.
212
217
213
218
```php
214
-
translate_path($target_locale)
219
+
translate_path($page, $target_locale)
215
220
```
216
221
217
222
input/output examples:
218
223
219
224
| current path | translated path | current_locale to target_locale |
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.
0 commit comments