What does the underscore translate function do in my code without a textdoman

I am hooking into comment_form_default_fields doing some heavy string replacements. There are translated strings like __( 'Name' ) in there. I need the text. I have two or three different ways to solve this. I could just use my own text domain, but that would mean I have to provide my own translation for my code, do not like that. Especially because I just reuse already translated strings. Extract the text (with regex). Better but not a huge fan either. …
Category: Web

Gettext: Specify base language for translatable strings?

I have plenty of customizations on a page which was only meant to be used for german audience - hence I hardcoded the some German sentences directly into the functions.php whereever I had language output. Now the website shall be translated and make usable for different languages. So I thought to simply replace any hardcoded german string like this: # Before echo 'Das ist mein Text'; # After: echo __('Das ist mein Text', 'theme-functions-translation'); But now the questions is: This …
Category: Web

Localization inside shortcode not working

I use a shortcode that retrieves the url of woocommerce products and builds a link: function woo_url_shortcode( $atts ) { $atts = shortcode_atts( array( 'id' => null, ), $atts, 'bartag' ); $html = ''; if( intval( $atts['id'] ) > 0 && function_exists( 'wc_get_product' ) ) { $_product = wc_get_product( $atts['id'] ); $html = $_product->get_permalink(); } $text = 'Some text'; return '<p class="woo-booking"><a href="'.$html.'">'.$text.'</a></p>'; } Works fine. But now I need the text translated like this: esc_html_e( 'Some text', 'generatepress-child' ); …
Category: Web

Replace plugin HTML to single text using hooks

I have a plugin that has HTML div that displays like /<div class="llms-setting-group "><p class="llms-label">General Settings(?s).*<\/table><\/div>/ now I want to replace this to simple hello users. I tried with gettext but I have no success in it. Here is code block that I have used: function my_text_strings( $translated_text, $text, $domain ) { if ( $translated_text == '/<div class="llms-setting-group "><p class="llms-label">General Settings(?s).*<\/table><\/div>/') { $translated_text = 'Hello Users!'; } return $translated_text; } add_filter( 'gettext', 'my_text_strings', 20, 3 );
Category: Web

Ajax - gettext without a plugin

