How to use gettext in your php project
Here is how to make your site multilingual using gettext and php.
Concept is simple, default language is “en”, and language is changed based on subdomain.
For example fr.domain.com would load french localization.
1 2 3 4 5 6 7 8 9 10 11 |
// ** Languages ** // $language = "en"; list($sub) = explode(".", "http://" . $_SERVER["HTTP_HOST"], 2); if ($sub != SITE_DOMAIN) { $language = $sub; } putenv("LANG=$language"); setlocale(LC_ALL, $language); $domain = "messages"; bindtextdomain($domain, "/var/www/project/languages"); textdomain($domain); |
Directory structure (languages is located in the project root):
1 2 3 |
languages/fr/LC_MESSAGES/ languages/ge/LC_MESSAGES/ languages/es/LC_MESSAGES/ |
Each language LC_MESSAGES directory should contain .mo and .po.
And in php files you need to change all texts that need to be translatable into:
1 2 3 |
<?php _("some text"); ?> or <?php gettext("some text"); ?> |
Leave a Reply