1

I wrote quite a long script in Python which in several places calls git with Python subprocess and matches things that git returns for example:

nothing to commit, working tree clean
fast-forwardable
up to date

A colleague went to use the script and it did not behave as expected as their git was using a local German locale with messages like:

nichts zu committen, Arbeitsverzeichnis unverändert

I did not think of that at the time. Can see here and here some ideas for changing the language permanently.

Of course I don't want to alter the setup on the PCs where my script runs, is it possible to tell git to return in en_US just for a single command?

Actually, when I wrote the script I had my system set to en_GB and wonder if my git used en_GB or en_US and if there is any difference. Is it possible to ask git to tell you what language it is using? This is a secondary issue however, if it were possible to tell git to temporarily use en_US would be enough.

cardamom
  • 6,873
  • 11
  • 48
  • 102
  • 1
    Have you tried with [this?](https://askubuntu.com/questions/320661/change-gits-language-to-english-without-changing-the-locale) – Jalil May 02 '19 at 14:13
  • The thing with that is it involves doing something to the .bashrc file on the machine where it is being run and is such not a 'temporary' change. Was hoping for some kind of bash commands to add to a git command which overrides whatever git otherwise is pulling out of .bashrc – cardamom May 02 '19 at 14:17
  • 1
    The robust approach would be to use plumbing or commands meant for scripting such as `git status --porcelain`, which is unaffected by user configuration, instead of parsing messages meant for human consumption. – Benjamin W. May 02 '19 at 15:38

4 Answers4

3

You can actually prefix any command within a Shell with any variable setting, that would apply only for this command, whether it's git or any other else. Simply type it then separate it from your command with a blank:

$ LANG=fr_FR sh -c 'echo $LANG'
fr_FR
$ echo $LANG
en_us.UTF-8

You probably want to use C, however, to be sure to use the default language for the command you run:

$ LANG=C git

Keep in mind, though, that the chosen language may not be available if it's not have been compiled in. Regular english, however, should almost always be there as it's in general the one the software's strings are written in.

Obsidian
  • 3,719
  • 8
  • 17
  • 30
2

try for one line:

LANG=en_US git ...your command...

or for this bash session:

export LANG=en_US
UtLox
  • 3,744
  • 2
  • 10
  • 13
2

Prefix the git command with the desired language reference to apply the change only for that command.

e.g.

Default (en_US.UTF-8)

$ git status
fatal: Not a git repository (or any of the parent directories): .git

In German,

$ LANG=de_DE.UTF8 git status
fatal: Kein Git-Repository (oder irgendein Elternverzeichnis): .git

LANG Format:

<LANGUAGE>_<TERRITORY>.<CODESET>[@<MODIFIERS>]
Anubis
  • 6,995
  • 14
  • 56
  • 87
2

Set LANG somewhere at the beginning of the script:

#!/bin/sh

LANG=en_US
export LANG

The setting works for the current shell (that is, the script) and subprocesses spawned from it including git.

phd
  • 82,685
  • 13
  • 120
  • 165