Changing interface language without multisite

2 notes regarding your own answer: try to not access on super global variables directly, but using filter_input your code throw a notice if $_GET[‘lang’] is not set So your code can be improved: function toscho_change_language() { return ‘bn_BD’; } if ( filter_input( INPUT_GET, ‘lang’, FILTER_SANITIZE_STRING ) === ‘bn’ ) { add_filter( ‘locale’, ‘toscho_change_language’ ); … Read more

Post types translation

Use get_post_types( [‘public’ => TRUE ], ‘objects’ );. This will return the already translated post type labels and other information. Example for tests: add_action( ‘wp_footer’, function() { $post_types = get_post_types( [ ‘public’ => TRUE, ‘_builtin’ => TRUE ], ‘objects’ // complete information, not just the name ); foreach ( $post_types as $slug => $arguments ) … Read more

Where does the locale come from?

Locale comes from WPLANG constant defined in wp-config.php. In the last version of WordPress (4.0, maybe 3.9) something is changing. You can install more languages setting WPLANG to different locale. Then you can select the language in the backend from Settings > General > Site language and set locale in the header. It seems that … Read more

How to translate Widget Description in constructor?

You have slight timing issues because you generate title first (in constructor call), but postpone text domain load to later (init at default priority). Text domain mus be loaded for __() to translate correctly. Those are not “lazy”, they generate and return strings right where they are called. Text domain should be loaded very early, … Read more

WordPress Translations

.po and .mo files are for translating strings in code files. For translating post content use a translation plugin. I’ve used a few and find WPML to be the best in many aspects. Incidentally, they have a built-in workflow for translating both content and strings in code files.

failed to open stream: Permission denied warning while load_textdomain

If you want to use load_textdomain you must also specify the file name: function my_custom_locale() { load_textdomain(‘my-name’, get_stylesheet_directory().’/languages/my-name- ‘.get_locale().’.mo’); } add_action(‘after_setup_theme’, ‘my_custom_locale’); In my example I added custom translations used in additional template files placed inside the theme-child folder, where I also added the languages files folder (es. my-name-it_IT.mo, my-name-en_GB.mo). I added the code in … Read more

Localization of WP theme

Wrap the elements of $search and $replace arrays into __() function to get the translated strings: function search_replace_search() { $search_term = esc_attr( apply_filters( ‘the_search_query’, get_search_query( false ) ) ); // Get search term $search = array( __(‘word1’), __(‘word2’) ); $replace = array( __(‘something 1’), __(‘something 2’) ); $replacePairs = array_combine($search, $replace); echo strtr($search_term, $replacePairs); } … Read more