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

Fix/use package default locale #1

Merged
merged 5 commits into from
Apr 29, 2024
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
2 changes: 1 addition & 1 deletion .github/workflows/phpstan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.0'
php-version: '8.2'
coverage: none

- name: Install composer dependencies
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
fail-fast: true
matrix:
os: [ubuntu-latest, windows-latest]
php: [8.0]
php: [8.2, 8.3]
stability: [prefer-lowest, prefer-stable]

name: P${{ matrix.php }} - ${{ matrix.stability }} - ${{ matrix.os }}
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"phpstan/extension-installer": "^1.1",
"phpstan/phpstan-deprecation-rules": "^1.0",
"phpstan/phpstan-phpunit": "^1.0",
"tightenco/jigsaw": "^1.4"
"tightenco/jigsaw": "^1.7"
},
"autoload": {
"files": [
Expand Down
23 changes: 10 additions & 13 deletions src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

/**
* @param mixed $page
* @param string $text
* @param ?string $current_locale
* @return string The translated text if found, else returns the same given $text
*/
Expand All @@ -29,8 +28,6 @@ function current_path_locale($page): string
{
$path = trim($page->getPath(), '/');

$default_locale = $page->defaultLocale ?? packageDefaultLocale();

/**
* - [a-z]{2,3} language code
* - [A-Z]{2} region code
Expand All @@ -41,12 +38,12 @@ function current_path_locale($page): string

preg_match($locale_regex, $path, $matches);

return $matches['locale'] ?? $default_locale;
return $matches['locale'] ?? packageDefaultLocale();
}

/**
* @param mixed $page
* @param ?string $target_locale set to the default locale if null
* @param ?string $target_locale set to the default locale if null
* @return string Places $target_locale code in the current path
*/
function translate_path($page, ?string $target_locale = null): string
Expand All @@ -56,19 +53,19 @@ function translate_path($page, ?string $target_locale = null): string
$current_locale = current_path_locale($page);

$partial_path = match (true) {
$current_locale === $page->defaultLocale => $page->getPath(),
$current_locale === packageDefaultLocale($page) => $page->getPath(),
default => substr($page->getPath(), strlen($current_locale) + 1),
};

return match (true) {
$target_locale === $page->defaultLocale => "{$partial_path}",
$target_locale === packageDefaultLocale($page) => "{$partial_path}",
default => "/{$target_locale}{$partial_path}",
};
}

/**
* @param mixed $page
* @param ?string $target_locale set to the default locale if null
* @param ?string $target_locale set to the default locale if null
* @return string Places $target_locale code in the current url
*/
function translate_url($page, ?string $target_locale = null): string
Expand All @@ -78,8 +75,8 @@ function translate_url($page, ?string $target_locale = null): string

/**
* @param mixed $page
* @param string $partial_path A path without the language prefix
* @param ?string $target_locale uses the default locale if null
* @param string $partial_path A path without the language prefix
* @param ?string $target_locale uses the default locale if null
* @return string A path on the target locale
*/
function locale_path($page, string $partial_path, ?string $target_locale = null): string
Expand All @@ -89,15 +86,15 @@ function locale_path($page, string $partial_path, ?string $target_locale = null)
$partial_path = '/'.trim($partial_path, '/');

return match (true) {
$target_locale === $page->defaultLocale => $partial_path,
$target_locale === packageDefaultLocale($page) => $partial_path,
default => "/{$target_locale}{$partial_path}"
};
}

/**
* @param mixed $page
* @param string $partial_path A path without the language prefix
* @param ?string $target_locale uses the default locale if null
* @param string $partial_path A path without the language prefix
* @param ?string $target_locale uses the default locale if null
* @return string A URL on the target locale
*/
function locale_url($page, string $partial_path, ?string $target_locale = null): string
Expand Down
4 changes: 2 additions & 2 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Tests;

use PHPUnit\Framework\TestCase as BaseTestCase;
use TightenCo\Jigsaw\Container;

// use TightenCo\Jigsaw\File\ConfigFile;

Expand All @@ -14,8 +15,7 @@ protected function setUp(): void
{
parent::setUp();

require __DIR__.'/../vendor/tightenco/jigsaw/jigsaw-core.php';
$this->app = $container;
$this->app = new Container;

// $this->app->bind('config', function ($c) use ($cachePath) {
// $config = (new ConfigFile($c['cwd'] . '/config.php', $c['cwd'] . '/helpers.php'))->config;
Expand Down
5 changes: 0 additions & 5 deletions tests/_Mocks/PageMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ class PageMock

public array $localization = [];

public function __construct()
{
$this->defaultLocale = packageDefaultLocale();
}

public function setPath(string $path): static
{
$this->path = '/'.trim($path, '/');
Expand Down
Loading