Skip to content

Commit f02c04a

Browse files
authored
Merge pull request #7210 from kenjis/fix-docs-localization.rst
docs: improve localization.rst
2 parents f8cc7e6 + aa8fc48 commit f02c04a

5 files changed

Lines changed: 38 additions & 25 deletions

File tree

user_guide_src/source/installation/upgrade_4xx.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ Downloads
4141
Namespaces
4242
==========
4343

44-
- CI4 is built for PHP 7.4+, and everything in the framework is namespaced, except for the helpers.
44+
- CI4 is built for PHP 7.4+, and everything in the framework is namespaced,
45+
except for the helper and lang files.
4546

4647
Application Structure
4748
=====================

user_guide_src/source/outgoing/localization.rst

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ with different supported languages.
1717
Language strings are stored in the **app/Language** directory, with a sub-directory for each
1818
supported language::
1919

20-
/app
21-
/Language
22-
/en
20+
app/
21+
Language/
22+
en/
2323
App.php
24-
/fr
24+
fr/
2525
App.php
2626

2727
.. important:: Locale detection only works for web-based requests that use the IncomingRequest class.
@@ -30,7 +30,7 @@ supported language::
3030
Configuring the Locale
3131
======================
3232

33-
Every site will have a default language/locale they operate in. This can be set in **Config/App.php**:
33+
Every site will have a default language/locale they operate in. This can be set in **app/Config/App.php**:
3434

3535
.. literalinclude:: localization/001.php
3636

@@ -40,9 +40,9 @@ language codes like en-US for American English, or fr-FR, for French/France. A m
4040
to this can be found on the `W3C's site <https://www.w3.org/International/articles/language-tags/>`_.
4141

4242
The system is smart enough to fall back to more generic language codes if an exact match
43-
cannot be found. If the locale code was set to **en-US** and we only have language files set up for **en**
44-
then those will be used since nothing exists for the more specific **en-US**. If, however, a language
45-
directory existed at **app/Language/en-US** then that would be used first.
43+
cannot be found. If the locale code was set to ``en-US`` and we only have language files set up for ``en``
44+
then those will be used since nothing exists for the more specific ``en-US``. If, however, a language
45+
directory existed at the **app/Language/en-US** directory then that would be used first.
4646

4747
Locale Detection
4848
================
@@ -57,15 +57,15 @@ Should you ever need to set the locale directly you may use ``IncomingRequest::s
5757
Content Negotiation
5858
-------------------
5959

60-
You can set up content negotiation to happen automatically by setting two additional settings in Config/App.
60+
You can set up content negotiation to happen automatically by setting two additional settings in **app/Config/App.php**.
6161
The first value tells the Request class that we do want to negotiate a locale, so simply set it to true:
6262

6363
.. literalinclude:: localization/002.php
6464

6565
Once this is enabled, the system will automatically negotiate the correct language based upon an array
6666
of locales that you have defined in ``$supportLocales``. If no match is found between the languages
6767
that you support, and the requested language, the first item in $supportedLocales will be used. In
68-
the following example, the **en** locale would be used if no match is found:
68+
the following example, the ``en`` locale would be used if no match is found:
6969

7070
.. literalinclude:: localization/003.php
7171

@@ -80,7 +80,7 @@ segment will be your locale:
8080

8181
.. literalinclude:: localization/004.php
8282

83-
In this example, if the user tried to visit ``http://example.com/fr/books``, then the locale would be
83+
In this example, if the user tried to visit **http://example.com/fr/books**, then the locale would be
8484
set to ``fr``, assuming it was configured as a valid locale.
8585

8686
If the value doesn't match a valid locale as defined in ``$supportedLocales`` in **app/Config/App.php**, the default
@@ -89,7 +89,7 @@ file:
8989

9090
.. literalinclude:: localization/018.php
9191

92-
.. note:: ``useSupportedLocalesOnly()`` can be used since v4.3.0.
92+
.. note:: The ``useSupportedLocalesOnly()`` method can be used since v4.3.0.
9393

