mirror of
https://github.com/archtechx/gloss.git
synced 2025-12-12 03:04:04 +00:00
Initial commit
This commit is contained in:
commit
f73d6e2dce
14 changed files with 838 additions and 0 deletions
9
.gitattributes
vendored
Normal file
9
.gitattributes
vendored
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
/.github export-ignore
|
||||||
|
/.gitattributes export-ignore
|
||||||
|
/.gitignore export-ignore
|
||||||
|
|
||||||
|
/tests export-ignore
|
||||||
|
|
||||||
|
/phpstan.neon
|
||||||
|
/.php_cs.php
|
||||||
|
/phpunit.xml export-ignore
|
||||||
52
.github/workflows/ci.yml
vendored
Normal file
52
.github/workflows/ci.yml
vendored
Normal file
|
|
@ -0,0 +1,52 @@
|
||||||
|
name: CI
|
||||||
|
|
||||||
|
env:
|
||||||
|
COMPOSE_INTERACTIVE_NO_CLI: 1
|
||||||
|
LEAN_MYSQL_PORT: 3307
|
||||||
|
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
pull_request:
|
||||||
|
branches: [ master ]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
phpunit:
|
||||||
|
name: Tests (PHPUnit)
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v2
|
||||||
|
- name: Start docker containers
|
||||||
|
run: docker-compose up -d
|
||||||
|
- name: Install composer dependencies
|
||||||
|
run: composer install
|
||||||
|
- name: Run tests
|
||||||
|
run: vendor/bin/phpunit
|
||||||
|
|
||||||
|
phpstan:
|
||||||
|
name: Static analysis (PHPStan)
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v2
|
||||||
|
- name: Install composer dependencies
|
||||||
|
run: composer install
|
||||||
|
- name: Run phpstan
|
||||||
|
run: vendor/bin/phpstan analyse
|
||||||
|
|
||||||
|
php-cs-fixer:
|
||||||
|
name: Code style (php-cs-fixer)
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v2
|
||||||
|
- name: Install php-cs-fixer
|
||||||
|
run: composer global require friendsofphp/php-cs-fixer
|
||||||
|
- name: Run php-cs-fixer
|
||||||
|
run: $HOME/.composer/vendor/bin/php-cs-fixer fix --config=.php_cs.php
|
||||||
|
- name: Commit changes from php-cs-fixer
|
||||||
|
uses: EndBug/add-and-commit@v5
|
||||||
|
with:
|
||||||
|
author_name: Samuel Štancl
|
||||||
|
author_email: samuel.stancl@gmail.com
|
||||||
|
message: Fix code style (php-cs-fixer)
|
||||||
5
.gitignore
vendored
Normal file
5
.gitignore
vendored
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
.phpunit.result.cache
|
||||||
|
composer.lock
|
||||||
|
vendor/
|
||||||
|
.php_cs.cache
|
||||||
|
.vscode/
|
||||||
143
.php_cs.php
Normal file
143
.php_cs.php
Normal file
|
|
@ -0,0 +1,143 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
use PhpCsFixer\Config;
|
||||||
|
use PhpCsFixer\Finder;
|
||||||
|
|
||||||
|
$rules = [
|
||||||
|
'array_syntax' => ['syntax' => 'short'],
|
||||||
|
'binary_operator_spaces' => [
|
||||||
|
'default' => 'single_space',
|
||||||
|
'operators' => ['=>' => null]
|
||||||
|
],
|
||||||
|
'blank_line_after_namespace' => true,
|
||||||
|
'blank_line_after_opening_tag' => true,
|
||||||
|
'blank_line_before_statement' => [
|
||||||
|
'statements' => ['return']
|
||||||
|
],
|
||||||
|
'braces' => true,
|
||||||
|
'cast_spaces' => true,
|
||||||
|
'class_attributes_separation' => [
|
||||||
|
'elements' => ['method']
|
||||||
|
],
|
||||||
|
'class_definition' => true,
|
||||||
|
'concat_space' => [
|
||||||
|
'spacing' => 'one'
|
||||||
|
],
|
||||||
|
'declare_equal_normalize' => true,
|
||||||
|
'elseif' => true,
|
||||||
|
'encoding' => true,
|
||||||
|
'full_opening_tag' => true,
|
||||||
|
'declare_strict_types' => true,
|
||||||
|
'fully_qualified_strict_types' => true, // added by Shift
|
||||||
|
'function_declaration' => true,
|
||||||
|
'function_typehint_space' => true,
|
||||||
|
'heredoc_to_nowdoc' => true,
|
||||||
|
'include' => true,
|
||||||
|
'increment_style' => ['style' => 'post'],
|
||||||
|
'indentation_type' => true,
|
||||||
|
'linebreak_after_opening_tag' => true,
|
||||||
|
'line_ending' => true,
|
||||||
|
'lowercase_cast' => true,
|
||||||
|
'lowercase_constants' => true,
|
||||||
|
'lowercase_keywords' => true,
|
||||||
|
'lowercase_static_reference' => true, // added from Symfony
|
||||||
|
'magic_method_casing' => true, // added from Symfony
|
||||||
|
'magic_constant_casing' => true,
|
||||||
|
'method_argument_space' => true,
|
||||||
|
'native_function_casing' => true,
|
||||||
|
'no_alias_functions' => true,
|
||||||
|
'no_extra_blank_lines' => [
|
||||||
|
'tokens' => [
|
||||||
|
'extra',
|
||||||
|
'throw',
|
||||||
|
'use',
|
||||||
|
'use_trait',
|
||||||
|
]
|
||||||
|
],
|
||||||
|
'no_blank_lines_after_class_opening' => true,
|
||||||
|
'no_blank_lines_after_phpdoc' => true,
|
||||||
|
'no_closing_tag' => true,
|
||||||
|
'no_empty_phpdoc' => true,
|
||||||
|
'no_empty_statement' => true,
|
||||||
|
'no_leading_import_slash' => true,
|
||||||
|
'no_leading_namespace_whitespace' => true,
|
||||||
|
'no_mixed_echo_print' => [
|
||||||
|
'use' => 'echo'
|
||||||
|
],
|
||||||
|
'no_multiline_whitespace_around_double_arrow' => true,
|
||||||
|
'multiline_whitespace_before_semicolons' => [
|
||||||
|
'strategy' => 'no_multi_line'
|
||||||
|
],
|
||||||
|
'no_short_bool_cast' => true,
|
||||||
|
'no_singleline_whitespace_before_semicolons' => true,
|
||||||
|
'no_spaces_after_function_name' => true,
|
||||||
|
'no_spaces_around_offset' => true,
|
||||||
|
'no_spaces_inside_parenthesis' => true,
|
||||||
|
'no_trailing_comma_in_list_call' => true,
|
||||||
|
'no_trailing_comma_in_singleline_array' => true,
|
||||||
|
'no_trailing_whitespace' => true,
|
||||||
|
'no_trailing_whitespace_in_comment' => true,
|
||||||
|
'no_unneeded_control_parentheses' => true,
|
||||||
|
'no_unreachable_default_argument_value' => true,
|
||||||
|
'no_useless_return' => true,
|
||||||
|
'no_whitespace_before_comma_in_array' => true,
|
||||||
|
'no_whitespace_in_blank_line' => true,
|
||||||
|
'normalize_index_brace' => true,
|
||||||
|
'not_operator_with_successor_space' => true,
|
||||||
|
'object_operator_without_whitespace' => true,
|
||||||
|
'ordered_imports' => ['sortAlgorithm' => 'alpha'],
|
||||||
|
'phpdoc_indent' => true,
|
||||||
|
'phpdoc_inline_tag' => true,
|
||||||
|
'phpdoc_no_access' => true,
|
||||||
|
'phpdoc_no_package' => true,
|
||||||
|
'phpdoc_no_useless_inheritdoc' => true,
|
||||||
|
'phpdoc_scalar' => true,
|
||||||
|
'phpdoc_single_line_var_spacing' => true,
|
||||||
|
'phpdoc_summary' => true,
|
||||||
|
'phpdoc_to_comment' => true,
|
||||||
|
'phpdoc_trim' => true,
|
||||||
|
'phpdoc_types' => true,
|
||||||
|
'phpdoc_var_without_name' => true,
|
||||||
|
'psr4' => true,
|
||||||
|
'self_accessor' => true,
|
||||||
|
'short_scalar_cast' => true,
|
||||||
|
'simplified_null_return' => false, // disabled by Shift
|
||||||
|
'single_blank_line_at_eof' => true,
|
||||||
|
'single_blank_line_before_namespace' => true,
|
||||||
|
'single_class_element_per_statement' => true,
|
||||||
|
'single_import_per_statement' => true,
|
||||||
|
'single_line_after_imports' => true,
|
||||||
|
'no_unused_imports' => true,
|
||||||
|
'single_line_comment_style' => [
|
||||||
|
'comment_types' => ['hash']
|
||||||
|
],
|
||||||
|
'single_quote' => true,
|
||||||
|
'space_after_semicolon' => true,
|
||||||
|
'standardize_not_equals' => true,
|
||||||
|
'switch_case_semicolon_to_colon' => true,
|
||||||
|
'switch_case_space' => true,
|
||||||
|
'ternary_operator_spaces' => true,
|
||||||
|
'trailing_comma_in_multiline_array' => true,
|
||||||
|
'trim_array_spaces' => true,
|
||||||
|
'unary_operator_spaces' => true,
|
||||||
|
'visibility_required' => [
|
||||||
|
'elements' => ['method', 'property']
|
||||||
|
],
|
||||||
|
'whitespace_after_comma_in_array' => true,
|
||||||
|
];
|
||||||
|
|
||||||
|
$project_path = getcwd();
|
||||||
|
$finder = Finder::create()
|
||||||
|
->in([
|
||||||
|
$project_path . '/src',
|
||||||
|
])
|
||||||
|
->name('*.php')
|
||||||
|
->notName('*.blade.php')
|
||||||
|
->ignoreDotFiles(true)
|
||||||
|
->ignoreVCS(true);
|
||||||
|
|
||||||
|
return Config::create()
|
||||||
|
->setFinder($finder)
|
||||||
|
->setRules($rules)
|
||||||
|
->setRiskyAllowed(true)
|
||||||
|
->setUsingCache(true);
|
||||||
27
composer.json
Normal file
27
composer.json
Normal file
|
|
@ -0,0 +1,27 @@
|
||||||
|
{
|
||||||
|
"name": "leanadmin/gloss",
|
||||||
|
"description": "Brilliant localization for Laravel",
|
||||||
|
"license": "MIT",
|
||||||
|
"autoload": {
|
||||||
|
"psr-4": {
|
||||||
|
"Lean\\Gloss\\": "src"
|
||||||
|
},
|
||||||
|
"files": [
|
||||||
|
"src/helpers.php"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"autoload-dev": {
|
||||||
|
"psr-4": {
|
||||||
|
"Lean\\Gloss\\Tests\\": "tests"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"require": {
|
||||||
|
"illuminate/translation": "^8.18"
|
||||||
|
},
|
||||||
|
"require-dev": {
|
||||||
|
"orchestra/testbench": "^6.4.0",
|
||||||
|
"orchestra/testbench-core": "6.7.0",
|
||||||
|
"phpunit/phpunit": "^9.5",
|
||||||
|
"nunomaduro/larastan": "^0.6.11"
|
||||||
|
}
|
||||||
|
}
|
||||||
37
phpstan.neon
Normal file
37
phpstan.neon
Normal file
|
|
@ -0,0 +1,37 @@
|
||||||
|
includes:
|
||||||
|
- ./vendor/nunomaduro/larastan/extension.neon
|
||||||
|
|
||||||
|
parameters:
|
||||||
|
paths:
|
||||||
|
- src
|
||||||
|
- tests
|
||||||
|
|
||||||
|
level: 8
|
||||||
|
|
||||||
|
universalObjectCratesClasses:
|
||||||
|
- Illuminate\Routing\Route
|
||||||
|
|
||||||
|
ignoreErrors:
|
||||||
|
-
|
||||||
|
message: '#has no return typehint specified#'
|
||||||
|
paths:
|
||||||
|
- tests/*
|
||||||
|
- src/GlossTranslator.php
|
||||||
|
-
|
||||||
|
message: '#Cannot call method (.*?) on Lean\\Gloss\\GlossTranslator\|#'
|
||||||
|
paths:
|
||||||
|
- tests/*
|
||||||
|
-
|
||||||
|
message: '#with no typehint specified#'
|
||||||
|
paths:
|
||||||
|
- src/GlossTranslator.php
|
||||||
|
-
|
||||||
|
message: '#of function str_replace expects#'
|
||||||
|
paths:
|
||||||
|
- src/GlossTranslator.php
|
||||||
|
-
|
||||||
|
message: '#should return Lean\\Gloss\\GlossTranslator\|string\|void\|null but returns array\|string#'
|
||||||
|
paths:
|
||||||
|
- src/helpers.php
|
||||||
|
|
||||||
|
checkMissingIterableValueType: false
|
||||||
27
phpunit.xml
Normal file
27
phpunit.xml
Normal file
|
|
@ -0,0 +1,27 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" backupStaticAttributes="false" bootstrap="vendor/autoload.php" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
|
||||||
|
<coverage processUncoveredFiles="true">
|
||||||
|
<include>
|
||||||
|
<directory suffix=".php">./src</directory>
|
||||||
|
</include>
|
||||||
|
<exclude>
|
||||||
|
<file>./src/routes.php</file>
|
||||||
|
</exclude>
|
||||||
|
</coverage>
|
||||||
|
<testsuites>
|
||||||
|
<testsuite name="Unit">
|
||||||
|
<directory suffix="Test.php">./tests</directory>
|
||||||
|
</testsuite>
|
||||||
|
</testsuites>
|
||||||
|
<php>
|
||||||
|
<env name="APP_ENV" value="testing"/>
|
||||||
|
<env name="BCRYPT_ROUNDS" value="4"/>
|
||||||
|
<env name="CACHE_DRIVER" value="redis"/>
|
||||||
|
<env name="MAIL_DRIVER" value="array"/>
|
||||||
|
<env name="QUEUE_CONNECTION" value="sync"/>
|
||||||
|
<env name="SESSION_DRIVER" value="array"/>
|
||||||
|
<env name="DB_CONNECTION" value="sqlite"/>
|
||||||
|
<env name="DB_DATABASE" value=":memory:"/>
|
||||||
|
<env name="AWS_DEFAULT_REGION" value="us-west-2"/>
|
||||||
|
</php>
|
||||||
|
</phpunit>
|
||||||
40
src/Gloss.php
Normal file
40
src/Gloss.php
Normal file
|
|
@ -0,0 +1,40 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Lean\Gloss;
|
||||||
|
|
||||||
|
use Illuminate\Support\Facades\Facade;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 🔍 Gloss ✨ — Brilliant localization for Laravel.
|
||||||
|
*
|
||||||
|
* @method static void key(string $shortKey, string $newKey) Set a key override.
|
||||||
|
* @method static void value(string $shortKey, string $value) Set a value override.
|
||||||
|
* @method static void values(string $shortKey, string $value) Set multiple value overrides.
|
||||||
|
* @method static ?string get($key, $replace = [], $locale = null) Get a translation string.
|
||||||
|
* @method static ?string choice($key, $replace = [], $locale = null) Get a translation according to an integer value.
|
||||||
|
* @method static void extend(string $shortKey, callable(string, callable): string $value) Extend a translation string.
|
||||||
|
*/
|
||||||
|
class Gloss extends Facade
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* The key used to bind Gloss to the service container.
|
||||||
|
*/
|
||||||
|
public static string $containerKey = 'gloss';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Should ___() be used as a helper?
|
||||||
|
*/
|
||||||
|
public static bool $underscoreHelper = true;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Should the Translator instance be replaced by Gloss?
|
||||||
|
*/
|
||||||
|
public static bool $shouldReplaceTranslator = false;
|
||||||
|
|
||||||
|
protected static function getFacadeAccessor()
|
||||||
|
{
|
||||||
|
return static::$containerKey;
|
||||||
|
}
|
||||||
|
}
|
||||||
33
src/GlossServiceProvider.php
Normal file
33
src/GlossServiceProvider.php
Normal file
|
|
@ -0,0 +1,33 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Lean\Gloss;
|
||||||
|
|
||||||
|
use Illuminate\Contracts\Container\Container;
|
||||||
|
use Illuminate\Support\ServiceProvider;
|
||||||
|
|
||||||
|
class GlossServiceProvider extends ServiceProvider
|
||||||
|
{
|
||||||
|
public function register()
|
||||||
|
{
|
||||||
|
$this->app->singleton(Gloss::$containerKey, function ($app) {
|
||||||
|
$loader = $app['translation.loader'];
|
||||||
|
|
||||||
|
// 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'];
|
||||||
|
|
||||||
|
$trans = new GlossTranslator($loader, $locale);
|
||||||
|
|
||||||
|
$trans->setFallback($app['config']['app.fallback_locale']);
|
||||||
|
|
||||||
|
return $trans;
|
||||||
|
});
|
||||||
|
|
||||||
|
if (Gloss::$shouldReplaceTranslator) {
|
||||||
|
$this->app->extend('translator', fn () => $this->app->make(Gloss::$containerKey));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
155
src/GlossTranslator.php
Normal file
155
src/GlossTranslator.php
Normal file
|
|
@ -0,0 +1,155 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Lean\Gloss;
|
||||||
|
|
||||||
|
use Countable;
|
||||||
|
use Illuminate\Translation\Translator;
|
||||||
|
|
||||||
|
class GlossTranslator extends Translator
|
||||||
|
{
|
||||||
|
/** Overrides that refer to a different key. */
|
||||||
|
public array $keyOverrides = [];
|
||||||
|
|
||||||
|
/** Overrides with new values. */
|
||||||
|
public array $valueOverrides = [];
|
||||||
|
|
||||||
|
/** Extensions executed after the string is built. */
|
||||||
|
public array $extensions = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Register an override that returns a different key name.
|
||||||
|
*
|
||||||
|
* @param string $shortKey
|
||||||
|
* @param string $newKey
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function key(string $shortKey, string $newKey)
|
||||||
|
{
|
||||||
|
$this->keyOverrides[$shortKey] = $newKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Register an override that returns a value.
|
||||||
|
*
|
||||||
|
* @param string $shortKey
|
||||||
|
* @param string $value
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function value(string $shortKey, string $value)
|
||||||
|
{
|
||||||
|
$this->valueOverrides[$shortKey] = $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Register multiple value overrides.
|
||||||
|
*
|
||||||
|
* @param array $values
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function values(array $values)
|
||||||
|
{
|
||||||
|
foreach ($values as $key => $value) {
|
||||||
|
$this->valueOverrides[$key] = $value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Customize a translation string's value using a callback.
|
||||||
|
*
|
||||||
|
* @param string $shortKey
|
||||||
|
* @param callable $value
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function extend(string $shortKey, callable $value)
|
||||||
|
{
|
||||||
|
$this->extensions[$shortKey][] = $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function get($key, array $replace = [], $locale = null, $fallback = true)
|
||||||
|
{
|
||||||
|
if (array_key_exists($key, $this->extensions)) {
|
||||||
|
// We recursively call the same method, but we make sure to skip this branch.
|
||||||
|
$stringWithoutReplacedVariables = $this->getWithoutExtensions($key, [], $locale, $fallback);
|
||||||
|
|
||||||
|
$replacer = function (string $string, array $replacements) {
|
||||||
|
foreach ($replacements as $from => $to) {
|
||||||
|
$string = str_replace($from, $to, $string);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $string;
|
||||||
|
};
|
||||||
|
|
||||||
|
// We run all of the extend() callbacks
|
||||||
|
$extendedString = $key;
|
||||||
|
foreach ($this->extensions[$key] as $extension) {
|
||||||
|
$extendedString = $extension($stringWithoutReplacedVariables, $replacer);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Finally, we run the string through trans() once again
|
||||||
|
// to do the replacements in Laravel and potentially
|
||||||
|
// catch edge case overrides for values in Gloss.
|
||||||
|
$key = $extendedString;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->getWithoutExtensions($key, $replace, $locale, $fallback);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getWithoutExtensions($key, $replace = [], $locale = null, $fallback = true)
|
||||||
|
{
|
||||||
|
return array_key_exists($key, $this->keyOverrides)
|
||||||
|
? $this->get($this->keyOverrides[$key])
|
||||||
|
: $this->valueOverrides[$key]
|
||||||
|
?? parent::get($key, $replace, $locale, $fallback);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function choice($key, $number, array $replace = [], $locale = null)
|
||||||
|
{
|
||||||
|
if (array_key_exists($key, $this->extensions)) {
|
||||||
|
// We recursively call the same method, but we make sure to skip this branch.
|
||||||
|
$stringWithoutReplacedVariables = $this->getWithoutExtensions($key, [], $locale);
|
||||||
|
|
||||||
|
$replacer = function (string $string, array $replacements) {
|
||||||
|
foreach ($replacements as $from => $to) {
|
||||||
|
$string = str_replace($from, $to, $string);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $string;
|
||||||
|
};
|
||||||
|
|
||||||
|
// We run all of the extend() callbacks
|
||||||
|
$extendedString = $key;
|
||||||
|
foreach ($this->extensions[$key] as $extension) {
|
||||||
|
$extendedString = $extension($stringWithoutReplacedVariables, $replacer);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Finally, we run the string through trans() once again
|
||||||
|
// to do the replacements in Laravel and potentially
|
||||||
|
// catch edge case overrides for values in Gloss.
|
||||||
|
$key = $extendedString;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->choiceWithoutExtensions($key, $number, $replace, $locale);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function choiceWithoutExtensions($key, $number, array $replace = [], $locale = null)
|
||||||
|
{
|
||||||
|
$line = $this->getWithoutExtensions(
|
||||||
|
$key, $replace, $locale = $this->localeForChoice($locale)
|
||||||
|
);
|
||||||
|
|
||||||
|
// 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) {
|
||||||
|
$number = count($number);
|
||||||
|
}
|
||||||
|
|
||||||
|
$replace['count'] = $number;
|
||||||
|
|
||||||
|
return $this->makeReplacements(
|
||||||
|
$this->getSelector()->choose($line, $number, $locale), $replace
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
45
src/helpers.php
Normal file
45
src/helpers.php
Normal file
|
|
@ -0,0 +1,45 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
use Lean\Gloss\Gloss;
|
||||||
|
|
||||||
|
if (! function_exists('gloss')) {
|
||||||
|
/**
|
||||||
|
* Resolve a translation string or Gloss instance.
|
||||||
|
*
|
||||||
|
* @param string|array|null $key
|
||||||
|
* @param array $replace
|
||||||
|
* @param string|null $locale
|
||||||
|
* @return void|string|null|\Lean\Gloss\GlossTranslator
|
||||||
|
*/
|
||||||
|
function gloss($key = null, array $replace = [], string $locale = null)
|
||||||
|
{
|
||||||
|
if (is_array($key)) {
|
||||||
|
Gloss::values($key);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_string($key)) {
|
||||||
|
return Gloss::get($key, $replace, $locale);
|
||||||
|
}
|
||||||
|
|
||||||
|
return Gloss::getFacadeRoot();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! function_exists('___') && Gloss::$underscoreHelper) {
|
||||||
|
/**
|
||||||
|
* Resolve a translation string or Gloss instance.
|
||||||
|
*
|
||||||
|
* @param string|array|null $key
|
||||||
|
* @param array $replace
|
||||||
|
* @param string|null $locale
|
||||||
|
* @return void|string|null|\Lean\Gloss\GlossTranslator
|
||||||
|
*/
|
||||||
|
function ___($key = null, array $replace = [], string $locale = null)
|
||||||
|
{
|
||||||
|
return gloss($key, $replace, $locale);
|
||||||
|
}
|
||||||
|
}
|
||||||
10
tests/GlossLoader.php
Normal file
10
tests/GlossLoader.php
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Lean\Gloss\Tests;
|
||||||
|
|
||||||
|
use Illuminate\Translation\ArrayLoader;
|
||||||
|
|
||||||
|
class GlossLoader extends ArrayLoader
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
232
tests/GlossTest.php
Normal file
232
tests/GlossTest.php
Normal file
|
|
@ -0,0 +1,232 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Lean\Gloss\Tests;
|
||||||
|
|
||||||
|
use Lean\Gloss\Gloss;
|
||||||
|
use Lean\Gloss\GlossServiceProvider;
|
||||||
|
use Lean\Gloss\GlossTranslator;
|
||||||
|
|
||||||
|
class GlossTest extends TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @internal
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function addMessage_works()
|
||||||
|
{
|
||||||
|
$this->addMessage('foo', 'bar');
|
||||||
|
|
||||||
|
$this->assertSame('bar', gloss('test.foo'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @internal
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function locale_can_be_changed_halfway_through_a_test()
|
||||||
|
{
|
||||||
|
$this->addMessage('foo', 'english', 'en');
|
||||||
|
$this->addMessage('foo', 'czech', 'cs');
|
||||||
|
|
||||||
|
$this->assertSame('english', gloss('test.foo'));
|
||||||
|
|
||||||
|
gloss()->setLocale('cs');
|
||||||
|
|
||||||
|
$this->assertSame('czech', gloss('test.foo'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function value_can_be_replaced()
|
||||||
|
{
|
||||||
|
$this->addMessage(
|
||||||
|
'resource.create',
|
||||||
|
'Create :resource'
|
||||||
|
);
|
||||||
|
|
||||||
|
Gloss::value('test.resource.create', 'Create my resource');
|
||||||
|
|
||||||
|
$this->assertNotSame('Create foo', Gloss::get('test.resource.create', ['resource' => 'foo']));
|
||||||
|
$this->assertSame('Create my resource', Gloss::get('test.resource.create', ['resource' => 'foo']));
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function short_key_can_be_replaced()
|
||||||
|
{
|
||||||
|
$this->addMessages('en', 'test', [
|
||||||
|
'resource.create' => 'Create :resource',
|
||||||
|
'foo.create' => 'Foo/Create',
|
||||||
|
]);
|
||||||
|
|
||||||
|
Gloss::key('test.resource.create', 'test.foo.create');
|
||||||
|
|
||||||
|
$this->assertNotSame('Create foo', Gloss::get('test.resource.create', ['resource' => 'foo']));
|
||||||
|
$this->assertSame('Foo/Create', Gloss::get('test.resource.create', ['resource' => 'foo']));
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function key_overrides_work_recursively()
|
||||||
|
{
|
||||||
|
$this->addMessages('en', 'test', [
|
||||||
|
'resources.create' => 'Create :resource',
|
||||||
|
'foo.create' => 'Foo/Create',
|
||||||
|
'foo.create_new' => 'Foo/Create/New',
|
||||||
|
]);
|
||||||
|
|
||||||
|
Gloss::key('test.resource.create', 'test.foo.create');
|
||||||
|
Gloss::key('test.foo.create', 'test.foo.create_new');
|
||||||
|
|
||||||
|
$this->assertNotSame('Create foo', Gloss::get('test.resource.create', ['resource' => 'foo']));
|
||||||
|
$this->assertNotSame('Create/Create', Gloss::get('test.resource.create', ['resource' => 'foo']));
|
||||||
|
$this->assertSame('Foo/Create/New', Gloss::get('test.resource.create', ['resource' => 'foo']));
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function value_overrides_dont_work_recursively()
|
||||||
|
{
|
||||||
|
$this->addMessages('en', 'test', [
|
||||||
|
'Create :resource' => 'not called',
|
||||||
|
'Create Foo' => 'not called',
|
||||||
|
]);
|
||||||
|
|
||||||
|
Gloss::value('Create :resource', 'Create :Resource');
|
||||||
|
|
||||||
|
$this->assertNotSame('not called', Gloss::get('Create :resource', ['resource' => 'foo']));
|
||||||
|
$this->assertSame('Create :Resource', Gloss::get('Create :resource', ['resource' => 'foo']));
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function keys_can_be_extended()
|
||||||
|
{
|
||||||
|
$this->addMessage('pagination', 'Showing :start to :end of :total results', 'en');
|
||||||
|
$this->addMessage('pagination', 'Zobrazeno :start až :end z :total výsledků', 'cs');
|
||||||
|
|
||||||
|
Gloss::extend('test.pagination', fn ($value, $replace) => $replace($value, [
|
||||||
|
':start' => '<span class="font-medium">:start</span>',
|
||||||
|
':end' => '<span class="font-medium">:end</span>',
|
||||||
|
':total' => '<span class="font-medium">:total</span>',
|
||||||
|
]));
|
||||||
|
|
||||||
|
$this->assertSame(
|
||||||
|
'Showing <span class="font-medium">10</span> to <span class="font-medium">20</span> of <span class="font-medium">50</span> results',
|
||||||
|
Gloss::get('test.pagination', ['start' => 10, 'end' => 20, 'total' => 50])
|
||||||
|
);
|
||||||
|
|
||||||
|
gloss()->setLocale('cs');
|
||||||
|
|
||||||
|
$this->assertSame(
|
||||||
|
'Zobrazeno <span class="font-medium">10</span> až <span class="font-medium">20</span> z <span class="font-medium">50</span> výsledků',
|
||||||
|
Gloss::get('test.pagination', ['start' => 10, 'end' => 20, 'total' => 50])
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function values_can_be_extended()
|
||||||
|
{
|
||||||
|
$string = 'Showing :start to :end of :total results';
|
||||||
|
|
||||||
|
Gloss::extend($string, fn ($value, $replace) => $replace($value, [
|
||||||
|
':start' => '<span class="font-medium">:start</span>',
|
||||||
|
':end' => '<span class="font-medium">:end</span>',
|
||||||
|
':total' => '<span class="font-medium">:total</span>',
|
||||||
|
]));
|
||||||
|
|
||||||
|
$this->assertSame(
|
||||||
|
'Showing <span class="font-medium">10</span> to <span class="font-medium">20</span> of <span class="font-medium">50</span> results',
|
||||||
|
Gloss::get($string, ['start' => 10, 'end' => 20, 'total' => 50])
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function gloss_helper_can_be_used()
|
||||||
|
{
|
||||||
|
$this->addMessage('foo', 'bar', 'en');
|
||||||
|
$this->addMessage('foo', 'baz', 'cs');
|
||||||
|
|
||||||
|
$this->assertSame('bar', gloss('test.foo'));
|
||||||
|
$this->assertSame('baz', gloss('test.foo', [], 'cs'));
|
||||||
|
|
||||||
|
gloss(['test.foo' => 'xyz']);
|
||||||
|
|
||||||
|
$this->assertSame('xyz', gloss('test.foo'));
|
||||||
|
$this->assertSame('xyz', gloss('test.foo', [], 'cs'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function ___helper_can_be_used()
|
||||||
|
{
|
||||||
|
$this->assertTrue(___() instanceof GlossTranslator);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function the_helper_can_return_the_object_instance()
|
||||||
|
{
|
||||||
|
$this->assertTrue(gloss() instanceof GlossTranslator);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function pluralization_is_supported()
|
||||||
|
{
|
||||||
|
$this->addMessage('apples', 'There is one apple|There are many apples', 'en');
|
||||||
|
$this->addMessage('apples', 'Je tam jedno jablko|Je tam mnoho jablek', 'cs');
|
||||||
|
|
||||||
|
$this->assertSame('There is one apple', gloss()->choice('test.apples', 1));
|
||||||
|
$this->assertSame('There are many apples', gloss()->choice('test.apples', 2));
|
||||||
|
|
||||||
|
gloss()->setLocale('cs');
|
||||||
|
|
||||||
|
$this->assertSame('Je tam jedno jablko', gloss()->choice('test.apples', 1));
|
||||||
|
$this->assertSame('Je tam mnoho jablek', gloss()->choice('test.apples', 2));
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function value_replaces_work_with_choices()
|
||||||
|
{
|
||||||
|
$this->addMessage('apples', 'There is one apple|There are many apples');
|
||||||
|
|
||||||
|
Gloss::value('test.apples', 'One apple|Many apples');
|
||||||
|
|
||||||
|
$this->assertSame('One apple', gloss()->choice('test.apples', 1));
|
||||||
|
$this->assertSame('Many apples', gloss()->choice('test.apples', 2));
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function key_replaces_work_with_choices()
|
||||||
|
{
|
||||||
|
$this->addMessage('apples', '{1} Je tam jedno jablko|[2,*]Je tam mnoho jablek');
|
||||||
|
$this->addMessage('apples_with_0', '{0} Není tam žádné jablko|{1} Je tam jedno jablko|[2,*]Je tam mnoho jablek');
|
||||||
|
|
||||||
|
Gloss::key('test.apples', 'test.apples_with_0');
|
||||||
|
|
||||||
|
$this->assertSame('Není tam žádné jablko', gloss()->choice('test.apples', 0));
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function extend_works_with_choices()
|
||||||
|
{
|
||||||
|
$this->addMessage('apples', '{0} There are no apples|[1,*]There are :count apples', 'en');
|
||||||
|
$this->addMessage('apples', '{0} Není tam žádné jablko|[1,*]Je tam :count jablek', 'cs');
|
||||||
|
|
||||||
|
Gloss::extend('test.apples', fn ($apples, $replace) => $replace($apples, [
|
||||||
|
':count' => '<span class="font-medium">:count</span>',
|
||||||
|
]));
|
||||||
|
|
||||||
|
$this->assertSame('There are no apples', gloss()->choice('test.apples', 0));
|
||||||
|
$this->assertSame('There are <span class="font-medium">2</span> apples', gloss()->choice('test.apples', 2));
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function addMessage(string $key, string $value, string $locale = 'en', string $group = 'test', string $namespace = null): void
|
||||||
|
{
|
||||||
|
$this->addMessages($locale, $group, [$key => $value], $namespace);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function addMessages(string $locale, string $group, array $messages, string $namespace = null): void
|
||||||
|
{
|
||||||
|
/** @var GlossTranslator $translator */
|
||||||
|
$translator = gloss();
|
||||||
|
|
||||||
|
/** @var GlossLoader $loader */
|
||||||
|
$loader = $translator->getLoader();
|
||||||
|
|
||||||
|
$loader->addMessages($locale, $group, $messages, $namespace);
|
||||||
|
}
|
||||||
|
}
|
||||||
23
tests/TestCase.php
Normal file
23
tests/TestCase.php
Normal file
|
|
@ -0,0 +1,23 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Lean\Gloss\Tests;
|
||||||
|
|
||||||
|
use Lean\Gloss\Gloss;
|
||||||
|
use Lean\Gloss\GlossServiceProvider;
|
||||||
|
|
||||||
|
class TestCase extends \Orchestra\Testbench\TestCase
|
||||||
|
{
|
||||||
|
public function setUp(): void
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
|
$this->app->bind('translation.loader', GlossLoader::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getPackageProviders($app)
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
GlossServiceProvider::class,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue