Skip to content

Commit

Permalink
Merge pull request #18 from litstack/feat/remove-default-locale-route…
Browse files Browse the repository at this point in the history
…-path

Remove locale path from default locale
  • Loading branch information
jannescb authored Jul 25, 2023
2 parents c9d7156 + 444fdd6 commit f0fe7ed
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 51 deletions.
46 changes: 3 additions & 43 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ jobs:
strategy:
fail-fast: true
matrix:
php: [7.4, 8.0]
laravel: [7.*, 8.*]
php: [8.0]
laravel: [8.*]
stability: [prefer-lowest, prefer-stable]

name:
Expand Down Expand Up @@ -49,44 +49,4 @@ jobs:
composer update --${{ matrix.stability }} --prefer-dist --no-interaction
- name: Run PHP tests via PHPUnit
run: vendor/bin/phpunit
windows_tests:
runs-on: windows-latest
strategy:
fail-fast: true
matrix:
php: [7.4]
laravel: [7.*]
stability: [prefer-stable]
include:
- laravel: 7.*
name: p ${{ matrix.php }} - l ${{ matrix.laravel }} - Windows
steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Cache dependencies
uses: actions/cache@v1
with:
path: ~/.composer/cache/files
key:
dependencies-laravel-${{ matrix.laravel }}-php-${{
matrix.php }}-composer-${{ hashFiles('composer.json') }}

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions:
dom, curl, libxml, mbstring, zip, pdo, sqlite, pdo_sqlite,
bcmath, soap, intl, gd, exif, iconv, imagick, fileinfo
tools: composer:v2
coverage: none

- name: Install dependencies
run: |
composer require "laravel/framework:${{ matrix.laravel }}" --no-interaction --no-update
composer update --${{ matrix.stability }} --prefer-dist --no-interaction
- name: Run PHP tests via PHPUnit
run: vendor/bin/phpunit
run: vendor/bin/phpunit
17 changes: 12 additions & 5 deletions src/Localize/TransRoute.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function getLocale()
return $locale;
}

if (! isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
if (!isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
return $this->getFallbackLocale();
}

Expand Down Expand Up @@ -111,10 +111,17 @@ protected function makeTransRouteForLocale($uri, $action, $locale)
{
$uri = $this->compileUri($uri, $locale);

$route = Route::prefix($locale)
->as("{$locale}.")
->get($uri, $action)
->translator(fn ($locale) => request()->route()->parameters());
if ($locale != $this->getFallbackLocale()) {
$route = Route::prefix($locale)
->as("{$locale}.")
->get($uri, $action)
->translator(fn ($locale) => request()->route()->parameters());
} else {
$route = Route::as("{$locale}.")
->get($uri, $action)
->translator(fn ($locale) => request()->route()->parameters());
}


return $route;
}
Expand Down
6 changes: 3 additions & 3 deletions tests/Localize/LocalizeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ public function it_returns_translated_routes()
}

/** @test */
public function it_adds_locale_route_prefix()
public function it_adds_locale_route_prefix_for_secondary_language()
{
Route::trans('home', fn () => null);

foreach (Route::getRoutes()->getRoutes() as $route) {
$this->assertContains(explode('/', $route->uri)[0], ['de', 'en']);
$this->assertContains(explode('/', $route->uri())[0], ['de', 'home']);
}
}

Expand All @@ -70,7 +70,7 @@ public function test_transle_macro()
Request::setRouteResolver(fn () => $route);

$this->assertStringEndsWith('de/home', route($route->getName()));
$this->assertStringEndsWith('en/home', $route->translate('en'));
$this->assertStringEndsWith('home', $route->translate('en'));
}

/** @test */
Expand Down

0 comments on commit f0fe7ed

Please sign in to comment.