在 Laravel 中创建一个多语言站点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16146865/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Starting a multilingual site in Laravel
提问by enchance
I'm about to create a multilingual site where English will be used for the public pages then Arabic for the Admin. I've never worked on this before and am unsure where to begin. Maybe those who've done multilingual sites before can answer a few questions for me.
我即将创建一个多语言站点,其中公共页面将使用英语,然后管理员使用阿拉伯语。我以前从未做过这方面的工作,不确定从哪里开始。也许以前做过多语言网站的人可以为我回答几个问题。
- Can English and Arabic be saved in the same table? If so, my collation has always been utf-8 Unicode, does that mean I can't use utf8 anymore?
- Will the Arabic portions of my site need its own table? If so, what collation?
- In my Views, what do I type in replacement for
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
? - Is there anything I should know about starting a multilingual site?
- 英语和阿拉伯语可以保存在同一个表中吗?如果是这样,我的排序规则一直是 utf-8 Unicode,这是否意味着我不能再使用 utf8 了?
- 我网站的阿拉伯语部分需要自己的表格吗?如果是这样,什么整理?
- 在我的视图中,我输入什么来替代
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
? - 关于启动多语言网站,我应该了解什么?
Thanks in advance! I really am a bit lost on where to begin on this.
提前致谢!我真的有点不知道从哪里开始。
回答by BenjaminRH
- Yes, you can use utf8
- No, you can use both Arabic and English in the same table
- The Arabic charset is
iso-8859-6
, so you'll need to change the charset attribute in the admin views - You probably want to use
body { direction: rtl; }
in your css for the Arabic pages
- 是的,您可以使用 utf8
- 不,您可以在同一张表中同时使用阿拉伯语和英语
- 阿拉伯字符集是
iso-8859-6
,因此您需要更改管理视图中的字符集属性 - 你可能想
body { direction: rtl; }
在你的 css 中使用阿拉伯语页面
You might find these links useful:
您可能会发现这些链接很有用:
When you want to validate form fields using Laravel's translation files (application/language/XX/validation.php
), you will find that the :attribute
placeholder uses the form field's name
attribute, resulting in a mostly-translated error message with the word "email" (or whatever) in English. You can work around this by using the Validation Attributes array to hook up the English field names with the Arabic equivalent. If you find you've already translated them in another language file, you can use the following code instead:
当您想使用 Laravel 的翻译文件 ( application/language/XX/validation.php
)验证表单字段时,您会发现:attribute
占位符使用了表单字段的name
属性,从而产生了一个主要翻译的错误消息,其中包含英文单词“email”(或其他)。您可以通过使用 Validation Attributes 数组将英语字段名称与阿拉伯语等效项连接起来来解决此问题。如果您发现您已经将它们翻译成另一种语言文件,则可以使用以下代码:
'attributes' => array_merge(
Lang::file('application', 'ar', 'filename_without_extension'),
Lang::file('application', 'ar', 'another_file')
),
回答by moa101
By default, Laravel uses the email field for authentication. If you would like to customize this, you may define a username method on your LoginController:
默认情况下,Laravel 使用 email 字段进行身份验证。如果你想自定义这个,你可以在你的 LoginController 上定义一个 username 方法:
public function username()
{
return 'username';
}