{"id":2181,"date":"2011-05-19T12:06:27","date_gmt":"2011-05-19T10:06:27","guid":{"rendered":"http:\/\/wpengineer.com\/?p=2181"},"modified":"2016-09-16T08:50:33","modified_gmt":"2016-09-16T06:50:33","slug":"localization-with-javascript-in-wordpress","status":"publish","type":"post","link":"https:\/\/wpengineer.com\/2181\/localization-with-javascript-in-wordpress\/","title":{"rendered":"Localization with JavaScript in WordPress"},"content":{"rendered":"<p>Creating Plugins and Theme functions with multilanguage capability has been established knowadays.Especially for us as German developers it is a must have. But there are some difficulties if you are using JavaScript, the question is how to provide it <a href=\"https:\/\/multilingualpress.org\" target=\"_blank\">multilingual<\/a> or provide option values in different languages. WordPress offers some possibilities and I like to show them, since <a href=\"http:\/\/wordpress.stackexchange.com\/questions\/351\/modifying-a-js-file-with-data-from-plugin-settings\/357#357\">this question<\/a> was asked by many and developers are searching for solutions. The following little example should show the realization and the easy output of strings demonstrates it. Wether strings for multilanguage or providing options values, it doesn&#8217;t matter.<br \/>\n<!--more--><br \/>\nFirst we create a function with an array that contains the required content and can be accessed on the relevant content using variables.<\/p>\n<pre>\r\nfunction get_language_strings() {\r\n    $strings = array(\r\n        'example' => __( 'My Example String', TEXTDOMAIN_CONSTANT ),\r\n        'foo'   => __( 'My foo string', TEXTDOMAIN_CONSTANT ),\r\n        'bar'   => __( 'My bar', TEXTDOMAIN_CONSTANT )\r\n    )\r\n\r\n    return $strings;\r\n}\r\n<\/pre>\n<p>When you call the extension, Plugin or Theme, the function must be loaded. For this task WordPress provides the function <code><a href=\"http:\/\/codex.wordpress.org\/Function_Reference\/wp_localize_script\">wp_localize_script()<\/a><\/code> that takes care of everything:<\/p>\n<pre>\r\nwp_localize_script( \r\n                       'my_foo', \r\n                       'my_var_prefix',\r\n                       $this->get_language_strings() \/\/ inside class\r\n);\r\n<\/pre>\n<p>You can access the variables in the scripts:<\/p>\n<pre>\r\nalert( my_var_prefix.example );\r\n<\/pre>\n<p>No integration of <code>wp-load.php<\/code> or something like this, simply access the content. The strings are now translated into all languages using the classical methods and it applies only to create the appropriate files.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Creating Plugins and Theme functions with multilanguage capability has been established knowadays.Especially for us as German developers it is a must have. But there are some difficulties if you are using JavaScript, the question is how to provide it multilingual or provide option values in different languages. WordPress offers some possibilities and I like to [&hellip;]<\/p>\n","protected":false},"author":321,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[28,114,64,17,5,3,20],"class_list":["post-2181","post","type-post","status-publish","format-standard","hentry","category-wordpress-tutorials","tag-code","tag-development","tag-javascript","tag-php","tag-plugin","tag-wordpress","tag-wp"],"_links":{"self":[{"href":"https:\/\/wpengineer.com\/wp-json\/wp\/v2\/posts\/2181","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wpengineer.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wpengineer.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wpengineer.com\/wp-json\/wp\/v2\/users\/321"}],"replies":[{"embeddable":true,"href":"https:\/\/wpengineer.com\/wp-json\/wp\/v2\/comments?post=2181"}],"version-history":[{"count":0,"href":"https:\/\/wpengineer.com\/wp-json\/wp\/v2\/posts\/2181\/revisions"}],"wp:attachment":[{"href":"https:\/\/wpengineer.com\/wp-json\/wp\/v2\/media?parent=2181"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wpengineer.com\/wp-json\/wp\/v2\/categories?post=2181"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wpengineer.com\/wp-json\/wp\/v2\/tags?post=2181"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}