-
-
Notifications
You must be signed in to change notification settings - Fork 133
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Translating Strings #85
Comments
The translator instance must be created and register just once. It loads all translations, so you don't need the $t = new Gettext\Translator();
$t->loadTranslations(APP_PATH . 'lang' . DS . 'it_IT.php');
echo $t->gettext($msgid);
//register to use global functions: __(), n__(), etc...
$t->register();
echo __($msid);
//or if you preffer the _t() function:
function _t($msgid) {
return __($msgid);
} If, however, the strings still being untranslated, maybe they're not included in the array stored in it_IT.php. |
Ok, got it. Now, if it can only be registered once, how can I emulate native gettext to utilize more than one domain: default-it_IT.php Previously, my function was: function _t($msgid, $domain = '')
{
if ($domain !== '') {
return dgettext($domain, $msgid);
} else {
return gettext($msgid);
}
} For the default domain, the $domain variable was null, but if I have a plugin with it's own language directory, then the $domain variable would not be null. The reason I don't want to use native gettext, is because I want the freedom to define the locales instead of having to be chained to /en_US/LC_MESSAGES/ and so forth. From reading the docs and the code, it seems as though multi-domains without using native gettext could be possible. How, could I accomplish this with only having to $register global functions only once? Thanks for your help. |
You can see the TranslatorInterface to know all available functions to get translations (https://github.com/oscarotero/Gettext/blob/master/src/TranslatorInterface.php). All these methods are available also in the global functions (https://github.com/oscarotero/Gettext/blob/master/src/translator_functions.php) $t = new Gettext\Translator();
$t->loadTranslations(APP_PATH . 'lang' . DS . 'default-it_IT.php');
$t->loadTranslations(APP_PATH . 'lang' . DS . 'plugin-it_IT.php');
//Get translation using the default domain (messages)
$t->gettext('hello');
//Get translation using other domain
$t->dgettext('plugin-domain', 'hello');
//Singular-plural translations
$t->ngettext('one comment', 'various comments', $numComments);
//Domain + Singular-plural translations
$t->dngettext('plugin-domain', 'one comment', 'various comments', $numComments);
//etc... To use as global functions:
$t->register();
echo __('hello');
echo d__('plugin-domain', 'hello');
//etc |
Thanks. I am still having some issue with strings translating. I am using your first suggestion without trying to implement multi-domains yet. When using the .php array file, nothing happens (yes the translated strings are in the array). When I instead use the .po file, it echoes the content of the file instead of translating the strings. Have any ideas of what I could be doing to cause such anomalies? Attached is a screenshot. This first bit of code below, whether I use .php or .po returns nothing. The second bit of code, is what the screenshot produces when using .po and returns nothing when using .php. $t = new \app\src\Gettext\Translator();
$translations = \app\src\Gettext\Translations::fromPoFile($path . $domain . '-' . $locale . '.php');
$t->loadTranslations($translations); $t = new \app\src\Gettext\Translator();
$t->loadTranslations($path . $domain . '-' . $locale . '.po'); |
The Translator does not load .po files directly, only load php arrays (internally it does a include, so this is why you can see the content of the file). use Gettext\Translations;
use Gettext\Translator;
//load a .po file
$translations = Translations::fromPoFile('translations.po');
//generate a php array file
$translations->toPhpArrayFile('translations.php');
//Create a translator
$t = new Translator();
//Load the php with the translations
$t->loadTranslations('translations.php');
//use it
$t->gettext('msgid'); |
Ok, I will try generating it again, and then let you know. |
All set. Thanks again for your help. |
I am using the following function to translate strings in an application I created. However, I am not sure if I am using it correctly because the strings are not translating. Below is my function:
I exported the .po file into php arrays. But for some reason, the strings are not being translated into Italian. Am I instantiating the class incorrectly or missing a step?
The text was updated successfully, but these errors were encountered: