gpt4 book ai didi

php - Cakephp 3.x - 国际化整页

转载 作者:可可西里 更新时间:2023-10-31 22:41:53 24 4
gpt4 key购买 nike

我正在使用 cakephp 3.x,我想将我的网站翻译成多种语言。我阅读了有关 cakephp 3.x 国际化 ( http://book.cakephp.org/3.0/en/core-libraries/internationalization-and-localization.html ) 的文档,它只解释了如何翻译单词和句子,而不是整页。

翻译整页文本的最佳解决方案是什么?我想做的是为每种语言创建一个页面,并在 beforeFilter 函数中选择正确的页面。这是一个好的解决方案吗?

我也在问如何翻译保存在数据库中的某些段落,例如我的新闻页面保存在数据库中,我想用不同的语言翻译我的新闻。我必须为数据库中的每种语言添加一行还是有更好的解决方案?

谢谢你的时间

丹妮

最佳答案

Internationalization & Localization有两个基本任务,在这两种情况下,您在运行时更改语言所需要做的唯一事情就是 call I18n::locale例如:

use Cake\I18n\I18n;
...
public function beforeFilter()
{
if (should show german) {
I18n::locale('de_DE');
}
}

设置完成后,无需额外步骤即可拥有多语言网站。务必set the default locale在 Bootstrap 文件中,因为如果在运行时未覆盖语言环境,则会使用它。

静态内容

think to do is to have a page for each language and in the beforeFilter function, select the correct page. Is it a good solution ?

没有。

静态文件,除非您打算为每种语言或类似的页面布局完全不同,否则通常是使用 translate functions 的单个模板。和 po 文件。

来自文档:

Below is an example of some code for a single-language application:

<h2>Popular Articles</h2>

To internationalize your code, all you need to do is to wrap strings in __() like so:

<h2><?= __('Popular Articles') ?></h2>

涉及的步骤更多(请参阅文档),但对默认语言文本使用双下划线方法可以让您的应用程序准备好用于多种语言。

动态内容

数据库内容通常使用 the translate behavior 翻译.

此行为将使用该行为的所有表的所有翻译内容存储在单独的表中。在运行时会自动检索翻译。举例说明:

$articles = TableRegistry::get('Articles');

I18n::locale('eng');
$article = $articles->get(12);
echo $article->title; // Echoes 'A title'

I18n::locale('spa');
$article = $articles->get(12);
echo $article->title; // Echoes 'Un titulo'

综合考虑

同时使用静态翻译和翻译行为,您的模板文件将如下所示:

<h1><?= h($article->title) ?></h1>
<?= $article->body ?>
<p><?= __('Did you like that article? Let us know') ?></p>

请注意,除了使用双下划线方法外,它与您对单语言应用程序的期望完全相同。

关于php - Cakephp 3.x - 国际化整页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31716409/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com