Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 18 additions & 13 deletions development/language/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,24 @@ Using the Language System in php
================================

The object holding the language dictionary for the current user is the ``$user``
object (``phpbb\user`` class). To get the translation of a language entry inside
of php code, call the ``phpbb\user::lang()`` method:
object (``phpbb\language\language`` class). To get the translation of a language
entry inside of php code, call the ``phpbb\language\language::lang()`` method:

.. code-block:: php

$user->lang('LANG_KEY');
$language->lang('LANG_KEY');

You can also insert values into translated string. Use ``%s`` as placeholders
for string in the language string and ``%d`` for integers:

.. code-block:: php

// Language string: "My translation has a %s"
echo $user->lang('LANG_KEY', 'parameter');
echo $language->lang('LANG_KEY', 'parameter');
// Output: "My translation has a parameter"

// Language string: "My translation has %d parameter"
echo $user->lang('LANG_KEY', 1);
echo $language->lang('LANG_KEY', 1);
// Output: "My translation has 1 parameter"

.. warning::
Expand All @@ -55,13 +55,14 @@ for string in the language string and ``%d`` for integers:
The dictionary is not loaded into memory all at once, but is split up in several
files, which have to be manually loaded on demand. The files are stored inside
the language directory, named after its
`language tag <https://area51.phpbb.com/docs/31x/coding-guidelines.html#translation>`_,
`language tag <https://area51.phpbb.com/docs/master/coding-guidelines.html#translation>`_,
where one subdirectory is used for each installed language. The default language
files are in the root of that subdirectory, while ACP language files go into the
``/acp`` subdirectory; email templates are placed in the ``/email``
subdirectory.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can use {L_MY_KEY} as well. @Nicofuma?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{L_MY_KEY} or {{ lang('MY_KEY') }} in full twig syntax

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{{ lang('MY_KEY') }} works also in 3.1?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIK yes


.. note::

Files for extensions should be placed in the extension's ``/language``
directory.

Expand All @@ -79,27 +80,31 @@ pass the name with the subdirectory but without extension as argument of setup.

$user->setup('search');
// or
$user->setup('ucp');
// or
$user->setup(array('ucp', 'search'));

Since ``phpbb\user::setup()`` must only be called once,
``phpbb\user::add_lang()`` has to be used, to load additional language files,
after ``phpbb\user::setup()`` has already been called.
``phpbb\language\language::add_lang()`` has to be used, to load additional
language files, after ``phpbb\user::setup()`` has already been called.

.. code-block:: php

$language->add_lang('search');
// or
$language->add_lang(array('ucp', 'search'));

Loading from an extension
-------------------------

To load a file from an extension
you need to use ``phpbb\user::add_lang_ext()`` which takes
you need to use ``phpbb\language\language::add_lang()`` which takes
the vendor + extension name as first argument and the array of language files as
a second argument.

.. code-block:: php

$user->add_lang_ext('acme/demo', 'demo');
$language->add_lang('demo', 'acme/demo');
// or
$user->add_lang_ext('acme/demo', array('demo', 'demo2'));
$language->add_lang(array('demo', 'demo2'), 'acme/demo');

Using the Language System in template files
===========================================
Expand Down