Skip to content

Commit

Permalink
Merge pull request #3849 from paulbalandan/language-no-file
Browse files Browse the repository at this point in the history
Language: Allow parsing language strings without file
  • Loading branch information
paulbalandan authored Nov 4, 2020
2 parents e839289 + 21d16c9 commit 4d52ad0
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 127 deletions.
39 changes: 13 additions & 26 deletions system/Language/Language.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
*/
class Language
{

/**
* Stores the retrieved language lines
* from files for faster retrieval on
Expand Down Expand Up @@ -60,10 +59,10 @@ public function __construct(string $locale)
{
$this->locale = $locale;

if (class_exists('\MessageFormatter'))
if (class_exists('MessageFormatter'))
{
$this->intlSupport = true;
};
}
}

//--------------------------------------------------------------------
Expand Down Expand Up @@ -108,51 +107,38 @@ public function getLocale(): string
*/
public function getLine(string $line, array $args = [])
{
// ignore requests with no file specified
if (! strpos($line, '.'))
// if no file is given, just parse the line
if (strpos($line, '.') === false)
{
return $line;
return $this->formatMessage($line, $args);
}

// Parse out the file name and the actual alias.
// Will load the language file and strings.
[
$file,
$parsedLine,
] = $this->parseLine($line, $this->locale);
list($file, $parsedLine) = $this->parseLine($line, $this->locale);

$output = $this->getTranslationOutput($this->locale, $file, $parsedLine);

if ($output === null && strpos($this->locale, '-'))
{
[$locale] = explode('-', $this->locale, 2);
list($locale) = explode('-', $this->locale, 2);

[
$file,
$parsedLine,
] = $this->parseLine($line, $locale);
list($file, $parsedLine) = $this->parseLine($line, $locale);

$output = $this->getTranslationOutput($locale, $file, $parsedLine);
}

// if still not found, try English
if ($output === null)
{
[
$file,
$parsedLine,
] = $this->parseLine($line, 'en');
list($file, $parsedLine) = $this->parseLine($line, 'en');

$output = $this->getTranslationOutput('en', $file, $parsedLine);
}

$output = $output ?? $line;

if (! empty($args))
{
$output = $this->formatMessage($output, $args);
}

return $output;
return $this->formatMessage($output, $args);
}

//--------------------------------------------------------------------
Expand Down Expand Up @@ -230,7 +216,7 @@ protected function parseLine(string $line, string $locale): array
*/
protected function formatMessage($message, array $args = [])
{
if (! $this->intlSupport || ! $args)
if (! $this->intlSupport || $args === [])
{
return $message;
}
Expand All @@ -241,6 +227,7 @@ protected function formatMessage($message, array $args = [])
{
$message[$index] = $this->formatMessage($value, $args);
}

return $message;
}

Expand Down
Loading

0 comments on commit 4d52ad0

Please sign in to comment.