Home > Article > Backend Development > How to create a multilingual website using PHP and CGI
How to use PHP and CGI to create a multilingual website
In today's era of globalization, many websites need to support multilingual functions to adapt to the needs of different users. In this article, we will introduce how to use PHP and CGI to create a multilingual website and show some sample code.
First, we need to determine the supported languages. Before creating a website, we need to collect translated texts in various languages and store them in files related to the website. A common practice is to create a translation folder that contains translation files for each language. For example, we can create a folder named "lang" and store files such as "en.php", "fr.php", and "es.php" in it, representing English, French, and Spanish respectively.
Next, we need to use PHP to load the relevant translation files. We can use "$_SERVER['HTTP_ACCEPT_LANGUAGE']" to get the user's browser preference language setting and decide which translation file to load based on that setting. The following is a sample code:
$availableLanguages = ['en', 'fr', 'es']; // 支持的语言种类 $defaultLanguage = 'en'; // 默认语言 // 获取浏览器偏好语言设置 $browserLanguages = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']); // 根据偏好语言设置选择合适的语言 $selectedLanguage = $defaultLanguage; foreach ($browserLanguages as $browserLanguage) { $language = substr($browserLanguage, 0, 2); if (in_array($language, $availableLanguages)) { $selectedLanguage = $language; break; } } // 加载翻译文件 require_once "lang/$selectedLanguage.php";
In the above code, we first define the supported language types and the default language. We then split the browser preference language settings into an array and iterate over the array to select the appropriate language. Finally, we use the "require_once" function to load the corresponding translation file.
After loading the translation file, we can replace the text in the website by using a simple translation function. The following is a sample code:
function getTranslation($key) { global $translations; return isset($translations[$key]) ? $translations[$key] : $key; } // 使用示例 echo getTranslation('welcome_message');
In the above code, we define a function named "getTranslation", which accepts a key name as a parameter and returns the corresponding translation text. We use a global variable called "$translations" to store the translated text. By setting the corresponding key-value pairs in the translation file, we can easily replace the text in the current language with the translated text.
In addition, if we want users to be able to freely choose the language, we can provide a drop-down menu or button for language switching in the website. When the user selects a language, we can use PHP's "$_SESSION " or "$_COOKIE" to store the user's selection. We can then use a similar method to load the translation file for the corresponding language and display the translated text.
In summary, it is not difficult to create a multilingual website using PHP and CGI. By collecting the translated text and storing it in relevant files, we can use PHP to load and replace the text in the website. By providing the option of language switching, we also give users the freedom to choose the language they want. I hope this article helped you create a great multilingual website!
Note: This article only provides a basic implementation example and does not consider details and performance optimization. In actual projects, it is recommended to delve into and use mature multilingual solutions.
The above is the detailed content of How to create a multilingual website using PHP and CGI. For more information, please follow other related articles on the PHP Chinese website!