Language dependent 'yes' command in linux, possible?

Viewed 131

So apperently 'yes' command returns 'y' by default. It was working perfect with English as System Language in Ubuntu. Now I actually got a user who tries to run the software under Ubuntu with the German system language and it doesn't work as expected. It seems that the prompts expect 'j' or 'ja', but 'yes' still delievers 'y'.

Is there a way to make the output of 'yes' language dependent? For now we started using LC_ALL=C in the top of the script. But still evaluating if this is a good solution.

3 Answers

Ok, dug out this page: https://github.com/coreutils/coreutils/blob/master/src/yes.c

and on line 63:

setlocale (LC_ALL, "");

"" means following in setlocale:

If locale is an empty string, "", each part of the locale that should be modified is set according to the environment variables. The details are implementation-dependent. For glibc, first (regardless of category), the environment variable LC_ALL is inspected, next the environment variable with the same name as the category (see the table above), and finally the environment variable LANG. The first existing environment variable is used. If its value is not a valid locale specification, the locale is unchanged, and setlocale() returns NULL.

So it should get your system language, but it does not. (perhaps this might be a bug)

You best alternative is a alias.

When you are using support for different languages, you probably have a function that handels input.
Change that function so it will also accept 0 for yes, and 1 for no (true returns 0).
Once your software also accepts 0 and 1, you can use

yes 0

Please try this:

yes | sed 's/y/ja/'

More complex:

myes(){
    case ${LANG,,} in
        *en*) yes=yes;;
        *ru*) yes=да;;
        *gr*) yes=ja;;
        #...
    esac

    yes | sed "s/y/$yes/"
}

Looks like sed is not needed, just yes $yes

myes(){
    case ${LANG,,} in
        *en*) yes=yes;;
        *ru*) yes=да;;
        *gr*) yes=ja;;
        #...
    esac

    yes "$yes"
}
Related