Skip to navigation

How To Enable Default Value Translations in Joomla! Plugins

Written by and published on

Joomla! allows you to give translatable default values in extension forms, but it only works if you specify that the default value must be translated. This can be done by setting the translate_default attribute to true for an element.

  type="text" default="SMR_CCO_FIELDSET_DEFAULT_TITLE"
  translate_default="true" />

When the form is loaded, Joomla! checks if this attribute is true and if the value of the field is strictly null. If it is, the value is translated. If it’s not, the value is displayed as it is.

 * Get the value for the form field if not set.
 * Default to the translated version of the 'default' attribute
 * if 'translate_default' attribute if set to 'true' or '1'
 * else the value of the 'default' attribute for the field.
if ($value === null)
  $default = (string) $element['default'];

  if (($translate = $element['translate_default']) && ((string) $translate == 'true' || (string) $translate == '1'))
    $lang = JFactory::getLanguage();

    if ($lang->hasKey($default))
      $debug = $lang->setDebug(false);
      $default = JText::_($default);
      $default = JText::_($default);

  $value = $this->getValue((string) $element['name'], $group, $default);

Unfortunately, when a plugin is installed its parameters are inserted into the database at the same time and the default values are set as stated in the form XML. In the case of fields that have translatable default values, those values become the raw language string. This means that when the form is loaded for the first time the field has a value that is not null and therefore it’s not translated.

The only way seems to be to reset the values during installation with the installation script.

defined( '_JEXEC' ) or die( 'one thousand deaths' );

class PlgcontentmypluginInstallerScript
   * Setup the plugin.
   * The database entry is modified, because when the plugin is installed, the
   * params field in the database is set to have the translation string as the
   * default value. However, Joomla! translates default values defined in the
   * form XML only if the default value is strictly null. So unless we reset it,
   * no translation will occur.
   * @param  object  $parent
  public function install( $parent ) {
    $defaultParams = [
      'translatable_field' => null,
      'some_other_field'   => ""

    $db = JFactory::getDbo();
    $query = $db->getQuery( true );

    $query->update( '#__extensions' );
    $query->set( 'params = ' . $db->quote( json_encode( $defaultParams ) ) );
    $query->where( 'element = ' . $db->quote( 'myplugin' ) );
    $query->limit( 1 );

    $db->setQuery( $query );

You could also empty the options completely; that apparently works also.


Commenting has been disabled until I get a proper spam protection working. =(

External Links

Back to beginning