Support

Account

Home Forums Backend Issues (wp-admin) How to translate field labels without WPML

Solved

How to translate field labels without WPML

  • Hello,
    I have a problem translating ACF custom field labels without any translation plugin using WordPress’ own l18n functions.

    I created my own WordPress theme and included ACF 5.3.7 (https://www.advancedcustomfields.com/resources/including-acf-in-a-plugin-theme/).

    My theme is translated using WordPress’ l18n functions. I created the language files (en_US.mo/.po and de_DE.mo/.po) with Poedit in my theme, set up my own textdomain (my_theme) in functions.php and switched the WordPress language to German. My theme has a custom options page (completely independent of ACF) with some form labels which get translated just fine. Therefore my implementation of the translation and the language files are okay!

    Afterwards I created some custom fields with ACF, exported them as PHP and included them in the theme. The fields show up in the backend and work fine.

    
    <?php
    if(function_exists('acf_add_local_field_group')){
      acf_add_local_field_group(array(
        'key'                   => 'group_5719fb17d39b1',
        'title'                 => _x('Related Posts', 'ACF Related Post', 'my_theme'),
        'fields'                => array(
          array(
            'key'               => 'field_573ee1d2fc9cf',
            'label'             => _x('Choose some posts', 'ACF Related Post', 'my_theme'),
            'name'              => 'related_posts',
            'type'              => 'relationship',
            'instructions'      => '',
            'required'          => 0,
            'conditional_logic' => 0,
    ...
    

    I want to translate the labels of the ACF fields in the backend. As you can see I used WordPress’ l18n functions to translate the custom field labels.

    Unfortunately the ACF field labels do not get translated. I know I can use something like WPML to translate it, but I don’t want to use a plugin for something I can achieve in the theme.

    How do I translate custom ACF field labels in the WordPress backend without a translation plugin like WPML?

    I found this in the ACF documentation regarding translation but it isn’t very helpful at all: https://www.advancedcustomfields.com/resources/multilingual-custom-fields/

    I would appreciate every little hint how to solve this.

    Best regards,
    Brian

    ACF 5.3.7
    Wordpress 4.5.2
    No other plugins.

  • Hi @briandoh

    I believe you need to set the “l10n_textdomain” on ACF settings. This page should give you more idea about it: https://www.advancedcustomfields.com/resources/acfsettings/.

    I hope this helps 🙂

  • Hi James,
    thank you so much for your help, this solved it! 🙂

    I had to add 2 filters and replace every _x() function with __().

    The filter:

    
    function custom_acf_settings_localization($localization){
      return true;
    }
    add_filter('acf/settings/l10n', 'custom_acf_settings_localization');
    
    function custom_acf_settings_textdomain($domain){
      return 'my_theme';
    }
    add_filter('acf/settings/l10n_textdomain', 'custom_acf_settings_textdomain');
    

    And replace

    'title' => _x('Related Post', 'ACF Related Post', 'my_theme'),

    with

    'title' => __('Related Post', 'my_theme'),

    Now it’s working perfectly! Thanks! 🙂

  • I tried the same solution, but my problem continues. 🙁

  • The problem continues for me as well. I have added those filters mentioned by briandoh, and I have tried different priorities and replacing esc_html__() with __(), but nothing is being translated. Could the reason be, that I’m trying to do this within a plugin?

    This is my code for loading the translation:

    /**
     * Load translations
     */ 
    add_action('plugins_loaded', 'sg_slide_in_load_plugin_textdomain'); 
    function sg_slide_in_load_plugin_textdomain() {
        load_plugin_textdomain('sg-slide-ins', FALSE, basename(dirname( __FILE__ )) . '/languages/');
    }
    
    add_filter('acf/settings/l10n', 'sg_slide_in_acf_settings_localization');
    function sg_slide_in_acf_settings_localization($localization){
    	return true;
    }
    
    add_filter('acf/settings/l10n_textdomain', 'sg_slide_in_acf_settings_textdomain');
    function sg_slide_in_acf_settings_textdomain($domain){
    	return 'sg-slide-ins';
    }
Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.