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.

  1. 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.
  2. Extract the text (with regex). Better but not a huge fan either.
  3. Just use the same words __( 'Name' ) as in core without textdomain?

The translation is done by lines in files to some extent I think, but when lines change the translations do not suddenly stop to work correctly, but I think there can be different translation for the same world, and it will pick the right ones. If the translation is properly done.

My question is what actually happens when I use no domain? Will it work at all? Will it just pull the first translation for a word it can find in WP core?

Topic xgettext localization Wordpress

Category Web


In the __() documentation, it says

If there is no translation, or the text domain isn’t loaded, the original text is returned.

Also, if you don't provide a textdomain, then 'default' is used.

So, essentially, __( 'Name' ) will try to find a translation in the 'default' domain, and, failing that, return 'Name'.

If you do provide your own textdomain (eg __( 'Name', 'my-plugin-text-domain' )) but no translations, then the untranslated string will be used. There is, as far as I know, no downside to this, and it has the upside of making your theme or plugin translatable by you or others who wish to do it in the future.

About

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