Comments (7)
The translator instance must be created and register just once. It loads all translations, so you don't need the _t
function.
$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.
from gettext.
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
plugin-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.
from gettext.
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
from gettext.
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');
from gettext.
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).
How was created the php array? It must be created using the phpArray generator. For example:
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');
from gettext.
Ok, I will try generating it again, and then let you know.
from gettext.
All set. Thanks again for your help.
from gettext.
Related Issues (20)
- Wrong removing multi line disabled translations HOT 3
- Cannot install version 5 on PHP 8 HOT 1
- Fatal: strlen() expects parameter 1 to be string MoGenerator.php HOT 1
- Update Documentation HOT 1
- Feature Request: WordPress gettext functions support for scanner HOT 2
- testMoGenerator fails HOT 2
- Why PHP >= 7.2 for Gettext v5? HOT 1
- How to load multiple *.mo files (general and for specific textdomain) HOT 3
- Plural parsing for MO files appears to ignore Plural-Forms HOT 2
- Gettext doesn`t extract template literals from JS HOT 1
- PHP 8.1 compatibility: warnings about JsonSerializable compatibility
- Make use of msgctxt HOT 1
- translations findById HOT 2
- Handling of defective .po files HOT 4
- PHP functions prefixed with a slash are being ignored HOT 3
- add a ?? to string reader for php8.1 in /src/Utils/StringReader.php HOT 2
- what is the equivilent of v4 Gettext->register() in v5? HOT 5
- [question] How to store arbitrary key-value payload in Translation objects, now that it can no longer be faked using references? HOT 1
- Scanner for WordPress (provided repo link) HOT 1
- [Question] Simplest way to promote items in a Translations object to a subclass? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gettext.