1

I've seen in some places that strings are surrounded by _() like _('some string').

For example in a desktop program with these imports:

const Applet = imports.ui.applet;
const St = imports.gi.St;
const Gdk = imports.gi.Gdk;
const Gtk = imports.gi.Gtk;
const Keymap = Gdk.Keymap.get_default();
const Caribou = imports.gi.Caribou;
const PopupMenu = imports.ui.popupMenu;
const Lang = imports.lang;
const Gio = imports.gi.Gio;
const Main = imports.ui.main;
const MessageTray = imports.ui.messageTray;

What is the use of that function?

germanfr
  • 547
  • 5
  • 19

3 Answers3

6

It can really be anything. For instance the entire underscore library is built around using _ as a normal variable/function/class name.

_ is not a reserved character, not any more than i, a and so on.

Practical example:

_('test')
//ReferenceError: _ is not defined

function _(str){
    console.log(str);
}

_('test')
//Output: test

More often than not, if not using underscore.js, it would be used for a function that you would use very often hence only using a single character.

Note: As stated by @Xedecimal and @AliTorabi, it is also often used to name a function defined as a translator for internationalization since once again, it is very short and used a very often.

Slava Knyazev
  • 5,377
  • 1
  • 22
  • 43
0

The is NO _ function in JavaScript, unless you implement it your self, or use an external library.

const _ = (str) => str.split('').join('_')
_("hello") // "h_e_l_l_o"
webdeb
  • 12,993
  • 5
  • 28
  • 44
0

I'll answer myself based on comments I got. (I already knew _ is not anything special but a normal function name, that wasn't the question).

Based on those imports, it seems to be part of a system library. Concretely this:

const Lang = imports.lang;

It is used to translate strings into other languages automatically. For example:

_('Hello') //Hola
germanfr
  • 547
  • 5
  • 19