doulei2100 2015-03-05 15:05
浏览 18
已采纳

Laravel 5字段名称本地化

I have a system where I do translations in the same mysql record but in different fields.

Example:

title_en varchar(255)
title_de varchar(255)
title_fr varchar(255)

Of course there are also fields for content and seo like text_en, description_en etc.

What I do now to use the collection data in my blade template is this:

<h1>{{$page->{'title_' . App::getLocale()} }}</h1>
{!! $page->{'text_' . App::getLocale()} !!}

This works but is it the way to do it? What are best practices?

  • 写回答

1条回答 默认 最新

  • dongzhu3548 2015-03-05 15:12
    关注

    I haven't created page with translations in Laravel yet, but If I were you, I would create helper method for this and I would use something like that in my template:

    <h1>{{ trtx('title') }}</h1>
    {!! trtx('text') !!}
    

    This way, you would have quite clean code in your template without hardcoding way of creating labels.

    Just a sample implementation for this:

    1. Create app/helpers/functions.php file:

      <?php
      
      function trtx($title, $locale = null)
      {
          if ($locale == null) {
              $locale = App::getLocale();
          }
      
          $trans = App::make('App\Services\Translator');
      
          return $trans->get($title . '_' . $locale);
      }
      
    2. Create app/Services/Translator.php file:

      <?php
      
      namespace App\Services;
      
      class Translator
      {
          public function get($text)
          {
              return "This is translated text for " . $text;
          }
      }
      
    3. In app/Providers/AppServiceProvider.php in register function add:

          $this->app->singleton(
              'App\Services\Translator',
              'App\Services\Translator'
          );
      
    4. In composer.json file add to autoload section loading your helper file, so it should look like this:

      "autoload": {
          "classmap": [
              "database"
          ],
          "psr-4": {
              "App\\": "app/"
          },
        "files": [
          "app/Helpers/functions.php"
        ]
      },
      
    5. Run in console composer dump-autoload and then run in console php artisan clear-compiled

    Now it should work, if you put in your template for example:

    {{ trtx('title') }} {{ trtx('title', 'fr') }}
    

    you will get:

    This is translated text for title_en This is translated text for title_fr 
    

    Of course now you should implement in Translator class the real way you get translation from database. Of course there is no point to use DB for translations all the time. You should cache table where you have translations because if you will run query each time you want to display translation it will be a waste of DB performance.

    EDIT

    I don't know if I understand you well, but probably you need to translate property for record you already have.

    You could modify then trtx function a bit and use it this way:

    {{ trtx($page, 'title') }} {{ trtx($page, 'title', 'fr') }}
    

    (in this case you don't need to use Translate at all)

    But if I use Eloquent (you haven't mentioned about it), you could do it also in a bit cleaner way. For your Page model you could add accessors for title and text properties (you must not have title or text columns in your table in database):

    public function getTitleAtrtribute() 
    {    
        return $this->{'title_'.\App::getLocale()};
    }
    public function getTextAtrtribute() 
    {    
        return $this->{'text_'.\App::getLocale()};
    }
    

    and now in your Blade template you could use just:

    <h1>{{ $page->title }}</h1>
    {!! $page->text !!}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题