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.

All other Modules /

Discuss all other Modules here.

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

Translatable: Can´t set $translate_excluded_fields from config.yml


Go to End


2 Posts   1594 Views

Avatar
sebastiand

Community Member, 5 Posts

4 March 2016 at 9:56pm

Edited: 04/03/2016 10:07pm

I want to exclude a field from being translated ('ChildPage' from lumberjack module).
I saw that there is a private static variable in Translatable.php called $translate_excluded_fields.
This variable is marked with @config, so I guess it can be set by YAML config.

I added the following lines to my mysite/_config/config.yml:

Translatable:
  translate_excluded_fields:
    - ChildPages

After running a /dev/build?flush=1 and accessing the CMS the field still gets translated. So obviously Translatable isn´t picking up my config.
When adding 'ChildPages' directly to the variable in class Translatable.php everything works as expected, and 'ChildPages' won´t get translated in the CMS:

/**
	 * Exclude these fields from translation
	 *
	 * @var array
	 * @config
	 */
	private static $translate_excluded_fields = array(
		'ViewerGroups',
		'EditorGroups',
		'CanViewType',
		'CanEditType',
		'NewTransLang',
		'createtranslation',
		'ChildPages'
	);

I am using Silverstripe v3.3.1 and Translatable v2.1.1

What am I doing wrong? Is this a bug or do I need to set the config variables differently?

Avatar
sebastiand

Community Member, 5 Posts

9 March 2016 at 7:22pm

Edited: 09/03/2016 7:25pm

I solved this one with the help of the Issuetracker on github (Always worth to visit if you have any issues with a Add-On)

You need to set the $translate_excluded_fields on the object that is getting translated, not on Translatable itself.
This makes sense of course, because I can control which fields are translated on a per-object-basis instead of globally setting the fields.

Let´s say we have Page.php which has a custom field ChildPages and we want to exclude this field from translation.
We can do this via mysite/config/_config.yml:

Page:
  translate_excluded_fields:
    - ChildPages

or directly in our Page class:

    class Page extends SiteTree {
    
        private static $db = array(
            'ChildPages' => 'Text', 
        );
    
        private static $translate_excluded_fields = array(
            'ChildPages'
        );
    
        function getCMSFields() {
            $fields = parent::getCMSFields();
    
            // Add fields as usual
            $additionalField = new TextField('ChildPages');
            $fields->addFieldToTab('Root.Main', $additionalField);
    
            // Apply Translatable modifications
            $this->applyTranslatableFieldsUpdate($fields, 'updateCMSFields');
    
            return $fields;
        }
    }

I added a pull request for updating the translatable docs with some example code for that.