doulao1934 2014-04-04 11:31
浏览 54
已采纳

本地化数据库结果

I have developed a simple application form that I serve in two languages. I'm also populating dropdown menus from the database and I'm struggling with how to present that data in two languages. The data is not very dynamic and yet time to time new entries might be added so manual work won't help me. I'm also pulling different number of rows for the different cases. I'm confused, I think unless I have two databases or add the translation to the database and present it with the original text, I have no other options. Or a new column in the database that will hold the tranlation and then pull the correct column according to the locale chosen... Any ideas would be appreciated, thanks!

  • 写回答

1条回答 默认 最新

  • dongyi1015 2014-04-04 14:37
    关注

    The way I see it, the best option in your case would be creating a separate table for storing translations of each record.

    For example, entry like a blog post would have some fields that are common in all site languages, but things like a title, content and slug would be locale specific. When user requests the information, all the common information would be retrieved from 'posts' table, while the locale specific translations would be retrieved from the 'posts_translations' table.

    This way you wouldn't have to manually add column for every translation. While technically that would work just fine, you would run into problems, if you ever had to add another language(s). If translations are stored in separate tables, that is no longer a problem.

    Here's an example how that might look when using Eloquent ORM:

    public function getPosts()
    {
        $language = App::getLocale();
        $posts = Post::with([
            'translation' => function ($query) use ($language) {
                    $query->where('language', '=', $language);
            }
        ])->get();
        return $posts;
    }
    

    Alternatively, the same method using Laravel's Query Builder:

    public function getPosts()
    {   
        $language = App::getLocale();
        $posts = DB::table('posts')
        ->leftJoin('posts_translations', 'posts.id', '=', 'posts_translations.post_id')
        ->where('posts_translations.language', '=', $language)
        ->get();
        return $posts;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 对于知识的学以致用的解释
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败