Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Form Questions /

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

[SOLVED]Translating form labels by translating statics


Go to End


2 Posts   2186 Views

Avatar
javelin

Community Member, 12 Posts

27 May 2010 at 8:23pm

Edited: 01/06/2010 9:18pm

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)

Avatar
javelin

Community Member, 12 Posts

1 June 2010 at 9:12pm

Edited: 01/06/2010 9:18pm

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(
				 $key,
				 PR_HIGH,
				 '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/