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
Copy file name to clipboardexpand all lines: docs/pacakges/localization.md
+108-112
Original file line number
Diff line number
Diff line change
@@ -3,161 +3,157 @@ sidebar_position: 2
3
3
title: Localization
4
4
---
5
5
6
-
Languages are not real Components, they are just files that holds translations.
6
+
The Localization Container is built on top of Laravel's [localization](https://laravel.com/docs/localization) features
7
+
and provides a convenient way to create and manage translations for your application.
7
8
8
-
Laravel localization features provide a convenient way to retrieve strings in various languages, allowing you to easily support multiple languages within your application.
9
+
## Requirements
9
10
10
-
## Rules
11
+
`Intl` PHP Extension is required to use this feature.
11
12
12
-
- Languages CAN be placed inside the Containers. However, the default laravel `resources/lang` languages files are still loaded and can be used as well.
13
+
## Installation
13
14
14
-
- All Translations are namespaced as the camelCase of its Section name + `@` + camelCase of its Container name.
15
-
For example, translation key inside a translation file named `messages` inside `MySection` > `MyContainer` can be accessed like this: `__(mySection@myContainer::messages.welcome)`
15
+
```
16
+
composer require apiato/localization-container
17
+
```
18
+
19
+
## Publishing Configs
20
+
```
21
+
php artisan vendor:publish
22
+
```
23
+
Config file will be placed at `app/Ship/Configs/vendor-localization.php`
16
24
17
25
## Folder Structure
18
26
27
+
```markdown
28
+
app
29
+
└── Containers
30
+
└── Section
31
+
└── Container
32
+
└── Languages
33
+
├── en
34
+
│ ├── messages.php
35
+
│ ├── users.php
36
+
│ └── ...
37
+
├── ar
38
+
│ ├── messages.php
39
+
│ ├── users.php
40
+
│ └── ...
41
+
└── ...
19
42
```
20
-
- app
21
-
- Containers
22
-
- {Section}
23
-
- {Container}
24
-
- Resources
25
-
- Languages
26
-
- en
27
-
- messages.php
28
-
- users.php
29
-
- ar
30
-
- messages.php
31
-
- users.php
43
+
44
+
## Usage
45
+
46
+
All Translations are namespaced with the following pattern:
47
+
```shell
48
+
camelCase of its Section name + `@` + camelCase of its Container name
32
49
```
33
50
34
-
## Usage {#usage}
51
+
Let's say we have a `welcome` translation key in the `app/Containers/MySection/MyContainer/en/messages.php` file.
35
52
53
+
You can retrieve this translation like this:
36
54
```php
37
-
__('myS
38
-
ection@myContainer::messages.welcome');
55
+
__('mySection@myContainer::messages.welcome');
39
56
```
40
57
41
-
## Installation{#installation}
42
58
:::note
43
-
Intl PHP Extension is required to use this feature.
59
+
Translation files in **Ship** folder are exception to this and will be namespaced with the word "**ship**"
60
+
instead of section name, e.g. `__('ship::messages.welcome')`
44
61
:::
45
62
46
-
```
47
-
composer require apiato/localization-container
48
-
```
49
-
## Creating new languages files {#create-new-languages-files}
63
+
## Adding New Languages
50
64
51
-
Languages file can be placed in any container, not only the Localization Container.
52
-
Place language files inside the `/Languages` folder of your container. For example
53
-
`app/Containers/Section/Container/Languages`.
65
+
Language files can be placed in any container's `Languages` folder.
54
66
You can also place general language files in `app/Ship/Languages`.
67
+
The language files placed in the default Laravel `lang` folder are still loaded and can be used as well.
55
68
56
-
:::note
57
-
Just create the `Languages` folder if it does not exist in your container or `app/Ship` folder.
58
-
:::
69
+
These language folders work exactly the same way as the default Laravel `lang` folder.
59
70
60
-
You can still use the default Laravel translations by placing language files in `/lang` folder.
71
+
:::note
72
+
Just create the `Languages` folder if it does not exist in your container or the `app/Ship` directory.
73
+
:::
61
74
62
-
## Support new languages{#support-new-languages}
75
+
## Supporting New Languages
63
76
64
77
All supported languages must be added to the `supported_languages` array in the `vendor-localization.php` config file
65
-
to prevent users from requesting unsupported languages. There are 2 ways to do this: Using the configs or by modifying the source code.
78
+
to prevent users from requesting unsupported languages.
66
79
67
80
```php
68
-
'supported_languages' => [
69
-
'ar',
70
-
'en' => [
71
-
'en-GB',
72
-
'en-US',
73
-
],
74
-
'es',
75
-
'fr',
76
-
'fa',
81
+
'supported_languages' => [
82
+
'ar',
83
+
'en' => [
84
+
'en-GB',
85
+
'en-US',
77
86
],
87
+
'es',
88
+
'fr',
89
+
'fa',
90
+
],
78
91
```
79
92
80
-
### Publishing configs
81
-
```
82
-
php artisan vendor:publish
83
-
```
84
-
Config file will be copied to `app/Ship/Configs/vendor-localization.php`
85
-
86
-
### Modifying the source code {#modify-code}
87
-
88
-
1- Copy the container from `Vendor` to `AppSection` (or any of your custom sections) of your project
89
-
2- Fix the namespaces
90
-
3- Remove `apiato/localization-container` dependency from project root composer.json
You can select the language of the response by adding the header `Accept-Language` to the request. By default, the
95
96
`Accept-Language` is set to the language defined in `config/app.php``locale`.
96
97
97
-
Please note that `Accept-Language` only determines, that the client _would like_ to get the information in this specific
98
-
language. It is not given, that the API responds in this language!
99
-
100
-
When the `Accept-Language` header is missing, the default locale will be applied.
98
+
Please note
99
+
that the `Accept-Language` only declares that the client _would like_ to get the information in this specific language.
100
+
It is not a given that the API responds in that language!
101
101
102
102
:::note
103
103
Please be sure that your client does not send an `Accept-Language` header automatically. Some REST clients
104
104
(e.g., `POSTMAN`) automatically add header-fields based on the operating-systems settings. So your `Accept-Language` header
105
105
may be set automatically without you knowing!
106
106
:::
107
107
108
-
The API will answer with the applied language in the `Content-Language`header of the response.
109
-
110
-
If the requested language cannot be resolved (e.g. it is not defined) the API throws an `UnsupportedLanguageException` to tell
111
-
the client about this.
108
+
Now that we have a `Accept-Language`set,
109
+
the API will answer with the applied language in the `Content-Language` header of the response.
110
+
And if the requested language cannot be resolved (e.g., it is not defined),
111
+
an `UnsupportedLanguageException` will be thrown.
112
112
113
113
The overall workflow of the Middleware is as follows:
114
114
1) Extract the `Accept-Language` field from the request header. If none is set, use the default locale from the config file
115
115
2) Build a list of all supported localizations based on the configuration of the respective container. If a language
116
116
(top level) contains regions (sub-level), order them like this: `['en-GB', 'en-US', 'en']` (the regions before languages,
117
117
as regions are more specific)
118
-
3) Check, if the value from 1) is within the list from 2). If the value is within this list, set it as application language,
119
-
if not throw an `Exception`.
120
-
121
-
## Translating Strings{#translating-strings}
122
-
123
-
By default, all the Container translation files are namespaced as the camelCase of its Section name + `@` + camelCase of its Container name.
124
-
125
-
:::note
126
-
Translation files in **Ship** folder are exception to this and will be namespaced with the word "**ship**" instead of section name, e.g. `__('ship::notifications.welcome')`
127
-
:::
128
-
129
-
## Example
130
-
131
-
If a translation file called `notifications` is inside `MySection` > `MyContainer` that contains translation for `welcome`
132
-
like "Welcome to our store :)". You can access this translation as follows `__('mySection@myContainer::notifications.welcome')`. If
133
-
you remove the namespace (part before `::`) and try to access it like this
134
-
`__('notifications.welcome')` it will not find your translation and will print `notifications.welcome` only.
135
-
136
-
:::note
137
-
If you try to load a string for a language that is **not available** (e.g., there is no folder for this language), Apiato
138
-
will stick to the default one that is defined in `app.locale` config file. This is also true, if the requested locale
139
-
is present in the `supported_languages` array from the configuration file.
0 commit comments