From 4785440f65331295b95ec883c0a40bff66c51481 Mon Sep 17 00:00:00 2001 From: Fabian Homborg Date: Fri, 29 May 2020 20:53:44 +0200 Subject: [PATCH] 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 --- doc_src/cmds/_.rst | 38 ++++++++++++++++++++++++++++++++++++++ share/functions/_.fish | 22 ---------------------- src/builtin.cpp | 13 ++++++++++++- 3 files changed, 50 insertions(+), 23 deletions(-) create mode 100644 doc_src/cmds/_.rst delete mode 100644 share/functions/_.fish diff --git a/doc_src/cmds/_.rst b/doc_src/cmds/_.rst new file mode 100644 index 000000000..c1b73464e --- /dev/null +++ b/doc_src/cmds/_.rst @@ -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 diff --git a/share/functions/_.fish b/share/functions/_.fish deleted file mode 100644 index ee4e25b37..000000000 --- a/share/functions/_.fish +++ /dev/null @@ -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 - diff --git a/src/builtin.cpp b/src/builtin.cpp index 3914f59a5..af5388ab4 100644 --- a/src/builtin.cpp +++ b/src/builtin.cpp @@ -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)