OctoberCMS - Rainlab 翻译

OctoberCMS - Rainlab translate

我正在寻找一种方法来为某些主机服务器设置默认区域设置。

假设服务器名称是 server-spain,例如我想默认重定向到西班牙语翻译,最好是用户登陆的任何页面,除了浏览器设置。所以默认情况下,即使应用 language/locale 默认是英语,如果通过 website.es(西班牙语域)访问,用户将看到西班牙语作为默认语言。

$this['servername'] = gethostname(); // host name
{% if servername is same as('server-in-spain') %}
// reload with spanish locale
{% endif %}

有人遇到过这种情况吗?有人解决了吗?

谢谢!

您可以将这段代码添加到您的 layout's code section 中,它应该可以完成工作。

use RainLab\Translate\Classes\Translator;

public function onStart() {
    
    $translator = Translator::instance();
    $currentLocale = $translator->getLocale();
    $newLocale = 'es';
    $translatedRedirect = false;
    $servername = gethostname(); // <- YOUR FUNCTION TO FIND HOST
    
    // MAKE SURE IF YOU DO NOT HAVE GIVEN LOCAE IN Backend
    // THNE IT WILL REDIRECT TO DEFAULT SET LOCALE
    if($servername === 'server-in-spain') {
        $newLocale = 'es';        
                
    }
    if($servername === 'server-in-germany') {
        $newLocale = 'de';    
    }
    
    // we do not want to redirect if user have already perfect locale
    if($currentLocale !== $newLocale) {
        $translatedRedirect = true;
    }
    
    if($translatedRedirect) {
        
        $translator->setLocale($newLocale);
        $currentUrl = $this->currentPageUrl();
        
        $parts = parse_url($currentUrl);
        $path = array_get($parts, 'path');
        
        $pageUrl = http_build_url($parts, [
            'path' => '/' . $translator->getPathInLocale($path, $newLocale)
        ]);

        return Redirect::to($pageUrl);
    }    
}

它应该做的工作

如有疑问请评论

有了哈迪克的回答我就可以解决了。 我在这个解决方案中遇到的唯一问题是:

  1. 我无法 select 给定域中的不同语言环境。即 example.es ,正确显示 /es 语言环境但无法切换语言环境(因为它是代码强制的)

我第一次用 cookie 解决了它:

function onStart() {
        //Set the cookie for firt time visit
        $first_visit = !isset( $_COOKIE["fist_locale"] );

        // Set the cookie so that the message doesn't show again
        setcookie( "first_locale", 1, strtotime( '+1 week' ) );
        if( $first_visit ){ // if user first time
        
            $translator = Translator::instance();
                $currentLocale = $translator->getLocale();
                $newLocale = 'en';
                $translatedRedirect = false;
                $servername = gethostname(); // <- YOUR FUNCTION TO FIND HOST
                
                // MAKE SURE IF YOU DO NOT HAVE GIVEN LOCAE IN Backend
                // THNE IT WILL REDIRECT TO DEFAULT SET LOCALE
                if($servername === 'example.es') {
                    $newLocale = 'es';        
                            
                }
                if($servername === 'example.de') {
                    $newLocale = 'de';    
                }
              
                
                // we do not want to redirect if user have already perfect locale
                if($currentLocale !== $newLocale) {
                    $translatedRedirect = true;
                }
                
                if($translatedRedirect) {
                    
                    $translator->setLocale($newLocale);
                    $currentUrl = $this->currentPageUrl();
                    
                    $parts = parse_url($currentUrl);
                    $path = array_get($parts, 'path');
                    
                    $pageUrl = http_build_url($parts, [
                        'path' => '/' . $translator->getPathInLocale($path, $newLocale)
                    ]);
            
                    return Redirect::to($pageUrl);
                }    
            
            }//end of first time
}
  1. 域名功能强制locale在域名后加/(locale)如示例de/de