Developers

Support translations in special inputs

If your extension contains some special inputs, different from default text inputs or textareas, you can easily support translations in them just by adding n3ttranslate CSS class to them.

Translations

If you are developer, you can easily use n3t Translate in your code.

First of all, you need to get the translation engine instance:

$translator = \Joomla\Plugin\System\n3tTranslate\TranslationEngine\TranslationEngine::getInstance();

Note, that in Joomla 3 namespace is not loaded automatically, you need to load it manually:

\JLoader::registerNamespace('\\Joomla\\Plugin\\System\\n3tTranslate', JPATH_BASE . '/plugins/system/n3ttranslate/src', false, false, 'psr4');

Also note, that because you (hopefully) set the access level for this plugin to something else then 'Public', if your application is not authorized (like in console / cli mode), plugin will not be loaded. In such case, you need to authorize your user manually. In Joomla 3 this could be achieved like this:

$user = \JUser::getInstance((\JUserHelper::getUserId('n3t')));
\JFactory::getSession()->set('user', $user);

After getting the instance of translation engine, you can start to translate:

$germanText = $translator->translate($englishText, 'en-GB', 'de-DE');
echo $germanText;

If your text includes HTML tags, just use 4th optional parameter:

$czechText = $translator->translate($englishText, 'en-GB', 'cs-CZ', true);
echo $czechText;

If you omit source language parameter (pass null), translation engine will automatically decide what language the source is:

$spanishText = $translator->translate($czechText, null, 'es-ES', true);
echo $spanishText;
echo $spanishText->getDetectedLanguage();

Note, that this could lead to mistakes when translating short texts. It is always better to choose the source language. Also note, that some translation engines could return different detected language than requested.

Finally, translate function could throw an exception (for example unsupported language, quota exceeded, unauthorized etc., this differs per translation engine), so you should take care of it:

try {
  $indonesianText = $translator->translate($englishText, 'en-GB', 'id-ID');
} catch (\Exception $e) {
  // handle the exception
}