With the rise of global audiences, building a website that supports multiple languages has become increasingly important. PHP, a widely-used server-side language, combined with the lightweight and flexible Typecho blogging system, makes it easy to implement multilingual support. This article walks you through how to build a multilingual website using PHP and Typecho, with detailed code examples.
Start by downloading the latest version of Typecho from its official website and uploading it to your server's root directory. Then, access install.php via your browser to launch the installation wizard. Follow the prompts to configure your database and set up an admin account.
Once the installation is complete, log in to the admin panel and navigate to “Settings” → “General” to fill out your site's basic information, including the title and description.
In the root directory of your Typecho installation, create a new folder named langs. Inside this folder, create multiple language files such as:
Each file should return an array containing translation strings. Example:
<?php
return array(
'welcome' => 'Welcome',
'about' => 'About Us',
'contact' => 'Contact Us',
);
To let users switch languages easily, add a language switcher menu in your template file. Here’s a sample snippet:
<ul class="lang-switcher">
<li><a href="<?php $this->permalink(); ?>?lang=zh-CN"<?php if($this->options->lang == 'zh-CN') echo ' class="active"'; ?>>简体中文</a></li>
<li><a href="<?php $this->permalink(); ?>?lang=en-US"<?php if($this->options->lang == 'en-US') echo ' class="active"'; ?>>English</a></li>
<li><a href="<?php $this->permalink(); ?>?lang=ja-JP"<?php if($this->options->lang == 'ja-JP') echo ' class="active"'; ?>>日本語</a></li>
</ul>
The lang parameter is passed via GET request, allowing users to select a language, for example: ?lang=en-US.
To output translated content in templates, use Typecho’s translation function __() like so:
<?php echo __('welcome'); ?>
This function will display the appropriate translation string according to the currently selected language.
In a multilingual website, elements like date formats and currency symbols often need to vary by language. You can use conditional logic in your templates to load the right formatting options, enhancing the overall user experience.
By following the steps outlined in this article, you can enable multilingual support in your Typecho-powered website. PHP makes it easy to define custom language packs and build a flexible language switching mechanism, allowing you to serve global audiences more effectively.
Typecho's open architecture also provides ample room for further customization, such as creating an admin UI for managing translations or implementing auto-detection of browser language. We hope this guide helps you internationalize your website and improve its accessibility for users worldwide.