Add an "_" builtin to call into gettext (#7036)

* Add an "_" builtin to call into gettext

We already have gettext in C++ (if available), so it seems weird to
fork off a command to start it from script.

This is only for fish's own translations. There's no way to call into
other catalogs, it just translates all arguments separately.

This is faster by a factor of ~1000, which allows us to call
translations much more, especially from scripts.

E.g. making fish_greeting global by default would hurt cost-wise,
given that my fish starts up in 8ms and just calling the current `_`
function takes 2ms, and that would have two calls.

Incidentally, this also makes us rely on a weirdly defined function
less, so it:
Fixes #6804.

* docs: Add `_` docs

Let's see if that filename works out.

* Reword _ docs
This commit is contained in:
Fabian Homborg 2020-05-29 20:53:44 +02:00 committed by GitHub
parent 1e17a68133
commit 4785440f65
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 50 additions and 23 deletions

38
doc_src/cmds/_.rst Normal file
View file

@ -0,0 +1,38 @@
.. _cmd-_:
_ - call fish's translations
============================
Synopsis
--------
::
_ STRING...
Description
-----------
``_`` translates its arguments into the current language, if possible.
It is equivalent to ``gettext fish STRING``, meaning it can only be used to look up fish's own translations.
It requires fish to be built with gettext support. If that support is disabled, or there is no translation it will simply echo the argument back.
The language depends on the current locale, set with ``$LANG`` and ``$LC_MESSAGES``.
Options
-------
``_`` has no options.
Examples
--------
::
> _ File
Datei

View file

@ -1,22 +0,0 @@
#
# Alias for gettext or a fallback if gettext isn't installed.
#
# Use ggettext if available.
# This is the case on OpenIndiana, where the default gettext
# interprets `\n` itself, so
# printf (_ 'somemessage\n')
# won't print a newline.
if command -sq ggettext
function _ --description "Alias for the ggettext command"
command ggettext fish $argv
end
else if command -sq gettext
function _ --description "Alias for the gettext command"
command gettext fish $argv
end
else
function _ --description "Fallback alias for the gettext command"
echo -n $argv
end
end

View file

@ -327,6 +327,15 @@ int builtin_false(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
return STATUS_CMD_ERROR;
}
int builtin_gettext(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
UNUSED(parser);
UNUSED(streams);
for (int i = 1; i < builtin_count_args(argv); i++) {
streams.out.append(_(argv[i]));
}
return STATUS_CMD_OK;
}
// END OF BUILTIN COMMANDS
// Below are functions for handling the builtin commands.
// THESE MUST BE SORTED BY NAME! Completion lookup uses binary search.
@ -338,6 +347,7 @@ static const builtin_data_t builtin_datas[] = {
{L".", &builtin_source, N_(L"Evaluate contents of file")},
{L":", &builtin_true, N_(L"Return a successful result")},
{L"[", &builtin_test, N_(L"Test a condition")},
{L"_", &builtin_gettext, N_(L"Translate a string")},
{L"and", &builtin_generic, N_(L"Execute command if previous command succeeded")},
{L"argparse", &builtin_argparse, N_(L"Parse options in fish script")},
{L"begin", &builtin_generic, N_(L"Create a block of code")},
@ -393,7 +403,8 @@ static const builtin_data_t builtin_datas[] = {
{L"true", &builtin_true, N_(L"Return a successful result")},
{L"ulimit", &builtin_ulimit, N_(L"Set or get the shells resource usage limits")},
{L"wait", &builtin_wait, N_(L"Wait for background processes completed")},
{L"while", &builtin_generic, N_(L"Perform a command multiple times")}};
{L"while", &builtin_generic, N_(L"Perform a command multiple times")},
};
#define BUILTIN_COUNT (sizeof builtin_datas / sizeof *builtin_datas)