简体   繁体   English

在Laravel中从数据库转换字符串名称的最有效方法是什么

[英]What is the most effective way to translate string names from database in Laravel

I am currently building a small social network with language select. 我目前正在建立一个带有语言选择功能的小型社交网络。 My problem is what I'm clueless with is i have user groups saved in the database. 我的问题是我一无所知,因为我将用户组保存在数据库中。

id  name    
1   Model   
2   Photographer 
3   Makeup artist

I select these and place it in to my signup form 我选择这些并将其放入我的注册表单

<div class="form-group">
            {{ Form::label('group', Lang::get('auth.group'), array("class" => "col-lg-3 control-label ")) }}
            <div class="col-lg-8">
             <select class="col-lg-12" name="group_id[]" id="" multiple size="6" >
                @foreach($groups as $group)
                    <option value="{{ $group->id }}">{{ $group->name }}</option>
                @endforeach
             </select>
            </div>
        </div>

But I'm totally clueless how to translate these, I am using Laravel's Localization . 但是我完全不知道如何翻译这些内容,我正在使用Laravel's Localization

Could someone please give me a hint about this? 有人可以给我一个提示吗?

If you want to localize string that come from your database, then you have to store the keys into your database tables. 如果要本地化来自数据库的字符串,则必须将键存储到数据库表中。 Then in your views you translate as you will normally do in Laravel. 然后在您的视图中进行翻译,就像通常在Laravel中一样。

A database table will have a the following structure (note that apart from the name you will also store a key): 数据库表将具有以下结构(请注意,除了名称之外,您还将存储键):

-------------------------------------------
| ID | Name          | NameKey            |
===========================================
| 1  | Model         | group.model        |
| 2  | Photographer  | group.photographer |
| 3  | Makeup Artist | group.makeupartist |
-------------------------------------------

The name will not me mandatory, but it is useful when the key are not clearly understandable. 该名称不是强制性的,但是在无法清晰理解键的情况下很有用。

Then your view, instead of rendering the name you will render the key localized: 然后,您的视图将呈现本地化的键,而不是呈现名称

<option value="{{ $group->id }}">{{ Lang::get($group->nameKey) }}

Another suggestions would be using the name itself as the key, but it could problematic when two words that are the same has different meaning in a language, but in another language it is written differently. 另一个建议是使用名称本身作为关键字,但是如果两个相同的单词在一种语言中具有不同的含义,而在另一种语言中其书写方式不同,则可能会出现问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从一开始删除字符串的最有效,最快的方法是什么? (PHP微优化) - What is most effective and fastest way to remove string from beginning? (PHP micro-optimisation) Laravel 4.2中保护视图的最有效方法? - Most effective way of protecting a view in Laravel 4.2? 将AdWords信息同步到本地数据库的最经济有效的方法是什么 - What is the most cost-effective way of synchronising Adwords information to local database 从多对多表中选择所有数据的最有效方法是什么? - What is the most effective way to select all data from many-to-many table? 从中匹配数据的最有效方法是什么 <table > 用PHP? - What's the most effective way to match out data from <table > with PHP? 是否有任何“自动”方法可以从 Laravel 中的数据库中翻译记录? - Is there any 'automatic' way to translate records from database in Laravel? 找出最有效的自定义数据库服务器(PHP)方法 - Figuring out the most effective way for a custom database server (PHP) 高级数据库设计-什么是执行以下任务的最有效方法 - Advanced Database Design - Whats the most effective way to execute the following 保留唯一文章查看次数记录的最有效方法是什么 - what is the most effective way to keep records of number of uniqie article views 在 PHP/MySQL 应用程序中管理时区的最有效方法是什么? - What is the most effective way to manage time zones in a PHP/MySQL application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM