From f73d6e2dcefd2bf151a7bf7fcd7df0ab92e754fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Sun, 13 Dec 2020 03:05:55 +0100 Subject: [PATCH] Initial commit --- .gitattributes | 9 ++ .github/workflows/ci.yml | 52 ++++++++ .gitignore | 5 + .php_cs.php | 143 +++++++++++++++++++++ composer.json | 27 ++++ phpstan.neon | 37 ++++++ phpunit.xml | 27 ++++ src/Gloss.php | 40 ++++++ src/GlossServiceProvider.php | 33 +++++ src/GlossTranslator.php | 155 +++++++++++++++++++++++ src/helpers.php | 45 +++++++ tests/GlossLoader.php | 10 ++ tests/GlossTest.php | 232 +++++++++++++++++++++++++++++++++++ tests/TestCase.php | 23 ++++ 14 files changed, 838 insertions(+) create mode 100644 .gitattributes create mode 100644 .github/workflows/ci.yml create mode 100644 .gitignore create mode 100644 .php_cs.php create mode 100644 composer.json create mode 100644 phpstan.neon create mode 100644 phpunit.xml create mode 100644 src/Gloss.php create mode 100644 src/GlossServiceProvider.php create mode 100644 src/GlossTranslator.php create mode 100644 src/helpers.php create mode 100644 tests/GlossLoader.php create mode 100644 tests/GlossTest.php create mode 100644 tests/TestCase.php diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..094163d --- /dev/null +++ b/.gitattributes @@ -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 diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..1bdee71 --- /dev/null +++ b/.github/workflows/ci.yml @@ -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) diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..649c3ac --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +.phpunit.result.cache +composer.lock +vendor/ +.php_cs.cache +.vscode/ diff --git a/.php_cs.php b/.php_cs.php new file mode 100644 index 0000000..c7834ee --- /dev/null +++ b/.php_cs.php @@ -0,0 +1,143 @@ + ['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); diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..d874a72 --- /dev/null +++ b/composer.json @@ -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" + } +} diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..0eb9a59 --- /dev/null +++ b/phpstan.neon @@ -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 diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..02e1228 --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,27 @@ + + + + + ./src + + + ./src/routes.php + + + + + ./tests + + + + + + + + + + + + + + diff --git a/src/Gloss.php b/src/Gloss.php new file mode 100644 index 0000000..3c38a1d --- /dev/null +++ b/src/Gloss.php @@ -0,0 +1,40 @@ +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)); + } + } +} diff --git a/src/GlossTranslator.php b/src/GlossTranslator.php new file mode 100644 index 0000000..c555cca --- /dev/null +++ b/src/GlossTranslator.php @@ -0,0 +1,155 @@ +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 + ); + } +} diff --git a/src/helpers.php b/src/helpers.php new file mode 100644 index 0000000..cceae5c --- /dev/null +++ b/src/helpers.php @@ -0,0 +1,45 @@ +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' => ':start', + ':end' => ':end', + ':total' => ':total', + ])); + + $this->assertSame( + 'Showing 10 to 20 of 50 results', + Gloss::get('test.pagination', ['start' => 10, 'end' => 20, 'total' => 50]) + ); + + gloss()->setLocale('cs'); + + $this->assertSame( + 'Zobrazeno 1020 z 50 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' => ':start', + ':end' => ':end', + ':total' => ':total', + ])); + + $this->assertSame( + 'Showing 10 to 20 of 50 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' => ':count', + ])); + + $this->assertSame('There are no apples', gloss()->choice('test.apples', 0)); + $this->assertSame('There are 2 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); + } +} diff --git a/tests/TestCase.php b/tests/TestCase.php new file mode 100644 index 0000000..dd6102e --- /dev/null +++ b/tests/TestCase.php @@ -0,0 +1,23 @@ +app->bind('translation.loader', GlossLoader::class); + } + + protected function getPackageProviders($app) + { + return [ + GlossServiceProvider::class, + ]; + } +}