The ACF plugin allows you to create Field Labels and add Choices to Select, Checkbox, and Radio Button field types. We’ll show you how to translate ACF Field Labels and the labels for Choices with WPML.
Before getting started, make sure to install and activate the Advanced Custom Fields, WPML core, WPML String Translation, and Advanced Custom Fields Multilingual plugins.
In order to translate FieldLabels and labels for Choices, you need to follow a two-step process. Read on to learn how to:
Adjust the function in your theme’s files to make the labels translation-ready
Scan your theme for new strings and translate the labels using WPML’s String Translation
In ACF, each custom field contains a Field Label, Field Name, and Field Type. As an example, take a look at this Text field on the Custom Fields backend:
A Text field on the Custom Fields backend
For most custom fields, you may only want to return the field value. Sometimes, however, you may want to translate and display both the field’s label and value on the front-end.
In such cases, you should adjust the function in your theme’s files with code that wraps the content you want to translate into a WordPress-standard getttext function. Note that the Field Labels you provide in ACF will be ignored. You will need to enter the Field Label into your code.
Of course, you need to adjust the snippet to fit your custom field settings. Below, we briefly explain the parameters of the function shown above.
‘recipe_short_description‘ is the name of the field. Here, you need to input the Field Name of your custom field.
‘Short description‘ is the label of the field. Here, you need to input the Field Label of your custom field.
‘twentyfifteen-child‘ is the textdomain, or unique identifier you create. It wraps the static text string, such as Shortdescription, in a translation function and makes it available for translation. It also makes it easier for WordPress to distinguish between translations and know which theme or plugin your strings belong to.
In the sections below, we cover each of the steps you need to take in detail.
Setting the Translation Preferences for Select, Checkbox, and Radio Button Field Types
Before you make the labels for Choices translatable, make sure you set the translation preferences of your Select, Checkbox, or RadioButton fields to Copy. You can do this by going to WPML → Settings and scrolling down to the Custom Fields Translation section.
Setting the translation preferences of a Choice field to Copy
When you create a Select, Checkbox, or RadioButton field type, ACF allows you to add Choices. In the Choices section, you can specify both a value and a label.
A Radio Button field with values and labels added in Choices
In the example above, beginner, intermediate, and expert are the values, while Beginner, Intermediate, and Expert are the labels.
To translate the labels for Choices and display them correctly on the front end, you need to add some additional code to your theme files. This code wraps the content you want to translate into a WordPress-standard getttext function.
With the example above in mind, here’s the code we need to add to our theme files:
You need to adjust the snippet to fit your custom field settings. Below, we briefly explain the parameters of the function.
‘beginner’, ‘intermediate’, and ‘expert’ are the Choicevalues. Here, you need to input the exact same value(s) as you did in the Choices section of your Select, Checkbox, or RadioButton field on the Custom Fields backend.
‘Beginner‘, ‘Intermediate’, and ‘Expert’ are the Choice labels. Here, you need to input the label(s) you want to translate and display on the front-end. If you only specified a value when setting up your Choices, you can add any label to your code. However, if you specified a label when setting up the Choices for your Select, Checkbox, or RadioButton fields in Custom Fields, you need to input the exact same label into your code.
‘twentyfifteen-child’ is the name, or unique identifier you create. It makes it easier for WordPress to distinguish between translations and know which theme or plugin your strings belong to.
‘Level of difficulty‘ is the label of the field. Here, you need to input the Field Label of your custom field.
You can then scan your theme for the texts and translate them using WPML’s String Translation, which allows you to translate texts that are not found in posts or pages. This includes texts from other themes and plugins on your site.
Translating Field Labels and Labels for Choices
Once you make your Field Labels and labels for Choices translatable, you can follow the steps below to translate them.
Go to WPML → Theme and plugins localization and scroll to the Strings in the themes section.
Select your theme and click on Scan selected theme for strings.
Scanning the theme for strings
Once scanning is complete, you should see the unique identifier you added to your code appear in the Textdomain column. This means the new texts for translation have been found and added to the WPML’s String Translation table.
A new Textdomain appears once scanning is complete
Now, navigate to WPML → String Translation.
Using the display filter at the top of the String Translation page, select the domain your texts belong to. In this case, we are searching for In domain: twentyfifteen-fields.
Using the In domain dropdown menu to search for the new strings
Click the plus icon next to the string(s) you want to translate and hit enter to save the translation.