thepurpleblob

What is this .. __("something") ?

Recommended Posts

There seems to be a function, somewhat bizarrely, named double underscore. Which confused the hell out of me for quite  a few minutes. 

After a bit of digging, it seems to be to do with language translation. 

Why would anybody call a function __?  Probably just venting...

Share this post


Link to post
Share on other sites
21 hours ago, Soma said:

I don't know what's so confusing about it. 

It could just as well be named gettext(), translate() or something long those lines and be way more descriptive and without the need to lookup docs.

Share this post


Link to post
Share on other sites

Ok... get the idea. Thanks.

I'm going to add this to the list of things that I would give one of my devs a row about, though 😁   Along with jQuery's '$', most uses of try..catch (rather than writing proper error handling) and anything that results in 'unknown error'. Always use descriptive function names. I don't mind typing a few extra letters.

Share this post


Link to post
Share on other sites

It is quite common to use one or two characters for "translate functions", since they are typed and used a lot. Some examples:

Underscore appears to be a popular one.

 

  • Like 5

Share this post


Link to post
Share on other sites
11 hours ago, szabesz said:

It is quite common to use one or two characters for "translate functions", since they are typed and used a lot. Some examples:

Underscore appears to be a popular one.

 

Yeh - so I see. Now I've dug a bit more. I'd not encountered it before. 

I'm still horrified but as I'm not going to write my own CMS I'll just shut up now 😂

  • Like 1

Share this post


Link to post
Share on other sites

Old discussion, but yeah – the plus sides of this kind of naming are that 1) it's a common practice across different systems (which makes it easy for new devs to grasp), and 2) that it's a very commonly typed function – not to mention one that you often see in front-end (or "view") part of a site or an app, and thus it's good to keep it nice and short .

Expanding on the second point a bit, in the view side it often makes sense to have short function names, even if they're not particularly descriptive. Think of PHP's short echo tags (<?= ... ?>) or various tags implemented by templating languages: the point is to create minimal clutter, thus keeping view files clean and easy to follow 🙂

---

Note: this thread is not related to module development per se, so I'm moving it to the Getting Started area of the forum.

  • Like 1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.