大型网站的 Laravel 本地化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21342767/
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
Laravel Localization for large websites
提问by Steve Bauman
Is there possibly a better way to store large amounts of text for localization in laravel? It would be easy if my entire page was just pure text, but several of my pages have complex layouts and I need to add multiple strings to wrap the text around content such as images/links/media.
有没有更好的方法可以在 Laravel 中存储大量文本以进行本地化?如果我的整个页面只是纯文本会很容易,但是我的几个页面具有复杂的布局,我需要添加多个字符串来将文本包裹在图像/链接/媒体等内容周围。
This is a pain if I ever need to italicize/bold or do any sort of HTML for the text as well as I need to break them into sections to be able to do that.
如果我需要斜体/粗体或为文本做任何类型的 HTML 以及我需要将它们分成几部分才能做到这一点,这会很痛苦。
An example of what I'm using:
我正在使用的示例:
return array(
'exchange_rate' => array(
'title' => 'Exchange Rate',
'p1' => 'Disclaimer',
'p2' => 'Currency rate displayed is subject to change.',
'p3' => 'View All Rates to Date'
),
The first array is the page, the second is the content of the page. I often have to go multiple arrays deeper for more complex layouts such as:
第一个数组是页面,第二个是页面的内容。对于更复杂的布局,我经常需要深入多个数组,例如:
return array(
'exchange_rate' => array(
'title' => 'Exchange Rate',
'p1' => 'Disclaimer',
'p2' => 'Currency rate displayed is subject to change.',
'p3' => 'View All Rates to Date',
'table1' => array(
'title' => 'Currency Table',
'row1' => array(
'l1' => 'Current Rate'
),
'row2' => array('etc')
)
);
Am I doing this right? Is there a better way to format my language files so I can work around the layouts in my views? I'm just curious how large websites manage localization.
我这样做对吗?有没有更好的方法来格式化我的语言文件,以便我可以解决视图中的布局?我只是好奇大型网站如何管理本地化。
Any help is greatly appreciated, thanks!!
非常感谢任何帮助,谢谢!!
EDIT: I'm also aware that you can add placeholders inside your localization arrays such as:
编辑:我也知道您可以在本地化数组中添加占位符,例如:
'title' => ':title'
But adding place-holders for all of my links, images, and media on one page could get messy. Laravel also doesn't support HTML inside the language arrays so I can't just plop in content inside the language files.- Yes it does
但是在一页上为我的所有链接、图像和媒体添加占位符可能会变得混乱。Laravel 也不支持语言数组中的 HTML,所以我不能只在语言文件中插入内容。- 是的,它确实
As it stands now there seems to be two different ways to go here.
就目前而言,似乎有两种不同的方式可以到达这里。
- Continue breaking them into small sections to format text differently.
- Paste the page text into one 'content' array per page, use Waavi to transfer them to the database, and then format them correctly using a WYSIWYG editor on the website itself to format the database entry. (Though this has issues as well because then you can't use blade templating as Lang::get() returns only safe text)
- 继续将它们分成小部分以不同的格式设置文本。
- 将页面文本粘贴到每页一个“内容”数组中,使用 Waavi 将它们传输到数据库,然后使用网站本身的 WYSIWYG 编辑器正确格式化它们以格式化数据库条目。(虽然这也有问题,因为那样你就不能使用刀片模板,因为 Lang::get() 只返回安全文本)
EDIT (Feb 10th 2015):
编辑(2015 年 2 月 10 日):
After lots searching, I've created a package as well that suits my needs. It completely removes the need for any text arrays in laravel. It will automatically add text to the database and translate it to your set locale. Plus, you don't need to manage and decipher dot-notated translation paths.
经过大量搜索,我也创建了一个适合我需求的包。它完全不需要 laravel 中的任何文本数组。它会自动将文本添加到数据库并将其翻译为您设置的语言环境。此外,您无需管理和破译点标记的翻译路径。
回答by Potsky
I have written a package to generate lang files for you. Write your templates, controllers, ... without boring with translations. When you have finished to code, just execute php artisan localisation:missing
and it will generate all needed files in your lang/*/
directories.
我已经写了一个包来为你生成 lang 文件。编写您的模板、控制器,...而不会对翻译感到厌烦。完成编码后,只需执行php artisan localisation:missing
即可在您的lang/*/
目录中生成所有需要的文件。
It will synchronize translations :
它将同步翻译:
- keep the currently used translatations
- create new translatations
- remove or move on bottom obsolete translations
- 保留当前使用的翻译
- 创建新的翻译
- 删除或移动底部过时的翻译
The package is available here : https://github.com/potsky/laravel-localization-helpers
该软件包可在此处获得:https: //github.com/potsky/laravel-localization-helpers
In daily usage, it is the same workflow as po files. Let me know if your life is easier now with translations in Laravel awesome framework !
在日常使用中,它的工作流程与 po 文件相同。让我知道你的生活现在是否通过 Laravel 很棒的框架中的翻译更轻松!
回答by Nik Chankov
Although the author created his own package for translation, I would add another one: laravel-gettext
虽然作者创建了自己的翻译包,但我会添加另一个: laravel-gettext
Coming from CakePHP world where the translation is handled with .po files in my laravel projects I would use the extension above, because:
来自 CakePHP 世界,在我的 Laravel 项目中使用 .po 文件处理翻译,我将使用上面的扩展名,因为:
- I've used to use the syntax, for me it's sort of natural to use it (I have code snippets for creating it)
- GNU Gettext is a translation standard and there are editors for translating the files (PoEdit)
- it's easier to maintain visioning and adding new labels into the file
- 我曾经使用过语法,对我来说使用它是很自然的(我有用于创建它的代码片段)
- GNU Gettext 是一种翻译标准,并且有用于翻译文件的编辑器 (PoEdit)
- 更容易维护愿景并将新标签添加到文件中
The extension is not mine, and I haven't used it so far, but I will in a first occurrence.
扩展不是我的,到目前为止我还没有使用它,但我会在第一次出现。
回答by Nik Chankov
You can always use a Package called Waavi translation
您可以随时使用名为Waavi 翻译 的包
Keeping a project's translations properly updated is cumbersome. Usually translators do not have access to the codebase, and even when they do it's hard to keep track of which translations are missing for each language or when updates to the original text require that translations be revised.
This package allows developers to leverage their database and cache to manage multilanguage sites, while still working on language files during development and benefiting from all the features Laravel's Translation bundle has, like pluralization or replacement.
保持项目的翻译正确更新是很麻烦的。通常翻译人员无法访问代码库,即使他们访问代码库,也很难跟踪每种语言缺少哪些翻译,或者何时对原始文本进行更新需要修改翻译。
这个包允许开发人员利用他们的数据库和缓存来管理多语言站点,同时在开发过程中仍然处理语言文件并受益于 Laravel 的翻译包具有的所有功能,如复数或替换。
回答by Andreas
I don't think there's an ideal solution here, it depends on the size of the project. Perhaps just have different views for each language and then call View::make dynamically - something like this:
我认为这里没有理想的解决方案,这取决于项目的大小。也许只是对每种语言有不同的看法,然后动态调用 View::make - 像这样:
$view = 'pages.'.Lang::getLocale().'.mypage';
if (View::exists($view)) return View::make($page);
else App::abort(404);
回答by Savageman
Well,
好,
Large websites probably use some sort of CMS to allow editing the texts for the different languages.
大型网站可能使用某种 CMS 来允许编辑不同语言的文本。
I'm not sure about Laravel in particular, but you want the user to be able to edit the texts. There's not so much solutions for that you will either need:
我特别不确定 Laravel,但您希望用户能够编辑文本。您需要的解决方案并不多:
- an onlineinterface in your website
- or a script to export/import the translations offline.
- 您网站中的在线界面
- 或离线导出/导入翻译的脚本。
The most common format for this is .pofiles, some softwares even allow you to import / export files containing PHP array directly. This won't work with your format though, because your files are multi-dimensional array.
最常见的格式是.po文件,有些软件甚至允许您直接导入/导出包含 PHP 数组的文件。但是,这不适用于您的格式,因为您的文件是多维数组。
If you go this way, this PHP PoParser classwhich can both read and write .po files.
如果你这样做,这个 PHP PoParser 类可以读取和写入 .po 文件。