简体   繁体   English

PHP使用即时变量从数据库中获取多语言数据

[英]PHP get multilanguage data from database with instant variables

What I would like is to store every word / sentence / ... in a database tabel like so: 我想将每个单词/句子/ ...存储在数据库表中,如下所示:

Table name: 表名:

translate

columns: 列:

id, var, dutch, english, french

Example: 例:

'1', 'titleOne', 'Hallo Wereld!', 'Hello World!', 'Bonjour le monde!'

What I would like is to print the variable for example at the title section: 我想在标题部分打印变量:

<?php
echo $titleOne;
?>

Since there will be hundreds of lines I do not want to set $titleOne = $row['titleOne'] for every line. 由于会有数百行,所以我不想为每行设置$titleOne = $row['titleOne']

Can someone please help me out with a nice query, pull, fetch, array, loop, ... however you call this? 有人可以通过一个不错的查询,拉取,获取,数组,循环等帮助我吗……但是您称呼它为? (or a nice alternative way is also good!) (或者一种不错的替代方法也不错!)

This is plain PHP, no frameworks are used. 这是纯PHP,未使用任何框架。

PS: I am not a PHP expert at all so please try to keep it simple :-)! PS:我根本不是PHP专家,所以请尽量保持简单:-)!

Thanks a lot! 非常感谢!

To answer your question directly. 直接回答您的问题。 You could query your current setup as follows: 您可以按以下方式查询当前设置:

-- For Dutch (pseudocode)
$query = 'SELECT var, dutch FROM translate';

$translations = array();   
while loop ($row = $query) {
    $translations['var'] = $row['dutch'];
}

// Then, to access translations you would:
echo $translations['titleOne'];

BUT you don't want to do this. 但是您不想这样做。 This table structure will lead you down a path of regret. 此表结构将引导您走后悔之路。 Heed our warnings. 注意我们的警告。 There are many ways to go about this and you've chosen to go the SQL route so here are some tips. 有很多方法可以解决此问题,您已选择执行SQL路由,因此这里有一些技巧。

Change your table structure so you don't have to add a new column each time you add a new language. 更改表结构,以便您不必在每次添加新语言时都添加新列。

Table: languages (Add a new row to support a new language) 表格:语言(添加新行以支持新语言)

id, locale, language

1, en_CA, English
2, en_US, English
3, en_UK, English
4, es_ES, Spanish
5, es_MX, Spanish
...

Table: translations (add a new row to add a translation) 表:翻译(添加新行以添加翻译)

id, locale, translation_key, translation

1, en_CA, 'hello', 'Hi from Canada'
2, en_US, 'hello', 'Hi from the US'
3, en_UK, 'hello', 'Hi from the UK'
4, es_ES, 'hello', 'Hi from Spain'
5, es_MX, 'hello', 'Hi from Mexico'
...

translations.locale should be a foreign key pointing back to languages but I'm not sure what your SQL level is so leaving it as clear as I know how. translations.locale应该是指向语言的外键,但是我不确定您的SQL级别是什么,所以就我所知仍然如此。

I assume you can figure out how to query your database from PHP. 我假设您可以弄清楚如何从PHP查询数据库。 What you want to do is: 您想要做的是:

  • figure out which locale to use. 找出要使用的语言环境。 This will be one of the trickiest parts. 这将是最棘手的部分之一。 Ex. 例如 should you be using the one specified in the URL, the session, the browser setting? 您应该使用URL中指定的会话,会话,浏览器设置吗? What happens if none of those is specified? 如果未指定任何内容,该怎么办? What is the fallback locale? 后备语言环境是什么? See my getLanguage() function in https://stackoverflow.com/a/49758067/296555 for some ideas. 有关一些想法,请参见https://stackoverflow.com/a/49758067/296555中的 getLanguage()函数。
  • Pull all translations for that locale and store them in memory. 拉出该语言环境的所有翻译,并将其存储在内存中。 Here is a basic query to pull all translations for a given locale. 这是一个基本查询,用于提取给定语言环境的所有翻译。

     <?pseudocode $locale = 'en_CA'; // Find all translations for a given locale $dbQuery = 'SELECT translation_key, translation FROM translations WHERE locale = :locale'; ; 
  • Put those entries you just pulled in to memory. 将您刚刚拉入的那些条目放入内存中。

     <?pseudocode $translations = array(); while loop ($row = $dbQuery) { $translations[$row['translation_key']] = $row['translation']; } 
  • Throughout your application, you can now access translations via that array. 现在,在整个应用程序中,您都可以通过该数组访问翻译。

    echo $translations['hello'];

You'll want to do this high up in your application in a part that is called on every request. 您将需要在应用程序中的每一个请求中都调用该部分。 Once you have this going you will want to start optimizing. 完成此操作后,您将要开始优化。 You could store the database results to the session so you don't have to query the DB on each page load or implement a more robust form of caching. 您可以将数据库结果存储到会话中,这样就不必在每次页面加载时查询数据库,也不必实施更可靠的缓存形式。

In all honesty, I think you have the right idea but the devil is in the details or so they say. 老实说,我认为您有正确的主意,但魔鬼在细节上,所以他们说。 Read the link that I pointed to. 阅读我指向的链接。 It isn't perfect but it should help you get to a workable solution relatively quickly. 它不是完美的,但是它应该可以帮助您相对快速地找到可行的解决方案。

I 2nd the advice given by others about your table structure, but to answer your question you can use extract 我对别人的表结构提出了第二条建议,但是要回答您的问题,您可以使用提取

$row = array(
    col_1 => 'a', 
    col_2 => 'b', 
    col_3 => 'c' 
)

extract($row);


// results in:

//   $col_1  assigned value 'a'
//   $col_2  assigned value 'b'
//   $col_3  assigned value 'c'

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

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