How to Translate the 'Exit mobile version' Text in AMP
Content
Many multilingual WordPress sites using the AMP plugin need to translate its default text strings, such as the "Exit mobile version" link. This link allows visitors to switch from the AMP version of a page back to the standard version. By default, this text is in English, but it's a common requirement to change it to match the site's primary language.
Why You Need to Translate This Text
The AMP plugin uses translatable text strings. While many parts of the plugin may be translated via language files, some strings might not be covered by your site's language pack or might require a custom translation that isn't available in the default set. This is often the case for less common phrases or for sites that require a very specific dialect or wording.
Solution: Use a Custom Code Snippet
The most effective way to change this specific text is by adding a small code snippet to your website. This method uses a WordPress filter called gettext_amp to intercept the translation process and return your custom text.
Step-by-Step Instructions
- Access your WordPress site's files. This is typically done via an FTP client or your web hosting provider's file manager.
- Navigate to your theme's directory. To prevent your changes from being lost during a theme update, it is highly recommended to use a child theme.
- Open the child theme's
functions.phpfile for editing. - Copy and paste the following code snippet at the very bottom of the file, making sure it is placed before the closing
?>PHP tag if one exists.
add_filter( 'gettext_amp', function( $translation, $text, $domain ) {
if ( 'amp' !== $domain ) {
return $translation;
}
if ( 'Exit mobile version' === $text ) {
return 'Your Custom Translation Here';
}
return $translation;
}, 10, 3 );
Important: Replace Your Custom Translation Here with the text in your desired language. For example, for German, you would use Mobile Ansicht verlassen.
- Save the changes to the
functions.phpfile and upload it back to your server. - Clear any caching mechanisms on your site (server cache, plugin cache, or CDN) and then check your AMP pages to see the new translated text.
Troubleshooting Tips
- Code Doesn't Work: Double-check that the code was placed correctly in your child theme's
functions.phpfile and that there are no syntax errors, like missing semicolons or brackets. - Translation Appears Partially: Ensure you are replacing the exact string
'Exit mobile version'. The comparison is case-sensitive. - Best Practices: Using a child theme is crucial. Editing a parent theme's
functions.phpfile directly will cause your customizations to be erased the next time the theme is updated.
This method provides a reliable and update-safe way to customize the text strings in the AMP plugin without modifying its core files.
Related Support Threads Support
-
Translate “quit mobile Version”https://wordpress.org/support/topic/translate-quit-mobile-version/