Support

Account

Home Forums Backend Issues (wp-admin) Translating exported fields

Solving

Translating exported fields

  • Hi!

    I’ve managed to export my fields and wrapped label and instructions with translation strings thanks to some modification to export.php in /core/controllers folder from your nightly build.

    Like this:

    // add the WP __() function to specific strings for translation in theme
    $html = preg_replace("/'label'(.*?)('.*?')/", "'label'$1__($2, 'my_text_domain')", $html);
    $html = preg_replace("/'instructions'(.*?)('.*?')/", "'instructions'$1__($2, 'my_text_domain')", $html);

    I get this (example):

    array (
    	'key' => 'field_48',
    	'label' => __('Label title example', 'my_text_domain'),
    	'name' => 'field_name_example',
    	'type' => 'true_false',
    	'instructions' => __('Instructions example', 'my_text_domain'),
    	'message' => '',
    	'default_value' => 0,
    ),

    Then i translate them with .po and then .mo files.

    I then tell wordpress to pick up the translation like this:
    load_theme_textdomain('my_text_domain', get_template_directory() . '/lang');

    This all works for all the rest of my theme, for custom post types etc. But the acf-fields just won’t pick up the translation. I can’t understand why it works for the rest of the theme but not for the acf-fields. Can it have anything to do with the order things load? Do you Elliot or anyone else have an idea?
    Do you have an example where you successfully have translated acf-fields?

    Thank you for an awesome plugin and hoping for some help 🙂
    Anton

  • Hello,

    I’m having the same issue, i’ve exported the fields and i’m trying to translate them for the front-end acf form however the translation file doesn’t work, it works for every other string in the theme but the acf fields.

    How would we fix this problem ?

  • Hi guys

    So is the issue that your poedit program is not picking up the strings as translatable? or is the issue that the data is not being translated at all even through your .po file is correct?

    Thanks
    E

  • The latter.
    My poedit program is picking up the fields but the data isn’t being translated.

  • Same as above, the po file is picking up the strings however the data isn’t being translated.

  • How strange. I wonder if it has something to do with the time at which the fields are being loaded. Perhaps it is before the translation files are being registered in WP?

  • That doesn’t seem to be related to the issue, doesn’t matter where i move the translation action before / or after the fields are loaded

    Right now my function file is like

    – require_once to file with fields

    – action that tells wordpress to load translation

    I also tried

    – action that tells wordpress to load translation

    – require_once to file with fields

    None of the above fixed the issue.

  • Had some more time and did further testings, and solved it by adding this to my functions.php before loading the translation action.

    function acf_translate_fields( $field )
    {
    	$field['label' ] = __( $field['label' ], 'domain'  );
    	$field['instructions' ] = __( $field['instructions' ], 'domain'  );
    
    	return $field;
    }
    add_filter('acf/load_field' , 'acf_translate_fields' );
  • Perfect! That did the trick for me!

    However, i need to translate ‘choices’ and ‘sub_fields’ as well.

    How can i do this?

  • I really need to fix the choices and sub_fields/label and instructions as well.
    Any idea how i can do that?

    I realized what the above fix does and with that you wouldn’t have to export the fields for translation. Well for the Po edit program to pick up the strings but that maybe can be done in some other way? If the end-purpose is to translate the fields and not to translate exported fields that is to say.

  • Also, the title of the register_field_group needs to be translatable.
    And Layouts/Sub fields for Flexible content fields.

    Elliot, am i missing something obvious or is the theme not supporting translatable fields fully yet?

  • Hi @AntonCa

    It seems that for some reason, the __() function is not working in the functions.php file when fields get registered…

    It may be worth testing registering the exported fields within an action such as init or ‘wp’. Maybe that will allow the __() function to work?

  • There we go!

    Including the exported fields inside an init function did the trick:

    add_action('init', 'load_exported_fields');
    function load_exported_fields(){
    	include 'acf-exported-fields.php';
    }
    

    I would like to do a request.

    As i mentioned before, you have a function that wraps certain fields in __() in your nightly build in the file:
    /core/controllers/export.php on line 387:

    // add the WP __() function to specific strings for translation in theme
    				$html = preg_replace("/'label'(.*?)('.*?')/", "'label'$1__($2)", $html);
    				$html = preg_replace("/'instructions'(.*?)('.*?')/", "'instructions'$1__($2)", $html);

    Could you make this include other necessary fields as well? For example: ‘title’, ‘choices’, ‘sub_fields’, ‘layouts/sub_fields’?

    Or are you planning on some other way to translate fields?

    I would also like to report a bug.
    When i export fields that have the following location values they show up on every page with a loading sign going on and on, see attached file. Do you know why?

    'location' => array (
    			array (
    				array (
    					'param' => 'options_page',
    					'operator' => '==',
    					'value' => 'acf-options-admin',
    					'order_no' => 0,
    					'group_no' => 0,
    				),
    				array (
    					'param' => 'user_type',
    					'operator' => '==',
    					'value' => 'administrator',
    					'order_no' => 1,
    					'group_no' => 0,
    				),
    			),
    		),

    Thank you Elliot!

  • Hi @AntonCa

    Thanks for the feedback.

    As for the spinning wheel, can you perform a hard refresh to make sure you do not have any cached CSS?

    Thanks
    E

  • Hi guys,

    I’m quite new to WorpPress and ACF, and am facing the same issue as the one mentioned in this thread. I applied the first fix proposed by @AntonCa and I’m now stuck to have group_fields and sub_fields translated. I’d like to try the second fix but am not too sure where to find that ‘acf-exported-fields.php’ or what it is supposed to contain.
    Unless another permanent fix has been found in the meantime … ?

    Can you help me out on this ?

    Thanks in Advance !

  • @AlleyOop, acf-exported-fields.php is created when you go to Custom fields / Import/Export and click on “Download export file”. Then you have to include that file in your functions.php file. I don’t know of any way to translate the fields while they’re in the database though.


    @Elliot
    Condon
    Sorry for not replying about the spinning wheels. I guess it got sorted on some update! Is there a more dynamical way of translating the fields in backend now? It would be great to have all the text-strings automatically wrapped in “translation tags” upon export. Labels, instructions etc.

Viewing 16 posts - 1 through 16 (of 16 total)

The topic ‘Translating exported fields’ is closed to new replies.