May 17, 2018

Debugging i18n translation in SilverStripe

The scaffold form generator does a very good job bringing you the models to forms for editing. When using translations, sometimes debugging might help you to find which translation is looked up.

Since there is no way to monkey patch static methods in PHP, I use to "hack" the _t function in the i18n (can be found in vendor/framework/src/i18n/i18n.php):

<?php

class i18n implements TemplateGlobalProvider
{
  …
  public static function _t($entity, $arg = null)
    {
      …
      // this would output directly to your html
      // and can be handy especially on large outputs
      echo ("<!-- $entity => $result -->\n");
      // -or- using the php log without affecting the html output
      error_log($entity.' => '.$result);
      return $result;
}

and voilà - now you'll see all looked up translations.

Besides that, the following example should demonstrate all fields and relations which are translated by the DataObject scaffolding:

de:
  Order:
    SINGULARNAME: Bestellung
    PLURALNAME: Bestellungen
    PLURALS:
      one: 'Eine Bestellung'
      other: '${count} Bestellungen'
    db_Number: Bestellnummer
    db_Comment: Kommentar
    has_one_Client: Kunde
    has_many_Items: Bestellte Ware
    belongs_many_many_Vouchers: Gutscheinen
    many_many_Adresses: Adressen

You can find the official SilverStripe i18n documentation here.