dongmi6102 2015-06-26 13:06
浏览 15
已采纳

如何在Drupal 7中以另一种语言显示注册页面,保持其自然行为?

I need to show my registration page in another language, keeping this separate from his natural behaviour. In particular this page must be showed in English on the page of the site it owns to, but i need to show it in Italian on another site into an iframe. Now, common sense tells me i need a page

www.example.com/user

and a

www.example.com/it/user

and i tried to achieve this by "admin/config/regional/language/configure" and the option "Determine the language from the URL (Path prefix or domain)." and all works fine, but, for some reason, as i activate this option, ALL my site pages get a "/en/" in their url, breaking a lot of things into the site (that is extremely complex). There's a way to achieve this behaviour for the registration page only? (be able to call a page www.example.com/it/user to show registration in italian and having www.example.com/user and all other pages in the site work without any changes?)

  • 写回答

2条回答 默认 最新

  • douzhongqiu5032 2015-06-26 14:17
    关注

    My solution would be a small custom module.

    Pros:

    • Easy to tweak and install
    • Lightweight, code based so you can just check it into VCS and deploy
    • Can be extended for other language sets

    Cons:

    • Not scaleable.
    • Requires a dev if you want to change the text

    .info file:

    name = User Registration Languages
    description = Alter the user registration form to present alternative translations
    core = 7.x
    

    .module file

    <?php
    
    /**
     * implements hook_form_FORM_ID_alter()
     * https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_form_FORM_ID_alter/7
     *
     * @param $form
     * @param $state
     */
    function userreglang_form_user_register_form_alter(&$form, &$state) {
    
      // the arg() function returns a path component (zero based index)
      // https://api.drupal.org/api/drupal/includes%21bootstrap.inc/function/arg/7
      // in this case we're looking for:
      // user/register/{language_arg} e.g user/register/it
      $lang = arg(2);
    
      // uncomment the following if you want take a look at the form array prior to modification
      // be sure to  temporarily give the anonymous user role the ability to "Access developer information"
      /*if(module_exists('devel')) {
        dpm($form);
      } // */
    
      if($lang && $map = _userreglang_languages($lang)) {
        $form['account']['name']['#title'] = $map['name'];
        $form['account']['name']['#description'] = $map['name_desc'];
        $form['account']['mail']['#title'] = $map['mail'];
        $form['account']['mail']['#description'] = $map['mail_desc'];
        $form['actions']['submit']['#value'] = $map['button'];
      }
    
      // uncomment the following if you want take a look at the form array after modification
      /*if(module_exists('devel')) {
        dpm($form);
      } // */
    }
    
    
    /**
     * helper function for different language maps you might have for the user registration form
     *
     * @param $lang
     * @return array|null
     */
    function _userreglang_languages($lang) {
    
      $map = [
        'it' => [
          'name'      => 'Your name, yo!',
          'name_desc' => 'Input your username fool!',
          'mail'      => 'Email, you gots it?',
          'mail_desc' => 'Seriously do you not know what email is?',
          'button'    => 'Click it baby!',
        ]
      ];
    
      if(isset($map[$lang])) {
        return $map[$lang];
      }
      else {
        return null;
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动