What to use for localization (php/smarty/pear application)?

99封情书 提交于 2019-12-01 09:34:27

问题


I have a php/smarty/pear app that has it's own localization implementation that involves using ids for strings and the strings are stored in a db. currently the app is only in English but I will start adding other languages. The current implementation is very poor and basically I'm not a big fan of re-inventing the wheel.

Can anybody recommend what to use for localization? I had used gettext years ago, is that still used or is there something newer and better ?

thanks in advance.


回答1:


I would use gettext, since it's a mature system and provides features like singular/plural versions of translations. To be not dependant on the availability of the php extension, you should fall back to http://launchpad.net/php-gettext which provides a pure php implementation of gettext.

gettext has also the big advantage that dozens of tools exist that make it easy to translate those files.




回答2:


My best advice is to look at how other apps (Drupal, Joomla, Wordpress) handle localization, and implement something similar.

A few of the apps I have delved into have a single ini file for each language that contains STRING-ID="String Value" definitions, where the STRING-ID is similar to a PHP CONSTANT name. These files are then loaded by a localization class, which has functions that handle setting the language to be used (based on a config value or user setting), loading the file, and handle the translation of the STRING-ID into the String Value. You would end up using it like this:

<?php
// config.php
$lang = "DE";

// languages/de.ini
HELLOWORLD="Hallo Welt"
OTHERSTRING="Anderer String"

// example.php
require_once("classes/localization.class.php");
$localize = localization::getInstance();
echo $localize->translate("HELLOWORLD");
?>

This should produce something like Hallo Welt.



来源:https://stackoverflow.com/questions/3852242/what-to-use-for-localization-php-smarty-pear-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!