Git Product home page Git Product logo

Comments (7)

oscarotero avatar oscarotero commented on May 18, 2024

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.

parkerj avatar parkerj commented on May 18, 2024

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.

oscarotero avatar oscarotero commented on May 18, 2024

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.

parkerj avatar parkerj commented on May 18, 2024

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.

gettext

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.

oscarotero avatar oscarotero commented on May 18, 2024

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.

parkerj avatar parkerj commented on May 18, 2024

Ok, I will try generating it again, and then let you know.

from gettext.

parkerj avatar parkerj commented on May 18, 2024

All set. Thanks again for your help.

from gettext.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.