表' MySQL 驱动的多语言网站架构
我正在开发多语言网站。尝试为此目的设计最佳的数据库模式。
如您所见,有 2 个表:langs
和 menu
。 我的想法如下:
例如。让我们看一下从 MySQL 表生成多语言导航。在 PHP 后端,从数据库生成导航时
- 从
menu
表的行中获取所有数据 - 左连接第二个表 -
langs
(通过name
字段>菜单表) 并从定义语言的列中获取数据(例如 en、ru)
您认为这是最佳方式还是有更有效的解决方案?请给我与数据库相关的答案,而不是文件。 (例如 gettext,...等)
I'm working on multi-lang website. Trying to design optimal db schema for this purpose.
As you see, there are 2 tables: langs
and menu
. My idea is following:
For ex. lets take a look at multi-language navigation generation from MySQL table. In PHP backend, while generating navigation from database
- Get all data from
menu
table's row - Left join second table -
langs
(byname
field ofmenu
table )
and get data from defined language's column (for ex. en, ru)
How do you think, is it optimal way, or is there more efficient solution? Please, give me database related answers, not file. (for ex. gettext,... etc)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果
langs
表包含一个language
列,并且每个翻译占一行,那就更好了。这允许您稍后添加另一种语言,并且只需应用数据更改(而不需要重新编写查询)。正如您已经暗示的那样,执行左连接并回退到直接保存在
menus
表中的默认语言也是一个好主意(在这种情况下,您不需要保存翻译)例如langs
表中的en
,因为英语版本始终可用)。您可能还需要考虑存储特定于国家/地区的翻译(例如,如果有多个西班牙语国家/地区的翻译可能不同),并遵循查找的后备策略:
语言国家
,<代码>语言,英语
翻译。It would be better if the
langs
table contained alanguage
column, with one row for each translation. This allows you to add another language later and only have to apply data changes (rather than having to re-write queries).As you've already implied, performing a left join and falling back to a default language held directly in the
menus
table is also a good idea (in which case, you don't need to hold a translation for e.g.en
in thelangs
table, since the english version will always be available).You may also want to consider storing country-specific translations (if, say, there are multiple spanish speaking countries where the translations can be different), and following a fallback strategy of finding:
language-country
,language
,English
translations.您可以像这样进一步规范化:
tokenname_id, lang, text
引用字符串的表然后通过 tokenname.id 作为外键引用该字符串。
请注意,您将需要某种迷你模板语言来进行文本本地化。
You can normalize further like so:
tokenname_id, lang, text
Tables that reference strings then refer to the string by tokenname.id as a foreign key.
Note that you will need some kind of mini templating language for text localizations.