我正在尝试使用Laravel的本地化功能,但我需要能够强调或强化一个短语的一部分.将HTML标记插入语言文件会导致在输出到刀片时将其转义.
例如,这是我的语言文件条目:
return [
'nav' => [
'find' => '<strong>Find</strong> Your Home',
]
];
Run Code Online (Sandbox Code Playgroud)
当我从刀片中调用它时:( 我也试过使用三重括号.)
{{ trans('base.nav.find') }}
Run Code Online (Sandbox Code Playgroud)
它输出:
<strong>Find</strong> Your Home
Run Code Online (Sandbox Code Playgroud)
我可能会将措辞分成如下:
return [
'nav' => [
'fyh' => [
'find' => 'Find',
'yh' => 'Your Home',
]
]
]
Run Code Online (Sandbox Code Playgroud)
然后输出:
<strong>{{ trans('base.nav.fyh.find') }}</strong>{{ trans('base.nav.fyh.yh') }}
Run Code Online (Sandbox Code Playgroud)
但这似乎有点矫枉过正.更好的解决方案?
缺少 [路由:verification.notice] [URI:{语言}/email/verify] 所需的参数
使用本地化后,我将 laravel 电子邮件验证添加到我的项目中。但现在我遇到了 Route:verification.notice 缺少参数的问题。我知道我需要将 app()->getLocale() 参数添加/传递到路线,但找不到位置
我尝试搜索项目中的所有路由和 URL,并检查了 VerificationController.php 和 verify.blade.php。但我没有找到缺少参数的路线。另外,我在网上找不到有同样问题的人。
网页.php
Route::group([
'prefix' => '{language}',
'where' => ['{language}' => '[a-Za-Z]{2}'],
'middleware' => 'SetLanguage',
],
function () {
Route::get('/', function () {
return view('welcome');
})->name('Welcome');
Auth::routes(['verify' => true]);
Route::get('/home', 'HomeController@index')->name('home');
Route::namespace('User')->group(function () {
Route::get('/profile', 'UserController@editProfile')->name('profile');
Route::put('profile', 'UserController@updateProfile');
});
Route::namespace('Admin')->group(function () {
Route::get('/dashboard', 'AdminController@index')->name('dashboard');
});
});
Run Code Online (Sandbox Code Playgroud)
用户控制器
class UserController extends Controller
{
public function __construct()
{
$this->middleware('verified');
}
public function editProfile()
{
$user = User::where('id', Auth()->user()->id)->first(); …Run Code Online (Sandbox Code Playgroud) laravel-localization laravel-blade laravel-authentication laravel-6
这是一个关于方法论和建议做法的问题。我知道它并没有严格地附属于框架,甚至不是 PHP,答案可能是“由你决定”。但我关心的是最佳实践和方法,因为通常存在针对特定上下文的最佳方法。
我想知道这是最好的做法,关键命名的trans()功能Laravel 5.1?
考虑到内置的 Laravel 翻译存储在一个数组中,我关心的是哪种组织结构允许我实现以下目标:
一致性:所以我尽量减少使用不同单词表示相同含义的可能性,或者创建许多最终具有相同翻译的不同键(如常用单词)。
可重用性:所以我尽量减少整体翻译文件的大小并尽可能少翻译,并保持灵活性。
可读性:这样翻译人员即使在缺乏翻译价值的情况下也能识别出key的用途。
组织:因此开发人员可以轻松记住完整的密钥路径,并尽量减少每次检查翻译文件。
举个例子,假设我想为管理配置文件命名用户模型更新的成功警报。可能的方法是:
trans('manager.user.update.alert.sucess')
trans('alerts.success.manager.user.update')
trans('manager.user.alert.update.success')
trans('alert.the_user_was_updated_successfully')
Run Code Online (Sandbox Code Playgroud)
更新
到 2016 年 11 月,看起来Laravel 5.4 正在引入一种基于 JSON 的翻译机制,可以简化翻译文件。尽管如此,关注智能文件结构和组织良好的文本是一个加分项。
我正在使用mcamara/laravel-localization包,但我不知道如何让它与我的单元测试一起工作。以下两个都失败并显示红色:
// 1. This one results in "Redirecting to http://myapp.dev/en"
$this->get('/')->assertSee('My App Homepage');
// 2. This one results in 404
$this->get('/en')->assertSee('My App Homepage');
Run Code Online (Sandbox Code Playgroud)
在浏览器中,http://myapp.dev返回 302 并重定向到http://myapp.dev/en,这很公平。但是,http://myapp.dev/en返回 200。所以这两种情况在前端都能 100% 正常工作,但不能用于单元测试。
但是,我确实有一些自定义功能,这再次在浏览器中起到了很好的作用。
// in web.php
Route::group([
'prefix' => app('PREFIX'), // instead of LaravelLocalization::setLocale()
'middleware' => ['localeSessionRedirect', 'localizationRedirect']],
function() {
Route::get('/', function() {
return view('home');
});
}
]);
// in AppServiceProvider.php
public function boot()
{
// This, unlike LaravelLocalization::setLocale(), will determine …Run Code Online (Sandbox Code Playgroud) 我有一种情况,我必须将我的数组与语言文件同步,所以每次我都必须生成和翻译它。我正在寻找像laravel-langman这样的包,它可以选择同步。但是现在我正在寻找,它不允许我在不要求输入的情况下直接使用工匠推荐创建具有值的键。
任何帮助将不胜感激。
对于可以添加和修改字段的动态表单:
通知
<input name="gallery[1][title]">
<input name="gallery[1][text]">
.
.
.
<input name="gallery[n][title]">
<input name="gallery[n][text]">
Run Code Online (Sandbox Code Playgroud)
在控制器中进行验证:
'gallery.*.file' => 'nullable|image',
'gallery.*.title' => 'nullable|string',
Run Code Online (Sandbox Code Playgroud)
在本地化文件中:
我永远不知道阵列中会有多少个。
'gallery.*.text' => 'text of gallery 1',
'gallery.*.title' => 'title of gallery 1',
Run Code Online (Sandbox Code Playgroud)
我该怎么写呢?
我想要这样的结果:
画廊名称 1
。
。
。
画廊名称 n
php localization laravel-5 laravel-validation laravel-localization
我有一个 Laravel 应用程序,并且有两种不同的语言。\n我的问题是我正在使用queued emails并且仅适用于默认语言,因此我尝试将新的键和值添加到默认语言数组中,以便我可以在两种语言。
问题是无法识别新密钥,让我认为这是缓存情况,但我already clear the cache with artisan commands继续这样做。
现在我删除了我的messages.phplang 文件夹,它仍然考虑旧的密钥。是缓存吗?我该如何解决这个问题?
我把它放在刀片中并适用于默认语言:
\n\n@lang('messages.'. $months->month)\nRun Code Online (Sandbox Code Playgroud)\n\n接下来,我添加新的键,以便我可以使用相同的默认数组语言翻译成另一种语言,并执行以下操作:
\n\n@lang('messages.'. $months->month .'-en')\nRun Code Online (Sandbox Code Playgroud)\n\n我的数组是这样的:
\n\n'Mar\xc3\xa7o' => 'Mar\xc3\xa7o', \n'Mar\xc3\xa7o-en' => 'March',\nRun Code Online (Sandbox Code Playgroud)\n\n但它只识别第一个键。
\n\n谢谢
\nphp laravel laravel-localization laravel-blade laravel-queue
我有这个新的 Laravel 项目要处理。我们希望提供多种语言版本。
我用 JetStream 开始了这个项目。JetStream / Fortify 会自动处理身份验证路由等。然后我添加了https://github.com/mcamara/laravel-localization来处理本地化。它适用于我自己创建的路线:
Route::group(
[
'prefix' => LaravelLocalization::setLocale(),
'middleware' => [ 'localeSessionRedirect', 'localizationRedirect', 'localeViewPath' ]
], function()
{
Route::get('/', function () {
return view('welcome');
});
Route::middleware(['auth:sanctum', 'verified'])->get('/dashboard', function () {
return view('dashboard');
})->name('dashboard');
});
Run Code Online (Sandbox Code Playgroud)
但是如何在 Jetstream 和 Fortify 处理的路由上设置组、前缀和中间件?
[编辑]
因此,在@TEFO 提出一些建议之后,我正在尝试添加一个中间件来处理语言环境的设置。添加 :
强化.php :
'path' => '{lang}',
'middleware' => ['web', 'setLang']
Run Code Online (Sandbox Code Playgroud)
新的中间件 setLang :
class SetLang {
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure …Run Code Online (Sandbox Code Playgroud) localization laravel laravel-localization laravel-8 laravel-jetstream
我已经尝试过这个,但出现错误:
{{ strtolower( @lang('basics.days') ) }}
Run Code Online (Sandbox Code Playgroud)
如何.blade.php在 5.4 中将文件中的本地化字符串转换为小写Laravel?
使用“password.required”等标准符号,我可以为内置验证规则自定义错误消息。但是如何自定义Illuminate\\Validation\\Rules\\Password规则的错误消息呢?
$rules = [\n 'password' => [\n 'required',\n 'confirmed',\n Rules\\Password::min(8)->letters()->mixedCase()->numbers()->symbols(),\n ],\n];\n$messages = [\n 'password.required' => '\xd9\x8a\xd8\xac\xd8\xa8 \xd8\xa7\xd8\xaf\xd8\xae\xd8\xa7\xd9\x84 \xd9\x83\xd9\x84\xd9\x85\xd8\xa9 \xd8\xa7\xd9\x84\xd9\x85\xd8\xb1\xd9\x88\xd8\xb1',\n 'password.confirmed' => '\xd9\x83\xd9\x84\xd9\x85\xd8\xa9 \xd8\xa7\xd9\x84\xd9\x85\xd8\xb1\xd9\x88\xd8\xb1 \xd8\xba\xd9\x8a\xd8\xb1 \xd9\x85\xd8\xaa\xd8\xb7\xd8\xa7\xd8\xa8\xd9\x82\xd8\xa9',\n];\n$request->validate($rules, $messages);\nRun Code Online (Sandbox Code Playgroud)\nmin()如何自定义、letters()等消息?
php localization laravel laravel-validation laravel-localization
我想在用户注册时翻译通知电子邮件,但它始终是发送的默认语言。我有CustomEmailVerificationNotification一个类会覆盖默认的供应商 / SendEmailVerificationNotification。
该项目使用 laravel-localization 包。
protected $listen = [
Registered::class => [
SendEmailVerificationNotification::class,
],
];
Run Code Online (Sandbox Code Playgroud)
<?php
namespace App\Notifications;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Lang;
use Illuminate\Support\Facades\URL;
class CustomEmailVerificationNotification extends Notification
{
/**
* The callback that should be used to create the verify email URL.
*
* @var \Closure|null
*/
public static $createUrlCallback;
/**
* The callback that should be used to build the mail message.
*
* @var \Closure|null
*/
public …Run Code Online (Sandbox Code Playgroud) 我使用Laravel 5。
我尝试
"use Illuminate\Contracts\Routing\Middleware;"
Run Code Online (Sandbox Code Playgroud)
将“中间件”实施为
class Language implements Middleware {
// Some Functions
}
Run Code Online (Sandbox Code Playgroud)
我得到错误,
Interface 'Illuminate\Contracts\Routing\Middleware' not found
Run Code Online (Sandbox Code Playgroud)
实际上是该接口丢失了吗?
(OR)错误定义?
(OR)需要创建| 下载 ?
谢谢Q!
php laravel laravel-middleware laravel-localization laravel-5.2
我正在尝试使用 mcamara 包进行语言翻译,但在根 url 上我收到 404 错误。实际上我正在尝试检测用户的 IP 地址,然后根据该国家/地区设置区域设置。我将区域设置和国家/地区名称存储在数据库中。下面是我的代码:
AppServiceProvider.php在这个文件中,我通过https://github.com/stevebauman/location这个包获取用户 IP 地址,并从数据库检查区域设置并根据该区域设置设置区域设置。
$ip = request()->getClientIp();
$position = Location::get($ip);
$locale = Locale::where('country_code',strtolower($position->countryCode))->first();
if($locale){
LaravelLocalization::setLocale($locale->country_code);
}
Run Code Online (Sandbox Code Playgroud)
网页.php
Route::group([
'prefix' => LaravelLocalization::getCurrentLocale(),
'middleware' => ['localizationRedirect', 'localeViewPath' ]], function(){
Route::get('/',function(){
dd('check');
});
}
Run Code Online (Sandbox Code Playgroud)
我收到 404 错误。
laravel ×10
php ×8
localization ×4
laravel-5 ×3
laravel-5.4 ×2
laravel-8 ×2
blade ×1
laravel-5.2 ×1
laravel-6 ×1
phpunit ×1
translation ×1