ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am writing a bash script and I want it to use the locale translations for common items such as 'Home', Web Browser', 'Terminal', 'Run'...
I know these are available in the system (with the correct locale packages installed) since it appears translated in the Gnome menu. How do I go about getting this information from the installed localization packages?
Thanks. I've been reading up on gettext but it all seems cryptic to me. I have to know what 'domain' (if that's correct) to get the translations from and the exact text in English... or something like that. If I could find some practical examples at least I could know if I'm doing it right. Most of the docs are prose...
It's probably quicker and easier to check the locale on startup of your script and use your own locally defined translations for your output. Of course unless you can speak Chinese it will probably only be good for Latin languages.
I had got as far as using nautilus as the domain and a quoted string as the word to translate, but my mistake was in trying to set it up to use a different language from inside a script. Of course this doesn't work.
It's ok in a shell but from a script it just ignores it.
which is not to say the translation won't work on a machine using a different LANG setting, but trying to make a test script is futile unless you are prepared to change your local env to another language to test it out.
Anyway ... lots to do.
I agree about creating your own translations too. If you don't and the machine where the script runs has a non standard locale location then your script won't work as expected. Also, not all the groovy words you might need will be in the standard files.
Because from what I had read (on this site and other places) you can't export an environment variable and use it directly in the same script(something about it being available to the child process, but not the parent). I had to jump through hoops to make my script call another script that exported a LANG setting, then try to call gettext to echo a word in that language. It did not work.
But it appears that I had made two mistakes.
Firstly I had assumed that I could pick any word to echo and it would magically be translated. Unfortunately, words like home or computer come out exactly the same !
Secondly, I had been concentrating on the export part and neglected to export TEXTDOMAIN in my script(s) correctly so it never seemed to take effect. Also, my calling of gettext was faulty, after trying to understand the concept from the link I provided above.
However, after further tests I have found that certain words work fine
Because from what I had read (on this site and other places) you can't export an environment variable and use it directly in the same script ...
I think you already found that is not correct. In case not here's a demonstration
Code:
c@CW8:~/d/bin/try$ cat test1.sh
#!/bin/bash
# Can we set an envar and have it available here and in a called scipt?
export TESTENVAR=foo
echo ${0##*/} $TESTENVAR
./test2.sh
c@CW8:~/d/bin/try$ cat test2.sh
#!/bin/bash
echo ${0##*/} $TESTENVAR
c@CW8:~/d/bin/try$ ./test1.sh
test1.sh foo
test2.sh foo
Does what you wrote meaning that, re-examining the problem in response to my question, you got it working? That would be nice
Well it was nearly 3am when I was trying it and things were getting a bit hazy.
I did find the advanced bash scripting guide section on localisation pretty cryptic, but bearing in mind I was trying to run stuff based on pretending to be in a different locale. This made things even less clear.
After seeing the response from the OP this morning and then you asked why not, I thought I'd have another go at it.
It was never made clear anywhere that I had to export the TEXTDOMAIN, only specify it.
I'm still looking for the LQ page I found last night where it was made plain that exporting was not possible if it was intended to apply to the current script. But I can't remember how I got there now !
So in a way, yes, your question prompted me to have another go.
It is always interesting to see other peoples ideas too.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.