在多网站多域设置中翻译每个网站的维护模式
-
13-12-2019 - |
题
在多网站多域设置中翻译每个网站的维护模式
是什么 恰当的 在多网站和多域环境中翻译(本地化)维护页面的方式?
设置为:
- MAGENTO_ROOT/index.php
- MAGENTO_ROOT/uk/index.php
- MAGENTO_ROOT/us/index.php
- MAGENTO_ROOT/somecode/index.php
假设可以通过以下方式访问商店:
- http://MagentoExample.com/index.php (默认)
- http://MagentoExample.uk/ (英国)
- http://MagentoExample.us/ (我们)
- http://MagentoExample.com (一些代码)
我可以很容易地看到一些解决方案,但所有这些似乎都更像是一种解决方法,而不是实际的漂亮、干净的解决方案。
你如何解决这个问题?
解决方案
默认情况下,Magento 不支持错误页面的开箱即用翻译,并且需要进行一些自定义才能实现此类功能。所以技术上不存在 恰当的 这样做的方法。
由于在错误生成期间未初始化完整堆栈,因此正常的转换功能 $this->__('foobar');
在模板中不起作用。
有关如何生成错误页面的一些详细信息可以在 MageBase 上找到:
一种选择是简单地复制 errors/
进入您的语言的每个子目录 uk
, us
, , ETC。并修改模板以反映最终用户的网站入口点的语言。
只需记下您的 index.php
每个视图包含子相对错误文档:
if (file_exists($maintenanceFile)) {
include_once dirname(__FILE__) . '/errors/503.php';
exit;
}
另请注意,所有错误都有一个基本模板,包括 404 page.html
可能有更优雅的解决方案,但因为你已经在复制 index.php
对于不同的视图,多几个文件可能不会太混乱。您还可以修改模板以包含顶级 CSS 和图像以节省一些冗余。
您还可以像在 Magento 标准本地化中一样创建一个语言文件,并使用 process.php
文件,标题和其他一些需要本地化的数据都在那里设置。使用 Varien_File_Csv 读取语言 .CSV 文件的简单示例:
$csvObject = new Varien_File_Csv();
$csvObject->getData($file);
附加说明:由于当前运行时点的堆栈可能不允许 Varien_File_Csv
使用内部 PHP 函数的类包含 获取csv 可能是一个更好的选择。
并解析所需的语言 CSV 文件以填充所需数据 process.php
文件。
另一种选择是简单地添加 Google 翻译或此类第三方工具来自动将错误页面翻译为最终用户的语言。
参考:
其他提示
如前所述,无需翻译维护页面。然而,它有一个解决方法(它具有自己的优势/缺点数量) - 使用一些维护模式扩展,例如这个:
http://www.magentocommerce.com/magento-connect/store-维护.html
它在加载magento堆栈后显示维护模式页面,这意味着您需要具有数据库连接和少数其他东西。因为它也很慢,需要更多的资源。但如果它不是您的问题,它会呈现维护页面完全自定义。
更新:
找到了另一种翻译维护页面的方法:
https://github.com/OpenMage/magento-lts/blob/1.9.3.x/errors/processor.php#L160-L162
if (isset($_GET['skin'])) {
$this->_setSkin($_GET['skin']);
}
维护页面构造函数接受 skin
POST 参数来更改布局。这似乎是有意的方式,但尚未记录在案......
添加一些重写规则到您的
.htaccess
附加一个skin
参数到你的URL。例如。RewriteCond %{HTTP_HOST} ^french.example.com$ RewriteCond %{DOCUMENT_ROOT}/.maintenance.flag -f RewriteCond %{QUERY_STRING} !(^|&)skin=french(&|$) [NC] RewriteRule ^ %{REQUEST_URI}?skin=french[L]
复制
errors/default
到errors/french
- 根据您的需要更改/翻译模板文件
也许有点晚了,但是 好的 工作解决方案,无需复制 error
每个子文件夹的目录...
“缺点”:
- 您必须编辑三个核心文件。为了尽可能避免在核心中进行编辑,我刚刚更改了错误/报告页面和扩展的包含路径
processor.php
读取修改后的local.xml
. - 它仍然需要每种语言的模板文件(目前没有翻译 - 也许稍后)
基本设置
多网站多商店设置 像这样,唯一的区别是我设置了 MAGE_RUN_CODE
在 .htaccess
代替 index.php
. 。对于第一个域,我使用 no RUN_CODE
其他所有看起来都像...
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule .* - [E=MAGE_RUN_CODE:website1]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule .* - [E=MAGE_RUN_TYPE:website]
除了链接的答案之外,我还必须设置 RewriteBase
在 .htaccess
匹配语言环境目录并编辑 index.php
在 en
, fr
并改变
$maintenanceFile = 'maintenance.flag';
...
if (file_exists($maintenanceFile)) {
include_once dirname(__FILE__) . '/errors/503.php';
exit;
}
到
$maintenanceFile = '../maintenance.flag';
...
if (file_exists($maintenanceFile)) {
include_once '../errors/503.php';
exit;
}
编辑 errors/404.php
, 503.php
和 report.php
代替
require_once 'processor.php';
$processor = new Error_Processor();
和
require_once 'processor_multiwebsite.php';
$processor = new Error_Processor_Multiwebsite();
并将其添加到 errors/processor_multiwebsite.php
<?php
require_once 'processor.php';
class Error_Processor_Multiwebsite extends Error_Processor
{
const DEFAULT_RUNCODE = 'default';
const DEFAULT_LOCALE = 'default';
/**
* Magento run code
*
* @var string
*/
protected $_runCode;
/**
* Magento run code
*
* @var string
*/
protected $_locale;
public function __construct()
{
$this->_runCode = self::DEFAULT_RUNCODE;
if (isset($_SERVER['MAGE_RUN_CODE'])) {
$this->_runCode = $_SERVER['MAGE_RUN_CODE'];
}
$this->_locale = self::DEFAULT_LOCALE;
$path = array_filter(explode('/', str_replace('index.php', '', $_SERVER['SCRIPT_NAME'])));
if (end($path)) {
$this->_locale = end($path);
}
parent::__construct();
}
/**
* Retrieve skin URL
*
* @return string
*/
public function getSkinUrl()
{
$baseUrl = str_replace($this->_locale . '/', '', $this->getBaseUrl());
return $baseUrl . self::ERROR_DIR. '/' . $this->_config->skin . '/';
}
/**
* Retrieve skin base URL
*
* @return string
*/
public function getSkinBaseUrl($file)
{
return $this->_config->skin_base ? "../{$this->_config->skin_base}/{$file}" : $file;
}
/**
* Prepare config data
*/
protected function _prepareConfig()
{
parent::_prepareConfig();
$local = $this->_loadXml(self::MAGE_ERRORS_LOCAL_XML);
if (!is_null($local)) {
if ((string)$local->{$this->_runCode}->{$this->_locale}->skin) {
$this->_config->skin = (string)$local->{$this->_runCode}->{$this->_locale}->skin;
}
# add skin base URL
if ((string)$local->{$this->_runCode}->{$this->_locale}->skin_base) {
$this->_config->skin_base = (string)$local->{$this->_runCode}->{$this->_locale}->skin_base;
}
}
}
}
新的 local.xml
结构
而不是设置<skin>
在第一级首先查找网站运行代码/区域设置
<?xml version="1.0"?>
<config>
<!-- 1st domain w/o runcode -->
<default>
<!-- no locale sub dir -->
<default>
<skin>default-default</skin>
...
</default>
<en>
<skin>default-en</skin>
<skin_base>default-default</skin_base>
...
</en>
<fr>
<skin>default-fr</skin>
<skin_base>default-default</skin_base>
...
</fr>
</default>
<!-- runcode website1 -->
<website1>
<!-- no locale sub dir -->
<default>
<skin>website1-default</skin>
...
</default>
...
</website1>
</config>
模板
添加 503.phtml
, ..., CSS 到目录匹配 <runcode>-<locale>
default-default
(第一域默认语言)default-en
default-fr
website1-default
(第二域默认语言)- ...
没有重复的 CSS/图像
- 将您网站特定的 CSS/图像文件放入一个目录并添加
<skin_base>
节点到local.xml
- 更改全部 静止的 链接于
page.phtml
来自即的文件href="css/styles.css"
到<?php echo $this->getSkinBaseUrl('css/styles.css')?>