ofat/laravel-translatable-routes

Laravel可翻译路由的包

v1.0.1 2022-04-28 09:42 UTC

This package is auto-updated.

Last update: 2024-09-28 15:12:46 UTC


README

用于使Laravel路由可翻译的包

Latest Version on Packagist MIT Licensed

此包为您提供了将URL进行翻译的可能性。例如

EN: /en/country/germany, /en/about-us
DE: /de/land/deutschland, /de/uber-uns

对于良好的搜索引擎优化(SEO)非常有用。

安装

  1. 使用Composer安装包
composer require ofat/laravel-translatable-routes 
  1. 将翻译添加到resource/lang中,就像通常的Laravel翻译一样。

resources/lang/en.json

{
    "country": "Country",
    "about-us": "About us"
}

resources/lang/de.json

{
    "country": "Land",
    "about-us": "Uber uns"
}
  1. 在配置文件中定义您的支持的语言区域

config/translatable-routes.php

return [
    'locales' => ['en', 'de']
];

路由

安装包后,您可以在定义路由时使用方括号中的翻译字符串。

要定义具有附加语言前缀的组,您可以使用localeGroup方法并在其中编写所有路由。

Route::localeGroup(function() {
    Route::get('[country]/{country}', function($country) {
    
    });
    Route::get('[about-us]', function(){
        return 'hi!';
    });
});

命名路由

您还可以在localeGroup内部使用路由的名称。它将为每个语言区域创建单独的路由名称。

Route::localeGroup(function(){
    Route::get('[country]/{country}', function($country){
    
    })->name('country')
});

URL生成

通过路由生成URL

您可以使用正常的route函数在当前语言区域生成URL

route('country', ['country' => $country]);

url()->route('country', ['country' => $country])

根据当前语言区域,将创建/en/country/.../de/land/...

如果您需要生成特定语言区域的URL,可以使用routeInLocale方法或在路由名称中添加语言区域

routeInLocale('de', 'country', ['country' => $country]);

url()->routeInLocale('de', 'country', ['country' => $country]);

route('de.country', ['country' => $country]);

通过路径生成URL

您还可以在url函数中使用方括号中的翻译键。但这不会将语言区域前缀添加到您的URL中

url('[country]/belgium')

url()->to('[country]/belgium')

要添加语言区域前缀到您的URL

url()->withLocale('[country]/belgium'); // generates `en/country/belgium`

urlWithLocale('[country]/belgium');

urlWithLocale('[country]/belgium', $params, 'en'); // specify needed locale. generates `de/land/belgium`

语言切换

要生成语言切换的URL,您可以使用命名路由switch-locale

route('switch-locale', 'fr')

所有静态路由默认会切换。但对于带参数的路由,您可以添加策略并定义翻译逻辑

namespace App\Service\UrlTranslator;

use App\Models\Country;
use Ofat\LaravelTranslatableRoutes\UrlTranslator\Abstracts\BaseUrlTranslation;

class CountryUrlTranslation extends BaseUrlTranslation
{
    /**
     * Get current route translated url
     * 
     * @param string $locale
     * @return string
     */
    public function getTranslatedUrl(string $locale): string
    {
        $country = Country::query()
            ->where('url->'.$this->route->getLocale(), $this->route->parameter('country'))
            ->firstOrFail();

        return $this->urlGenerator->route('country', $country->url);
    }

    /**
     * Check if current route is applicable to this strategy
     *
     * @return bool
     */
    public function isApplicable(): bool
    {
        return strpos($this->route->getName(), '.country') > 0;
    }
}

在这种情况下,如果您在页面/en/country/france上尝试切换语言,它将重定向到/fr/les-pays/la-france

isApplicable方法中,您应编写您的逻辑以检查路由是否确定需要您的组。

getTranslatedUrl方法中,您应编写您的逻辑以在新的语言区域生成您的路由的URL。