9494
Retrieving the Current Locale
9595
=============================
@@ -110,6 +110,8 @@ Language Localization
110110
Creating Language Files
111111
=======================
112112

113+
.. note:: The Language Files do not have namespaces.
114+
113115
Languages do not have any specific naming convention that are required. The file should be named logically to
114116
describe the type of content it holds. For example, let's say you want to create a file containing error messages.
115117
You might name it simply: **Errors.php**.
@@ -127,9 +129,9 @@ It also support nested definition:
127129
Basic Usage
128130
===========
129131

130-
You can use the ``lang()`` helper function to retrieve text from any of the language files, by passing the
132+
You can use the :php:func:`lang()` helper function to retrieve text from any of the language files, by passing the
131133
filename and the language key as the first parameter, separated by a period (.). For example, to load the
132-
``errorEmailMissing`` string from the ``Errors`` language file, you would do the following:
134+
``errorEmailMissing`` string from the **Errors.php** language file, you would do the following:
133135

134136
.. literalinclude:: localization/010.php
135137

@@ -183,7 +185,7 @@ Specifying Locale
183185
-----------------
184186

185187
To specify a different locale to be used when replacing parameters, you can pass the locale in as the
186-
third parameter to the ``lang()`` method.
188+
third parameter to the ``lang()`` function.
187189

188190
.. literalinclude:: localization/016.php
189191

@@ -198,8 +200,8 @@ Language Fallback
198200
=================
199201

200202
If you have a set of messages for a given locale, for instance
201-
``Language/en/app.php``, you can add language variants for that locale,
202-
each in its own folder, for instance ``Language/en-US/app.php``.
203+
**Language/en/app.php**, you can add language variants for that locale,
204+
each in its own folder, for instance **Language/en-US/app.php**.
203205

204206
You only need to provide values for those messages that would be
205207
localized differently for that locale variant. Any missing message
@@ -210,19 +212,23 @@ in case new messages are added to the framework and you haven't had
210212
a chance to translate them yet for your locale.
211213

212214
So, if you are using the locale ``fr-CA``, then a localized
213-
message will first be sought in ``Language/fr/CA``, then in
214-
``Language/fr``, and finally in ``Language/en``.
215+
message will first be sought in the **Language/fr-CA** directory, then in
216+
the **Language/fr** directory, and finally in the **Language/en** directory.
215217

216218
Message Translations
217219
====================
218220

219221
We have an "official" set of translations in their
220222
`own repository <https://github.com/codeigniter4/translations>`_.
221223

222-
You could download that repository, and copy its ``Language`` folder
223-
into your ``app``. The incorporated translations will be automatically
224-
picked up because the ``App`` namespace is mapped to your ``app`` folder.
224+
You could download that repository, and copy its **Language** folder
225+
into your **app** folder. The incorporated translations will be automatically
226+
picked up because the ``App`` namespace is mapped to your **app** folder.
227+
228+
Alternately, a better practice would be to run the following command inside your
229+
project::
230+
231+
> composer require codeigniter4/translations
225232

226-
Alternately, a better practice would be to ``composer require codeigniter4/translations``
227-
inside your project, and the translated messages will be automatically picked
233+
The translated messages will be automatically picked
228234
up because the translations folders get mapped appropriately.

user_guide_src/source/outgoing/localization/001.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
class App extends BaseConfig
88
{
9+
// ...
10+
911
public string $defaultLocale = 'en';
1012

1113
// ...

user_guide_src/source/outgoing/localization/002.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
class App extends BaseConfig
88
{
9+
// ...
10+
911
public bool $negotiateLocale = true;
1012

1113
// ...

user_guide_src/source/outgoing/localization/003.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
class App extends BaseConfig
88
{
9+
// ...
10+
911
public array $supportedLocales = ['en', 'es', 'fr-FR'];
1012

1113
// ...

0 commit comments

Comments
 (0)