{"id":1190,"date":"2019-04-21T18:09:42","date_gmt":"2019-04-21T21:09:42","guid":{"rendered":"https:\/\/felipeelia.com.br\/?p=1190"},"modified":"2019-07-04T19:59:46","modified_gmt":"2019-07-04T22:59:46","slug":"how-to-translate-theme-options-inside-customizer-with-polylang-and-wpml","status":"publish","type":"post","link":"https:\/\/felipeelia.dev\/how-to-translate-theme-options-inside-customizer-with-polylang-and-wpml\/","title":{"rendered":"How to Translate Theme Options inside Customizer with Polylang and WPML"},"content":{"rendered":"\n
As I said in my first post<\/a> here, after months thinking about it, I\u2019ve finally decided to turn this blog multilingual in an attempt to get more clients among Codeable<\/a>‘s target audience. I had to decide between turning this WordPress installation into a multisite one or using a plugin and, after this twitter thread<\/a>, I gave Polylang<\/a> a chance.<\/p>\n\n\n\n\n\n\n I used to work with WPML<\/a> but as Polylang has a free version, it was my first choice. As the blog theme is a simple one I didn’t face any difficulty, unless how to translate an option I have in Customizer. Are you seeing the Creative Commons license in the footer? Yes, that is a Customizer field, built this way:<\/p>\n\n\n\n That code creates a Copyright<\/em> field in the Rodap\u00e9 (Footer)<\/em> Customizer section. To show it I use the following code in my footer.php<\/em>:<\/p>\n\n\n\n I needed to translate that and I didn’t know how. Actually, I had a guess, but I didn’t work on anything like that before. A long time ago, WPML created a file pattern called wpml-config.php<\/strong>, that allows plugins and themes to declare which post types, taxonomies, and admin texts should be translated. Theme mods<\/em> fall in that “admin text” section. As it already was the pattern at that time, Polylang also uses the same file<\/strong>. Yeah, with their competitor’s name.<\/p>\n\n\n\n Everything kept in the wp_options<\/em> tables fall in the admin text section. Theme mods, these we call with get_theme_mod<\/a>, are stored in an option called theme_mods_THEMENAME<\/em> and its value is a serialized associative array, i.e., an array with named keys that passes through PHP serialize<\/a> function before being saved. At the end of the day, all I had to do was create a file called wpml-config.xml<\/em> in my theme root directory<\/strong> with the following content:<\/p>\n\n\n\n Note that felipeelia<\/em>, in this case, is my theme slug<\/p>\n\n\n\n If you found this content useful, please share it and leave a comment. That always helps a lot \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":" Read how to translate your theme options in Customizer with Polylang and WPML through the wpml-config.xml file<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","footnotes":""},"categories":[91],"tags":[110],"yoast_head":"\nfunction felipeelia_customize_register( $wp_customize ) {\n\t$wp_customize->add_section(\n\t\t'felipeelia_rodape',\n\t\tarray(\n\t\t\t'title' => 'Rodap\u00e9',\n\t\t\t'priority' => 125,\n\t\t)\n\t);\n\t$wp_customize->add_setting(\n\t\t'copyright',\n\t\tarray(\n\t\t\t'default' => '',\n\t\t\t'capability' => 'edit_theme_options',\n\t\t)\n\t);\n\t$wp_customize->add_control(\n\t\t'copyright',\n\t\tarray(\n\t\t\t'label' => 'Copyright',\n\t\t\t'section' => 'felipeelia_rodape',\n\t\t\t'settings' => 'copyright',\n\t\t\t'type' => 'textarea',\n\t\t)\n\t);\n}\nadd_action( 'customize_register', 'felipeelia_customize_register' );<\/code><\/pre>\n\n\n\n
<?php echo get_theme_mod( 'copyright' ); ?><\/code><\/pre>\n\n\n\n
<wpml-config>\n <admin-texts>\n <key name=\"theme_mods_felipeelia\">\n <key name=\"copyright\" \/>\n <\/key>\n <\/admin-texts>\n<\/wpml-config><\/code><\/pre>\n\n\n\n