简体   繁体   English

多语言网站的最佳数据库模型

[英]Optimal database model for multilanguage websites

My old database designs was looking like 我的旧数据库设计看起来像

 id | name_en | title_en | name_ru | title_ru ...

I was searching for optimal database structure for multilingual websites where I can add remove languages, posts ... etc without changing database structure quite a long time. 我正在寻找多语言网站的最佳数据库结构,在那里我可以添加删除语言,帖子等,而不会在很长一段时间内更改数据库结构。

Finally created one. 最后创建了一个。 But I'm not sure if it's optimal and it has several fatal problems: 但我不确定它是否是最佳的,它有几个致命的问题:

Language table - it's list of languages for whole application 语言表 - 它是整个应用程序的语言列表

-- ----------------------------
-- Table structure for Language
-- ----------------------------
DROP TABLE IF EXISTS `Language`;
CREATE TABLE `Language` (
  `id` tinyint(3) unsigned NOT NULL AUTO_INCREMENT,
  `iso` varchar(3) NOT NULL,
  `name` varchar(255) NOT NULL,
  `description` varchar(255) NOT NULL,
  `order` tinyint(3) NOT NULL DEFAULT '0',
  `active` tinyint(1) NOT NULL DEFAULT '1',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;

MenuType table - menu types like sidebar menu, top menu ... MenuType表 - 侧栏菜单,顶级菜单等菜单类型...

-- ----------------------------
-- Table structure for MenuType
-- ----------------------------
DROP TABLE IF EXISTS `MenuType`;
CREATE TABLE `MenuType` (
  `id` tinyint(2) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  PRIMARY KEY (`id`,`name`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;

Menu table - All menu items, based on parent child structure. 菜单表 - 所有菜单项,基于父子结构。

-- ----------------------------
-- Table structure for Menu
-- ----------------------------
DROP TABLE IF EXISTS `Menu`;
CREATE TABLE `Menu` (
  `uid` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `id` int(11) unsigned DEFAULT NULL,
  `name` varchar(255) DEFAULT NULL,
  `url` varchar(255) DEFAULT NULL,
  `languageID` tinyint(3) unsigned DEFAULT NULL,
  `menuTypeID` tinyint(2) unsigned DEFAULT NULL,
  `order` int(2) DEFAULT NULL,
  `parent` int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY (`uid`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;

Page table - multilingual pages table 表 - 多语言页表

-- ----------------------------
-- Table structure for Page
-- ----------------------------
DROP TABLE IF EXISTS `Page`;
CREATE TABLE `Page` (
  `uid` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `id` int(11) DEFAULT NULL,
  `name` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `title` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `languageID` tinyint(3) unsigned DEFAULT NULL,
  `content` text COLLATE utf8_unicode_ci,
  `deleted` tinyint(1) DEFAULT NULL,
  `permalink` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  PRIMARY KEY (`uid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

Well my design works like that: lets say our project works on 2 language. 那么我的设计就是这样的:假设我们的项目适用于2种语言。 English (id 1 in language table) and Russian (id 2 in language table). 英语( language表中为id 1)和俄语( language表中为id 2)。 Ever page has 2 records in table: like {uid - 1, id - 2 lang - 1 ...}; {uid - 2 , id - 2, lang - 2 ...} Ever page在表格中有2条记录:like {uid - 1, id - 2 lang - 1 ...}; {uid - 2 , id - 2, lang - 2 ...} {uid - 1, id - 2 lang - 1 ...}; {uid - 2 , id - 2, lang - 2 ...} . {uid - 1, id - 2 lang - 1 ...}; {uid - 2 , id - 2, lang - 2 ...}

I think it will have serious problems because of repeating id's with foreign keys and programmatically will be difficult to maintain it. 我认为它会有严重的问题,因为用外键重复id,并且以编程方式很难维护它。 Any suggestions to fix it or any other design suggestions? 有任何修复建议或任何其他设计建议吗?

Please share your multilingual database design ideas. 请分享您的多语言数据库设计理念。

I'm not experienced in databases and really need some rock solid database design for using long time in projects. 我没有数据库方面的经验,并且确实需要一些坚如磐石的数据库设计才能在项目中长时间使用。

Need some db design that will work with multilingual seo friendly urls, multilingual posts. 需要一些数据库设计,可以使用多语言seo友好的网址,多语言帖子。 etc.. 等等..

Thank you in advance. 先感谢您。

您只需将密钥更改为页面ID和语言ID的组合。

I don't know why you store the page content into database. 我不知道你为什么将页面内容存储到数据库中。 Can the page content be changed through some kind of backend? 可以通过某种后端更改页面内容吗?

I only store language dependent content when it comes from user input. 我只存储来自用户输入的语言相关内容。 In that case I use the same database design as you. 在这种情况下,我使用与您相同的数据库设计。

If the page is static I load the strings from a ini file and then I used the values into a template. 如果页面是静态的,我从ini文件加载字符串,然后我将值用于模板。

The steps would be: 步骤将是:

  • Create the ini files with key, value pairs. 使用键值对创建ini文件。 One per language. 每种语言一种。

strings.en.txt: strings.en.txt:

title = Title
paragraph1 = This is the first paragraph.

strings.es.txt strings.es.txt

title = Título
paragraph1 = Este es el primer párrafo
  • Load the ini file according to user preferences/browser language (at first, you can get that with $_SERVER['HTTP_ACCEPT_LANGUAGE']). 根据用户首选项/浏览器语言加载ini文件(首先,您可以使用$ _SERVER ['HTTP_ACCEPT_LANGUAGE']获取该文件)。

$langs = parse_ini_file("strings.".$_SERVER['HTTP_ACCEPT_LANGUAGE'].".txt");

  • Generate HTML replacing the strings with your variables. 生成用您的变量替换字符串的HTML。 And in my php file I could load/parse into a variable $lang and then when I generate the HTML content I use the proper variable. 在我的php文件中,我可以加载/解析变量$ lang然后当我生成HTML内容时,我使用正确的变量。

    <h1><?php echo $lang['title'];?></h1> <p><?php echo $lang['paragraph1'];?></p>

If you finally go through this solution I recommend you to use some template engine (such smarty or twig). 如果你最终通过这个解决方案,我建议你使用一些模板引擎(如smarty或twig)。 You will have a more much clean code. 你将拥有更加干净的代码。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM