LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-18-2010, 02:55 PM   #1
librano
Member
 
Registered: Jul 2004
Location: Here, there and everywhere.
Distribution: Arch+KDE, Linux Mint Fluxbox CE
Posts: 163

Rep: Reputation: 31
Bash script: How to get locale translations


Hi all!

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?

I am running Ubuntu 9.10.

Thanks in advance.
 
Old 04-18-2010, 05:27 PM   #2
smoker
Senior Member
 
Registered: Oct 2004
Distribution: Fedora Core 4, 12, 13, 14, 15, 17
Posts: 2,279

Rep: Reputation: 250Reputation: 250Reputation: 250
https://help.ubuntu.com/community/EnvironmentVariables

You still have to know the translated words in your bash script though. Unless you can figure out how to use gettext ...

Last edited by smoker; 04-18-2010 at 06:41 PM.
 
Old 04-18-2010, 05:44 PM   #3
librano
Member
 
Registered: Jul 2004
Location: Here, there and everywhere.
Distribution: Arch+KDE, Linux Mint Fluxbox CE
Posts: 163

Original Poster
Rep: Reputation: 31
Yes, I know about env variables. But that's not quite what I was looking for. Let me put it this way:

What command should I run to get the translation of 'Home' in the current language as it appears in the Nautilus bookmarks pane or Gnome menu?
 
Old 04-18-2010, 06:44 PM   #4
smoker
Senior Member
 
Registered: Oct 2004
Distribution: Fedora Core 4, 12, 13, 14, 15, 17
Posts: 2,279

Rep: Reputation: 250Reputation: 250Reputation: 250
You have to find out how to use gettext in bash.

http://tldp.org/LDP/abs/html/localization.html

I'm still trying ...
 
Old 04-18-2010, 07:03 PM   #5
librano
Member
 
Registered: Jul 2004
Location: Here, there and everywhere.
Distribution: Arch+KDE, Linux Mint Fluxbox CE
Posts: 163

Original Poster
Rep: Reputation: 31
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...
 
Old 04-18-2010, 07:16 PM   #6
smoker
Senior Member
 
Registered: Oct 2004
Distribution: Fedora Core 4, 12, 13, 14, 15, 17
Posts: 2,279

Rep: Reputation: 250Reputation: 250Reputation: 250
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.
 
Old 04-18-2010, 07:38 PM   #7
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
http://mywiki.wooledge.org/BashFAQ/098
 
Old 04-18-2010, 07:41 PM   #8
librano
Member
 
Registered: Jul 2004
Location: Here, there and everywhere.
Distribution: Arch+KDE, Linux Mint Fluxbox CE
Posts: 163

Original Poster
Rep: Reputation: 31
I really don't want to start maintaining translations when adequate ones already exist and are already more complete than I will ever get it.

For some reference, here is the basic syntax for gettext

Code:
export TEXTDOMAIN=nautilus;  echo "$(gettext "Accessed:")"
Where TEXTDOMAINs are .mo files located in an LC_MESSAGES folder like /usr/share/locale/fr/LC_MESSAGES/

I guess this is closer to what I was looking for... If anyone has a any more ideas please to mention them... apart from maintaining translations
 
Old 04-18-2010, 08:13 PM   #9
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
You *should* maintain your own translations. Trying to rely on external translations that may or may not exist is just Wrong.
 
Old 04-18-2010, 08:50 PM   #10
smoker
Senior Member
 
Registered: Oct 2004
Distribution: Fedora Core 4, 12, 13, 14, 15, 17
Posts: 2,279

Rep: Reputation: 250Reputation: 250Reputation: 250
Thanks for those links and explanations.

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.
 
Old 04-18-2010, 10:40 PM   #11
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by smoker View Post
... 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.
Why not?
 
Old 04-19-2010, 04:51 AM   #12
smoker
Senior Member
 
Registered: Oct 2004
Distribution: Fedora Core 4, 12, 13, 14, 15, 17
Posts: 2,279

Rep: Reputation: 250Reputation: 250Reputation: 250
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

as in :


Code:
#!/bin/bash

export LANG=de_DE

export TEXTDOMAIN=nautilus

echo $LANG

echo "$(gettext "Network")"

export LANG=en_GB

unset TEXTDOMAIN

echo $LANG
Of course in a real world setting I wouldn't need to export a language as it would be part of the locale anyway.

Last edited by smoker; 04-19-2010 at 04:55 AM.
 
Old 04-19-2010, 08:16 AM   #13
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by smoker View Post
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
 
Old 04-19-2010, 08:39 AM   #14
smoker
Senior Member
 
Registered: Oct 2004
Distribution: Fedora Core 4, 12, 13, 14, 15, 17
Posts: 2,279

Rep: Reputation: 250Reputation: 250Reputation: 250
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.

Last edited by smoker; 04-19-2010 at 08:40 AM.
 
1 members found this post helpful.
  


Reply

Tags
bash, locale, translation



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
locale: Cannot Set LC_ALL to default locale: No such file or directory. asym Linux - General 11 10-24-2018 05:54 PM
after-Installation errors. locale error. unsupported locale settings and two more gh0st Fedora 1 05-02-2008 05:06 AM
howto set 'locale LC_ALL=$MYVAR' within a script?? minike Slackware 10 01-29-2007 02:11 AM
locale: Cannot set LC_ALL to default locale: Invalid argument GadgetWiz Mandriva 1 03-31-2006 11:38 PM
bash: locale: command not found GabeF Linux - Software 1 07-10-2003 05:23 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:50 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration