简体   繁体   English

使用PHP从SQL数据库中选择不同的语言

[英]Selecting different languages from SQL database with PHP

I'm having a 2 issues with my website. 我的网站有2个问题。 It's a multi language one (german, english). 这是一种多语言的语言(德语,英语)。 I always kept the translations in an XML document like this 我总是将翻译保存在这样的XML文档中

<?xml version="1.0" encoding="utf-8"?>
<root>
    <les>
        <en>Lesson</en>
        <de>Lektion</de>
    </les>

Then I converted those XML vars into php ones and echo'd them where needed like this 然后我将这些XML变量转换为php变量,并在需要的地方像这样回显它们

$xml=simplexml_load_file("langindex.xml") or die("xml not found!");
$les = $xml->les->$lang;

<?php echo $les;?>

Problem 1 is that no language is set as default when I open the Website so I always have to select a language first, even though I included that !isset. 问题1是,当我打开网站时,没有语言设置为默认语言,因此即使包含!isset,我也总是必须首先选择一种语言。 If I don't select anything, some images will be streched and it gives me the error that no language is set. 如果我什么都没选择,则将拉伸一些图像,这给我一个错误,即未设置任何语言。

<?php
session_start();
$page = 0;
$lang = $_GET['lang'];
$langArray = array('en','de');
$found = false;

if(in_array($lang, $langArray))
    $found = true;
if(!$found)
    $lang = 'en';

if(!isset($_SESSION['lang']))
    $_SESSION['lang'] = 'en';

if(isset($_GET['lang']) && in_array($_GET['lang'], array('en', 'de')))
    $_SESSION['lang'] = $_GET['lang'];


include '../nihongo/php/dbconnect.php';

My language switch works with a simple <a href="?lang=X"> and that ?lang=X will be shown in the URL as well. 我的语言切换器可以与简单的<a href="?lang=X"> ,并且?lang = X也会显示在URL中。

Enough of that. 够了。 Since XML is pretty inpractical I wanted to switch the whole thing up to an SQL database. 由于XML不太实用,因此我想将整个过程切换到SQL数据库。

I've created a table with the columns ID, german and english and connected the db sucessfully. 我创建了具有ID,德语和英语列的表,并成功连接了数据库。 Now I want PHP to tell SQL to get only the text of the session language with a select like this for example. 现在,我希望PHP告诉SQL仅以这样的选择获取会话语言的文本。

SELECT german FROM texts WHERE ID = '1'
SELECT english FROM texts WHERE ID = '2'

My problem is that I really don't know how to do that with PHP. 我的问题是我真的不知道如何使用PHP做到这一点。 I would think that I'd either have to throw a PHP var into the statement somehow, or select the whole row and let PHP decide which one should be displayed. 我认为我要么必须以某种方式将PHP var放入该语句中,要么选择整行,然后让PHP决定应显示哪一行。 Every advice/suggestion is welcome! 欢迎任何建议/建议! Thanks in advance! 提前致谢!

I like the way OpenCart implemented this, because it's simple and doesn't add additional calls to the DB. 我喜欢OpenCart实现此方法的方式,因为它很简单并且不会向数据库添加其他调用。 Take a look at their implementation. 看一下它们的实现。 You learn a lot investigating and learning from Open Source projects. 您会从开放源代码项目中学到很多调查和学习。

They have a php script per language and module with an array that contains all the strings. 他们每种语言和模块都有一个php脚本,并带有包含所有字符串的数组。 It just include or load the scripts to get access to the strings, for example: en-gb . 它仅包含或加载脚本即可访问字符串,例如: en-gb Loading a string by its key with this method: 使用此方法通过其键加载字符串:

language->get('heading_title');

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

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