How would you go about setting the correct language returned in AJAX calls, when you are not using any plugin? All other strings in page work fine just by defining WPLANG to a proper locale. Example: function lpml_get_quiz_result_callback() { // do plenty of stuff before this // this is always returned in original english language, even if I setlocale before or load textdomain manually. $message = __('Some english defined string', 'my_txt_domain'); $status = true; echo json_encode(array('status' => $status, 'message' => …
Category: Web

How to use gettext for specific user role

I am trying to replace the publish button text to Submit for review only for specific user role. But I can't find anything which can specify current user role and apply the filter. Here's my code, it's not working. add_filter( 'gettext', 'change_publish_button', 10, 2 ); function change_publish_button( $translation, $text ) { if( !current_user_can('administrator') ) { if ( $text == 'Publish' ) return 'Submit for review'; }; return $translation; } I also tried using $current_user = wp_get_current_user(); if (in_array('level-1-contributor', $current_user->roles)) It …
Category: Web

How to set translations in javascripts for my plugin?

I have in my plugin : form.js : js script to do a custom validation on form fields js_lang.php : based on this stackoverflow post my-plugin-domain-fr_FR.po In form.js, I use a var (msg_must_match) from js_lang.php function inputsMatch( inputTypes, idInputReferring, idInputRepeat ){ var inputReferring = document.getElementById( idInputReferring ); var inputRepeat = document.getElementById( idInputRepeat ); var checkPasswordValidity = function() { if (inputReferring.value != inputRepeat.value) { inputRepeat.setCustomValidity( msg_must_match /*<-- here*/ ); } else { inputRepeat.setCustomValidity(''); } }; inputReferring.addEventListener('change', checkPasswordValidity, false); inputRepeat.addEventListener('change', checkPasswordValidity, false); …
Category: Web

Can I set Wordpress to display languages based on system language without a plugin?

This question seems extremely straightforward, but hours of googling have led to no results. Here's the basic gist: I am building a site for Japanese viewers, and am using the theme's language folder to replace certain text (e.g. "Read More" and "Older Entries") with Japanese text. It works just fine when I set the WP language to Japanese in the admin; however doing so will mean that the English versions will NEVER display. I find it strange that WP allows …
Category: Web

How to ignore extra whitespaces in translation functions like _e?

I am passing few big strings in my code for translation to _e(). These strings were earlier broken into multiple lines with few leading white spaces before every line to maintain indentation & readability. And since HTML ignore extra white spaces, it doesn't affect anything. But now, on passing these strings through _e(), these white spaces are also added in the string added for translation in POT file. How can I prevent these white spaces to be added for translation …
Category: Web

Can't we use strings defined as PHP constants if we want to translate them in a plugin?

I want to translate constant strings in my plugin defined as: define( 'CONSTANT', __( 'string-A', 'textdomain' ) ); And later using it somewhere like: $x= '<h4>'.CONSTANT_NAME.'<h4>'; echo $x; But I am still getting English text even after adding that locale's .mo file & the text-domain. This is happening only for strings defined by PHP constants, rest all strings are working fine. So, does strings defined using PHP constants not work for translation as even in popular translated plugins I can't …
Category: Web

How to prevent someone from entering strings without making it available for translation?

I am working to make all strings in my plugin ready for translation by putting them under __() or _e() as per requirement. Now in future to avoid others in my team to add strings without making available for translation(bare strings), I want to add a code that runs may be on pushing the code(in my git repository) and throws exception mentioning the bare strings.
Category: Web

Translation not working for Constant strings in Plugin

I want to translate constant strings in my plugin defined as: define( 'CONSTANT', __( 'string-A', 'textdomain' ) );. The 'string-A' is available for translation and I have uploaded the mo files in the desired language including this string but in the WordPress it still reflects as English string. Translation is working for all other strings except the one defined as constants in my config file. Any idea why is this happening?
Category: Web

How does translation (gettext) work for translating config file of plugin?

I am doing translation of my plugin. I am using __ & _e in php files. But can't figure out how to do same for config file that contains constants added using define(name, value)? Does it automatically get translated using Wordpress translation dictionary once plugin adds code for loading text-domain? Also, Do I need to use add text domain compulsorily or will Wordpress's translator will translate it even then?
Category: Web

Localization of WP theme

I am working on WP theme localization. On custom search template I have function which gets text of some search terms and replace them with other (just visually). 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); } echo search_replace_search(); How to wrap search orreplace words, or function output (echo), so WP will …
Category: Web

Translate a child theme with pure PHP and gettext

I'm trying to translate a child theme of a parent theme that has all msgstr empty. Therefore, I've created the file de_DE.mo using the "WordPress i18n tools" recommended in the docs together with msgfmt. I put the file into my child theme directory's subfolder languages/child. To tell Wordpress about it, I added this to my functions.php where mo_theme is the parent theme's translation domain: add_action( 'after_setup_theme', function () { load_theme_textdomain( 'mo_theme', get_stylesheet_directory() . '/languages/child' ); } ); However, the translation …
Category: Web

Using WordPress gettext functions in a library outside plugin or theme scope

If I have a library available through packagist that could be used in WordPress plugin or theme development, how should I address multilanguage support if I have a few strings that could be localized? I know that there has been some debate on composer usage in the WordPress world with very different views on it. (For example: 1, 2, 3) But my question here is not about opinions on Composer in WordPress development, rather what would be the best practice …
Category: Web

Does WPML translate numbers automatically, or do I have to do the string translation?

I'm developing my site in English, and when I'm done, I'll be turning it into Arabic. I'm using a function and wondering whether I should wrap it into a gettext call for WPML to get or just leave it as it is and it will be converted into Arabic automatically (since it's numbers and not text)? Should I do this? <?php echo $woocommerce->cart->cart_contents_count;?> Or keep it like this? <?php echo sprintf(__('%d', $woocommerce->cart->cart_contents_count));?> Thanks!
Category: Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.