Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ability for nested language definition #2833

Merged
Merged
Show file tree
Hide file tree
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
14 changes: 13 additions & 1 deletion system/Language/Language.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,19 @@ public function getLine(string $line, array $args = [])
$parsedLine,
] = $this->parseLine($line, $this->locale);

$output = $this->language[$this->locale][$file][$parsedLine] ?? null;
foreach (explode('.', $parsedLine) as $row)
{
if (! isset($current))
{
$current = $this->language[$this->locale][$file] ?? null;
}

$output = $current[$row] ?? null;
if (is_array($output))
{
$current = $output;
}
}

if ($output === null && strpos($this->locale, '-'))
{
Expand Down
11 changes: 11 additions & 0 deletions tests/_support/Language/en/Nested.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

return [
'a' => [
'b' => [
'c' => [
'd' => 'e',
],
],
],
];
8 changes: 8 additions & 0 deletions tests/system/Language/LanguageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -313,4 +313,12 @@ public function testAllTheWayFallbacks()
$this->assertEquals('Hanging Gardens of Babylon', $language->getLine('Allin.sev'));
}

public function testLanguageNestedArrayDefinition()
{
$lang = new SecondMockLanguage('en');
$lang->loadem('Nested', 'en');

$this->assertEquals('e', $lang->getLine('Nested.a.b.c.d'));
}

}
21 changes: 19 additions & 2 deletions user_guide_src/source/outgoing/localization.rst
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,18 @@ Languages do not have any specific naming convention that are required. The file
describe the type of content it holds. For example, let's say you want to create a file containing error messages.
You might name it simply: **Errors.php**.

Within the file, you would return an array, where each element in the array has a language key and the string to return::
Within the file, you would return an array, where each element in the array has a language key and can have string to return::

'language_key' => 'The actual message to be shown.'

It also support nested definition:

'language_key' => [
'nested' => [
'key' => 'The actual message to be shown.'
],
],

.. note:: It's good practice to use a common prefix for all messages in a given file to avoid collisions with
similarly named items in other files. For example, if you are creating error messages you might prefix them
with error\_
Expand All @@ -127,6 +135,11 @@ Within the file, you would return an array, where each element in the array has
'errorEmailMissing' => 'You must submit an email address',
'errorURLMissing' => 'You must submit a URL',
'errorUsernameMissing' => 'You must submit a username',
'nested' => [
'error' => [
'message' => 'A specific error message',
],
],
];

Basic Usage
Expand All @@ -138,8 +151,12 @@ filename and the language key as the first parameter, separated by a period (.).

echo lang('Errors.errorEmailMissing');

For nested definition, you would do the following::

echo lang('Errors.nested.error.message');

If the requested language key doesn't exist in the file for the current locale, the string will be passed
back, unchanged. In this example, it would return 'Errors.errorEmailMissing' if it didn't exist.
back, unchanged. In this example, it would return 'Errors.errorEmailMissing' or 'Errors.nested.error.message' if it didn't exist.

Replacing Parameters
--------------------
Expand Down