5

I want to use a specific locale for some application but not for the rest of my system. How do I do this?

UTF-8
  • 5,710
  • 10
  • 31
  • 67

2 Answers2

7

Theoretical part

You can change the locale or only parts of it for a specific process before you create it by changing its environment.

Check it out by launching gnome-calculator via

env LC_NUMERIC=de_DE.UTF-8 gnome-calculator

if you currently use a period as your decimal point and via

env LC_NUMERIC=en_IE.UTF-8 gnome-calculator

if you currently use a comma as you decimal point.

The gnome-calculator process will use the locale stated before its call.

Note that this doesn't persist if you close the application and open it via the Dash or just call gnome-calculator in a terminal. In fact, it is process-specific and you can use several instances of gnome-calculator, some of them using periods and some of them using commas as their decimal points. Check it out by running gnome-calculator in a different terminal. It will use your normal settings.

Finding the right locale

You don't need to find a single locale which matches your needs for everything. Instead, it's sufficient to find one that matches what you want in a specific localization category. Here, we only care about the time format which can be changed by manipulating the environment variable LC_TIME.

You're probably interested in the international time format (ISO 8601) or the time format of some country of which you know the time format. For the former, use en_DK.UTF-8. For the latter, use the 2-letters abbreviation of the language in lowercase letters, an underscore, the 2-letters abbreviation for the county in capital letters, and then .UTF-8. For example, en_IE.UTF-8 is Irish English, de_DE.UTF-8 is German German, de_CH.UTF-8 is Swiss German, and fr_CH.UTF-8 is Swiss French.

Practical part

Now that you learned a bit about how cool Linux is, let's get to the practical part.

You probably launch your application via the Dash. If you do so, you use a desktop file. Find your desktop file. It's either in /usr/share/applications or in ~/.local/share/applications. The former is system-wide, the latter is user-specific.

I'll use Firefox as the example application. It's desktop file is /usr/share/applications/firefox.desktop.

After you found the desktop file you want to manipulate, copy it to your user's desktop file folder:

cp /usr/share/applications/firefox.desktop ~/.local/share/applications

You can now manipulate the copy such that your changes only affect the user-local version.

Open it in a text editor. It doesn't matter whether you use a command line one or a graphical one. Normal Ubuntu comes with GEdit as its default graphical text editor:

gedit ~/.local/share/applications/firefox.desktop

If it doesn't work because you use a flavor of Ubuntu which doesn't come with GEdit, just use nano:

nano ~/.local/share/applications/firefox.desktop

Find the line which begins with Exec=. For Firefox, it's Exec=firefox %u. Then take whatever comes after Exec= and get it into this format with the locale you want instead of en_DK.UTF-8:

Exec=env LC_TIME=en_DK.UTF-8 firefox %u

Of course, you need to substitute en_DK.UTF-8 by whatever locale you want to use.

Save the file.

After you made your changes, you need to make the desktop file executable. The file you copied was executable but that property isn't copied, so you need to set it again for the new file. Do do so, run this command:

chmod +x ~/.local/share/applications/firefox.desktop

Now, restart your application.

Dash usually updates the desktop files automatically but on slow systems, it might take a while. Or at least it did in some version. If it doesn't work instantly, log out and back in again.

UTF-8
  • 5,710
  • 10
  • 31
  • 67
  • @EliahKagan Yes, I actually tested it this time (Ubuntu 16.04; Unity 7.4.0). Both with Firefox and Gnome Calculator. Exec=bash -c "(LC_NUMERIC=de_DE.UTF-8; gnome-calculator)" makes Gnome Calculator use a comma as the decimal point on my system, even though it normally uses a period. The reason I don't use env is that it's very restricted which means that I have to think about what will and what will not work. I just want full bash power everywhere where it looks like full bash power could be present. The reason I use a subshell is that I want to make sure the changes are contained. – UTF-8 Sep 07 '17 at 15:28
  • As we drift away from the topic, we should continue this discussion in chat. – UTF-8 Sep 07 '17 at 16:22
1

I prefer a wrapper shell script for doing it. First create the script with a text editor of your choice. Example script:

$ cat ~/bin/firefox
#!/bin/sh
export LC_TIME=en_DK.UTF-8
exec /usr/bin/firefox $@

Then make it executable:

chmod +x ~/bin/firefox

Advantages with this method:

  • No local copy of the .desktop file which would override possible changes to the original .desktop file when the package is updated.
  • Works both when starting the application from the graphical environment and when starting it from terminal.
Gunnar Hjalmarsson
  • 33,540
  • 3
  • 64
  • 94