Upgrade framework

This commit is contained in:
2023-11-14 16:54:35 +01:00
parent 1648a5cd42
commit 4fcf6fffcc
10548 changed files with 693138 additions and 466698 deletions

View File

@@ -2,7 +2,9 @@
namespace Illuminate\Translation;
class ArrayLoader implements LoaderInterface
use Illuminate\Contracts\Translation\Loader;
class ArrayLoader implements Loader
{
/**
* All of the translation messages.
@@ -16,18 +18,14 @@ class ArrayLoader implements LoaderInterface
*
* @param string $locale
* @param string $group
* @param string $namespace
* @param string|null $namespace
* @return array
*/
public function load($locale, $group, $namespace = null)
{
$namespace = $namespace ?: '*';
if (isset($this->messages[$namespace][$locale][$group])) {
return $this->messages[$namespace][$locale][$group];
}
return [];
return $this->messages[$namespace][$locale][$group] ?? [];
}
/**
@@ -42,6 +40,17 @@ class ArrayLoader implements LoaderInterface
//
}
/**
* Add a new JSON path to the loader.
*
* @param string $path
* @return void
*/
public function addJsonPath($path)
{
//
}
/**
* Add messages to the loader.
*

View File

@@ -2,9 +2,11 @@
namespace Illuminate\Translation;
use Illuminate\Contracts\Translation\Loader;
use Illuminate\Filesystem\Filesystem;
use RuntimeException;
class FileLoader implements LoaderInterface
class FileLoader implements Loader
{
/**
* The filesystem instance.
@@ -20,6 +22,13 @@ class FileLoader implements LoaderInterface
*/
protected $path;
/**
* All of the registered paths to JSON translation files.
*
* @var array
*/
protected $jsonPaths = [];
/**
* All of the namespace hints.
*
@@ -45,16 +54,16 @@ class FileLoader implements LoaderInterface
*
* @param string $locale
* @param string $group
* @param string $namespace
* @param string|null $namespace
* @return array
*/
public function load($locale, $group, $namespace = null)
{
if ($group == '*' && $namespace == '*') {
return $this->loadJsonPath($this->path, $locale);
if ($group === '*' && $namespace === '*') {
return $this->loadJsonPaths($locale);
}
if (is_null($namespace) || $namespace == '*') {
if (is_null($namespace) || $namespace === '*') {
return $this->loadPath($this->path, $locale, $group);
}
@@ -120,17 +129,27 @@ class FileLoader implements LoaderInterface
/**
* Load a locale from the given JSON file path.
*
* @param string $path
* @param string $locale
* @return array
*
* @throws \RuntimeException
*/
protected function loadJsonPath($path, $locale)
protected function loadJsonPaths($locale)
{
if ($this->files->exists($full = "{$path}/{$locale}.json")) {
return json_decode($this->files->get($full), true);
}
return collect(array_merge($this->jsonPaths, [$this->path]))
->reduce(function ($output, $path) use ($locale) {
if ($this->files->exists($full = "{$path}/{$locale}.json")) {
$decoded = json_decode($this->files->get($full), true);
return [];
if (is_null($decoded) || json_last_error() !== JSON_ERROR_NONE) {
throw new RuntimeException("Translation file [{$full}] contains an invalid JSON structure.");
}
$output = array_merge($output, $decoded);
}
return $output;
}, []);
}
/**
@@ -154,4 +173,25 @@ class FileLoader implements LoaderInterface
{
return $this->hints;
}
/**
* Add a new JSON path to the loader.
*
* @param string $path
* @return void
*/
public function addJsonPath($path)
{
$this->jsonPaths[] = $path;
}
/**
* Get an array of all the registered paths to JSON translation files.
*
* @return array
*/
public function jsonPaths()
{
return $this->jsonPaths;
}
}

View File

@@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) Taylor Otwell
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

View File

@@ -1,32 +0,0 @@
<?php
namespace Illuminate\Translation;
interface LoaderInterface
{
/**
* Load the messages for the given locale.
*
* @param string $locale
* @param string $group
* @param string $namespace
* @return array
*/
public function load($locale, $group, $namespace = null);
/**
* Add a new namespace to the loader.
*
* @param string $namespace
* @param string $hint
* @return void
*/
public function addNamespace($namespace, $hint);
/**
* Get an array of all the registered namespaces.
*
* @return array
*/
public function namespaces();
}

View File

@@ -2,8 +2,6 @@
namespace Illuminate\Translation;
use Illuminate\Support\Str;
class MessageSelector
{
/**
@@ -26,7 +24,7 @@ class MessageSelector
$pluralIndex = $this->getPluralIndex($locale, $number);
if (count($segments) == 1 || ! isset($segments[$pluralIndex])) {
if (count($segments) === 1 || ! isset($segments[$pluralIndex])) {
return $segments[0];
}
@@ -60,20 +58,20 @@ class MessageSelector
{
preg_match('/^[\{\[]([^\[\]\{\}]*)[\}\]](.*)/s', $part, $matches);
if (count($matches) != 3) {
return;
if (count($matches) !== 3) {
return null;
}
$condition = $matches[1];
$value = $matches[2];
if (Str::contains($condition, ',')) {
list($from, $to) = explode(',', $condition, 2);
if (str_contains($condition, ',')) {
[$from, $to] = explode(',', $condition, 2);
if ($to == '*' && $number >= $from) {
if ($to === '*' && $number >= $from) {
return $value;
} elseif ($from == '*' && $number <= $to) {
} elseif ($from === '*' && $number <= $to) {
return $value;
} elseif ($number >= $from && $number <= $to) {
return $value;
@@ -100,7 +98,7 @@ class MessageSelector
* Get the index to use for pluralization.
*
* The plural rules are derived from code of the Zend Framework (2010-09-25), which
* is subject to the new BSD license (http://framework.zend.com/license/new-bsd)
* is subject to the new BSD license (https://framework.zend.com/license)
* Copyright (c) 2005-2010 - Zend Technologies USA Inc. (http://www.zend.com)
*
* @param string $locale

View File

@@ -0,0 +1,101 @@
<?php
namespace Illuminate\Translation;
use Stringable;
class PotentiallyTranslatedString implements Stringable
{
/**
* The string that may be translated.
*
* @var string
*/
protected $string;
/**
* The translated string.
*
* @var string|null
*/
protected $translation;
/**
* The validator that may perform the translation.
*
* @var \Illuminate\Contracts\Translation\Translator
*/
protected $translator;
/**
* Create a new potentially translated string.
*
* @param string $string
* @param \Illuminate\Contracts\Translation\Translator $translator
*/
public function __construct($string, $translator)
{
$this->string = $string;
$this->translator = $translator;
}
/**
* Translate the string.
*
* @param array $replace
* @param string|null $locale
* @return $this
*/
public function translate($replace = [], $locale = null)
{
$this->translation = $this->translator->get($this->string, $replace, $locale);
return $this;
}
/**
* Translates the string based on a count.
*
* @param \Countable|int|array $number
* @param array $replace
* @param string|null $locale
* @return $this
*/
public function translateChoice($number, array $replace = [], $locale = null)
{
$this->translation = $this->translator->choice($this->string, $number, $replace, $locale);
return $this;
}
/**
* Get the original string.
*
* @return string
*/
public function original()
{
return $this->string;
}
/**
* Get the potentially translated string.
*
* @return string
*/
public function __toString()
{
return $this->translation ?? $this->string;
}
/**
* Get the potentially translated string.
*
* @return string
*/
public function toString()
{
return (string) $this;
}
}

View File

@@ -2,17 +2,11 @@
namespace Illuminate\Translation;
use Illuminate\Contracts\Support\DeferrableProvider;
use Illuminate\Support\ServiceProvider;
class TranslationServiceProvider extends ServiceProvider
class TranslationServiceProvider extends ServiceProvider implements DeferrableProvider
{
/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = true;
/**
* Register the service provider.
*
@@ -28,11 +22,11 @@ class TranslationServiceProvider extends ServiceProvider
// When registering the translator component, we'll need to set the default
// locale as well as the fallback locale. So, we'll grab the application
// configuration so we can easily get both of these values from there.
$locale = $app['config']['app.locale'];
$locale = $app->getLocale();
$trans = new Translator($loader, $locale);
$trans->setFallback($app['config']['app.fallback_locale']);
$trans->setFallback($app->getFallbackLocale());
return $trans;
});

View File

@@ -2,13 +2,13 @@
namespace Illuminate\Translation;
use Countable;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Illuminate\Support\Collection;
use Illuminate\Support\Traits\Macroable;
use Illuminate\Support\NamespacedItemResolver;
use Illuminate\Contracts\Translation\Loader;
use Illuminate\Contracts\Translation\Translator as TranslatorContract;
use Illuminate\Support\Arr;
use Illuminate\Support\NamespacedItemResolver;
use Illuminate\Support\Str;
use Illuminate\Support\Traits\Macroable;
use InvalidArgumentException;
class Translator extends NamespacedItemResolver implements TranslatorContract
{
@@ -17,7 +17,7 @@ class Translator extends NamespacedItemResolver implements TranslatorContract
/**
* The loader implementation.
*
* @var \Illuminate\Translation\LoaderInterface
* @var \Illuminate\Contracts\Translation\Loader
*/
protected $loader;
@@ -49,17 +49,25 @@ class Translator extends NamespacedItemResolver implements TranslatorContract
*/
protected $selector;
/**
* The callable that should be invoked to determine applicable locales.
*
* @var callable
*/
protected $determineLocalesUsing;
/**
* Create a new translator instance.
*
* @param \Illuminate\Translation\LoaderInterface $loader
* @param \Illuminate\Contracts\Translation\Loader $loader
* @param string $locale
* @return void
*/
public function __construct(LoaderInterface $loader, $locale)
public function __construct(Loader $loader, $locale)
{
$this->loader = $loader;
$this->locale = $locale;
$this->setLocale($locale);
}
/**
@@ -87,65 +95,16 @@ class Translator extends NamespacedItemResolver implements TranslatorContract
return $this->get($key, [], $locale, $fallback) !== $key;
}
/**
* Get the translation for a given key.
*
* @param string $key
* @param array $replace
* @param string $locale
* @return string|array|null
*/
public function trans($key, array $replace = [], $locale = null)
{
return $this->get($key, $replace, $locale);
}
/**
* Get the translation for the given key.
*
* @param string $key
* @param array $replace
* @param array $replace
* @param string|null $locale
* @param bool $fallback
* @return string|array|null
* @return string|array
*/
public function get($key, array $replace = [], $locale = null, $fallback = true)
{
list($namespace, $group, $item) = $this->parseKey($key);
// Here we will get the locale that should be used for the language line. If one
// was not passed, we will use the default locales which was given to us when
// the translator was instantiated. Then, we can load the lines and return.
$locales = $fallback ? $this->localeArray($locale)
: [$locale ?: $this->locale];
foreach ($locales as $locale) {
if (! is_null($line = $this->getLine(
$namespace, $group, $locale, $item, $replace
))) {
break;
}
}
// If the line doesn't exist, we will return back the key which was requested as
// that will be quick to spot in the UI if language keys are wrong or missing
// from the application's language files. Otherwise we can return the line.
if (isset($line)) {
return $line;
}
return $key;
}
/**
* Get the translation for a given key from the JSON translation files.
*
* @param string $key
* @param array $replace
* @param string $locale
* @return string
*/
public function getFromJson($key, array $replace = [], $locale = null)
{
$locale = $locale ?: $this->locale;
@@ -154,20 +113,31 @@ class Translator extends NamespacedItemResolver implements TranslatorContract
// only one level deep so we do not need to do any fancy searching through it.
$this->load('*', '*', $locale);
$line = isset($this->loaded['*']['*'][$locale][$key])
? $this->loaded['*']['*'][$locale][$key] : null;
$line = $this->loaded['*']['*'][$locale][$key] ?? null;
// If we can't find a translation for the JSON key, we will attempt to translate it
// using the typical translation file. This way developers can always just use a
// helper such as __ instead of having to pick between trans or __ with views.
if (! isset($line)) {
$fallback = $this->get($key, $replace, $locale);
[$namespace, $group, $item] = $this->parseKey($key);
if ($fallback !== $key) {
return $fallback;
// Here we will get the locale that should be used for the language line. If one
// was not passed, we will use the default locales which was given to us when
// the translator was instantiated. Then, we can load the lines and return.
$locales = $fallback ? $this->localeArray($locale) : [$locale];
foreach ($locales as $locale) {
if (! is_null($line = $this->getLine(
$namespace, $group, $locale, $item, $replace
))) {
return $line;
}
}
}
// If the line doesn't exist, we will return back the key which was requested as
// that will be quick to spot in the UI if language keys are wrong or missing
// from the application's language files. Otherwise we can return the line.
return $this->makeReplacements($line ?: $key, $replace);
}
@@ -175,23 +145,9 @@ class Translator extends NamespacedItemResolver implements TranslatorContract
* Get a translation according to an integer value.
*
* @param string $key
* @param int|array|\Countable $number
* @param array $replace
* @param string $locale
* @return string
*/
public function transChoice($key, $number, array $replace = [], $locale = null)
{
return $this->choice($key, $number, $replace, $locale);
}
/**
* Get a translation according to an integer value.
*
* @param string $key
* @param int|array|\Countable $number
* @param array $replace
* @param string $locale
* @param \Countable|int|array $number
* @param array $replace
* @param string|null $locale
* @return string
*/
public function choice($key, $number, array $replace = [], $locale = null)
@@ -203,7 +159,7 @@ class Translator extends NamespacedItemResolver implements TranslatorContract
// If the given "number" is actually an array or countable we will simply count the
// number of elements in an instance. This allows developers to pass an array of
// items without having to count it on their end first which gives bad syntax.
if (is_array($number) || $number instanceof Countable) {
if (is_countable($number)) {
$number = count($number);
}
@@ -232,7 +188,7 @@ class Translator extends NamespacedItemResolver implements TranslatorContract
* @param string $group
* @param string $locale
* @param string $item
* @param array $replace
* @param array $replace
* @return string|array|null
*/
protected function getLine($namespace, $group, $locale, $item, array $replace)
@@ -244,6 +200,10 @@ class Translator extends NamespacedItemResolver implements TranslatorContract
if (is_string($line)) {
return $this->makeReplacements($line, $replace);
} elseif (is_array($line) && count($line) > 0) {
array_walk_recursive($line, function (&$value, $key) use ($replace) {
$value = $this->makeReplacements($value, $replace);
});
return $line;
}
}
@@ -252,7 +212,7 @@ class Translator extends NamespacedItemResolver implements TranslatorContract
* Make the place-holder replacements on a line.
*
* @param string $line
* @param array $replace
* @param array $replace
* @return string
*/
protected function makeReplacements($line, array $replace)
@@ -261,30 +221,15 @@ class Translator extends NamespacedItemResolver implements TranslatorContract
return $line;
}
$replace = $this->sortReplacements($replace);
$shouldReplace = [];
foreach ($replace as $key => $value) {
$line = str_replace(
[':'.$key, ':'.Str::upper($key), ':'.Str::ucfirst($key)],
[$value, Str::upper($value), Str::ucfirst($value)],
$line
);
$shouldReplace[':'.Str::ucfirst($key ?? '')] = Str::ucfirst($value ?? '');
$shouldReplace[':'.Str::upper($key ?? '')] = Str::upper($value ?? '');
$shouldReplace[':'.$key] = $value;
}
return $line;
}
/**
* Sort the replacements array.
*
* @param array $replace
* @return array
*/
protected function sortReplacements(array $replace)
{
return (new Collection($replace))->sortBy(function ($value, $key) {
return mb_strlen($key) * -1;
})->all();
return strtr($line, $shouldReplace);
}
/**
@@ -298,7 +243,7 @@ class Translator extends NamespacedItemResolver implements TranslatorContract
public function addLines(array $lines, $locale, $namespace = '*')
{
foreach ($lines as $key => $value) {
list($group, $item) = explode('.', $key, 2);
[$group, $item] = explode('.', $key, 2);
Arr::set($this->loaded, "$namespace.$group.$locale.$item", $value);
}
@@ -351,6 +296,17 @@ class Translator extends NamespacedItemResolver implements TranslatorContract
$this->loader->addNamespace($namespace, $hint);
}
/**
* Add a new JSON path to the loader.
*
* @param string $path
* @return void
*/
public function addJsonPath($path)
{
$this->loader->addJsonPath($path);
}
/**
* Parse a key into namespace, group, and item.
*
@@ -376,7 +332,20 @@ class Translator extends NamespacedItemResolver implements TranslatorContract
*/
protected function localeArray($locale)
{
return array_filter([$locale ?: $this->locale, $this->fallback]);
$locales = array_filter([$locale ?: $this->locale, $this->fallback]);
return call_user_func($this->determineLocalesUsing ?: fn () => $locales, $locales);
}
/**
* Specify a callback that should be invoked to determined the applicable locale array.
*
* @param callable $callback
* @return void
*/
public function determineLocalesUsing($callback)
{
$this->determineLocalesUsing = $callback;
}
/**
@@ -407,7 +376,7 @@ class Translator extends NamespacedItemResolver implements TranslatorContract
/**
* Get the language line loader implementation.
*
* @return \Illuminate\Translation\LoaderInterface
* @return \Illuminate\Contracts\Translation\Loader
*/
public function getLoader()
{
@@ -439,9 +408,15 @@ class Translator extends NamespacedItemResolver implements TranslatorContract
*
* @param string $locale
* @return void
*
* @throws \InvalidArgumentException
*/
public function setLocale($locale)
{
if (Str::contains($locale, ['/', '\\'])) {
throw new InvalidArgumentException('Invalid characters present in locale.');
}
$this->locale = $locale;
}
@@ -465,4 +440,15 @@ class Translator extends NamespacedItemResolver implements TranslatorContract
{
$this->fallback = $fallback;
}
/**
* Set the loaded translation groups.
*
* @param array $loaded
* @return void
*/
public function setLoaded(array $loaded)
{
$this->loaded = $loaded;
}
}

View File

@@ -14,10 +14,13 @@
}
],
"require": {
"php": ">=5.6.4",
"illuminate/contracts": "5.4.*",
"illuminate/filesystem": "5.4.*",
"illuminate/support": "5.4.*"
"php": "^8.0.2",
"ext-json": "*",
"illuminate/collections": "^9.0",
"illuminate/contracts": "^9.0",
"illuminate/macroable": "^9.0",
"illuminate/filesystem": "^9.0",
"illuminate/support": "^9.0"
},
"autoload": {
"psr-4": {
@@ -26,7 +29,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "5.4-dev"
"dev-master": "9.x-dev"
}
},
"config": {