Jump to:

2012 Posts in 1438 Topics by 621 members

Form Questions

SilverStripe Forums » Form Questions » [SOLVED]Translating form labels by translating statics

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 1215 Views
  • javelin
    Community Member
    12 Posts

    [SOLVED]Translating form labels by translating statics Link to this post

    I'm working on a site that is about to be translated into a few different languages and this was very easy to set up with Silverstripe. But I have a question: Is it possible to access the strings (in statics) defined in my model for translation?

    The site uses several different page-types containing forms all generated from calls to getFrontEndFields() on a few different dataobjects and it works very good. Adding, and removing, fields from the dataobjects become very simple: just add a $db-field and write a new $field_labels-value to have it show up everywhere.

    How would I go about preparing this site, the forms, for translation ?
    Do I have to rewrite the forms themselves, foregoing getFrontEndFields()?
    I have found some clues in Dataobject.php with a reference to i18nCollectStatics() but that function doesn't seem to exist. There are some more clues in the forum thread about the Dashboard-module, referencing provideI18nEntities(), but I can't find it actually implemented there.

    /Daniel (working for a non-profit helping exchange student find cheap apartments/rooms)

  • javelin
    Community Member
    12 Posts

    Re: [SOLVED]Translating form labels by translating statics Link to this post

    Found a solution. Apparently this has been prepared for. In DataObject.php the function fieldLabels() which creates the labels already looks for a translation.

    This is not easy to find though since this is not spotted by i18nTextCollector. (There is a reference to a forgotten i18nCollectStatics in DataObject.php but it doesn't appear to exist in 2.3.7)

    If you include a call to provideI18nEntities() on your class formatted as below (in regards to "db") then the proper keys will be created in the lang file and the field labels will be automatically translated. Amazing what useful functions you can find deep within Silverstripe sometimes!

    Replace YOURCLASSNAMEHERE and optionally do the same/duplicate for other static arrays if you need to translate a has_one/has_many or something else. Remember to replace both occurrences of "db" with your static array name.

        function provideI18nEntities() {
           $entities = array();
           foreach($this->stat('db') as $key => $value) {
              $entities["YOURCLASSNAMEHERE.db_{$key}"] = array(
                 'Field labels for the db-fields of this class'
           return $entities;

    More information at http://doc.silverstripe.org/i18n and at http://www.balbus.tk/internationalize/

Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.