dongtiandexue123456 2014-10-13 15:12
浏览 112

laravel翻译自定义后备

I build a multilingual site. Initially, I store the content in the database and I give a UI in browser show that normal admin user can edit those data. Whenever admin adds any string in the database he/she can regenerate all those language files under app/lang/ under the corresponding file from admin panel by just pressing one button.

My table structure is

id | location | key     |hindi   | english |
---------------------------------------
 1 | global   | welcome |namaste | Welcome | 

and use in view file like echo trans('global.welcome');

Now I want to automatically collect data from view file. Suppose I add echo trans('global.hello'); in my view file and it's not present in app/lang/en/global.php so fallback language will call. I want to track that situation so that I can add one row in the database table with hello as a key. I want to add to the database at development time, not in production.

  1. Is it good or any better option?
  2. How to fire my own function in fallback time?
  • 写回答

1条回答 默认 最新

  • duanji8615 2014-11-03 20:12
    关注

    How about having your own translation function (only for development or even for production). In there you could first check if the translation exists and after that call the default trans method. Now the approach if you want to use it only for development or also for production (maybe it would be useful for some additional functionality)

    Development only: Edit the trans function directly in the helpers.php file or override it
    (you'll have to comment the function in helpers.php out)

    Development & Production: Create your own function with its own name

    The code, however, stays the same

    function trans($id, $parameters = array(), $domain = 'messages', $locale = null){
        $translator = app('translator');
        if(!$translator->has($id, Config::get('app.locale')){
            // insert db row
        }
        return $translator->trans($id, $parameters, $domain, $locale);
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。