localizing-shell-scripts-without bashisms, gettext or ...

Under development: PCMCIA, wireless, etc.
Message
Author
User avatar
L18L
Posts: 3479
Joined: Sat 19 Jun 2010, 18:56
Location: www.eussenheim.de/

Re: xdelta_gui

#61 Post by L18L »

rodin.s wrote:Same with version 0.5. Here is my translation. You can try it by yourself. It has something to do with UTF-8. Console messages were interesting.
I have used your file to /usr/share/locales/ru/xdelta_gui
-t12s from woof : did not translate, used UNDO button
-t12s v 0.5 : everything OK
-t12s from woof again: everything OK

Really strange

What are your console messages?

rodin.s
Posts: 344
Joined: Wed 22 Sep 2010, 19:50
Location: Ukraine

t12s console messages

#62 Post by rodin.s »

Here is console messages file.
Attachments
t12s_conslole.txt.gz
(713 Bytes) Downloaded 456 times

User avatar
L18L
Posts: 3479
Joined: Sat 19 Jun 2010, 18:56
Location: www.eussenheim.de/

Re: t12s console messages

#63 Post by L18L »

rodin.s wrote:Here is console messages file.
I see: just WARNINGs

# echo $LANG
de_DE.UTF-8
#
I have no such warnings and can read everything fine.
Attachments
t12s_ru.png
(6.13 KiB) Downloaded 588 times

cusco-travel-services
Posts: 8
Joined: Mon 02 Mar 2015, 22:35

Usage of [ -f $LOCALES ] && . $LOCALES

#64 Post by cusco-travel-services »

Hi L18L et al,

I've been using Linux for less than a year and 2 days ago I discovered this topic.

I like Technosaurus's idea for localisation of Bash scripts. It somewhat matches my thoughts of localisation for applications in that I think an indexed database containing all translations and using an Integer based index would be a more efficient mechanism instead of passing long strings of text as an index. There may be some valid reasons I don't know about but as someone who has programmed with databases for many years, I think that an indexed database should be much more efficient.

It appears that the following line of code is required each time a translation is to be accessed. Is that correct? If so, please explain for people new to Linux why it is needed each time.

Code: Select all

[ -f $LOCALES ] && . $LOCALES
Also, in version 0.5 of your script the following block is different to what I have seen used and quoted elsewhere.

Code: Select all

###################### copy this block into any other script that... ##########
app=\`basename \$0\`
T=/usr/share/locales # TEXTDOMAINDIR
# a one-liner to find existing translation file LOCALES allowing 'dialects'
[ ! -f \${T}/\${LANG%.*}/\${app} ] && LOCALES=\${T}/\${LANG%_*}/\${app} || LOCALES=\${T}/\${LANG%.*}/\${app}
[ -f \$LOCALES ] && . \$LOCALES # (re)load translation file (when using variables)
###################### ... uses this _ M _ method of internationalization #####
Notice, for example, that && is replaced by &&.

Thanks, Michael.

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

Re: Usage of [ -f $LOCALES ] && . $LOCALES

#65 Post by technosaurus »

cusco-travel-services wrote:Notice, for example, that && is replaced by &&.

Thanks, Michael.
That looks like the result of pasting to a webpage (where "&" is represented by "&")
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

cusco-travel-services
Posts: 8
Joined: Mon 02 Mar 2015, 22:35

Re: Usage of [ -f $LOCALES ] && . $LOCALES

#66 Post by cusco-travel-services »

technosaurus wrote:That looks like the result of pasting to a webpage (where "&" is represented by "&")
That's what I was thinking also.

What about all the \ characters?

cusco-travel-services
Posts: 8
Joined: Mon 02 Mar 2015, 22:35

#67 Post by cusco-travel-services »

Additionally, if [ -f $LOCALES ] && . $LOCALES is required each time, which of the following will work?

Code: Select all

[ -f $LOCALES ] && . $LOCALES # load translation file
[[ $DISPLAY ]] || { echo "${_M_04:-There is no xserver running. Exiting...}" ; exit 1 ; }
or

Code: Select all

[ -f $LOCALES ] && . $LOCALES # load translation file
NoXserver="${_M_04:-There is no xserver running. Exiting...}"
[[ $DISPLAY ]] || { echo "$NoXserver" ; exit 1 ; }

User avatar
L18L
Posts: 3479
Joined: Sat 19 Jun 2010, 18:56
Location: www.eussenheim.de/

Re: Usage of [ -f $LOCALES ] && . $LOCALES

#68 Post by L18L »

Hello Michael
you wrote:I like Technosaurus's idea for localisation of Bash scripts. It somewhat matches my thoughts of localisation for applications in that I think an indexed database containing all translations and using an Integer based index would be a more efficient mechanism instead of passing long strings of text as an index. There may be some valid reasons I don't know about but as someone who has programmed with databases for many years, I think that an indexed database should be much more efficient.
Thanks for making me search about official documentation about this method using index _M_nnn.

Actually not found any more.

Nobody has been using it.
And yes, there are reasons for not using it in Puppy.

So for me it is history now. 8)

I am thinking if you are developper, coder and maintainer all in one person you could give it a try.

Reloading the translation file is of course not always required,
-- only if the variable in the message has changed/another value than last loading.
-- and never required if command eval is used.

You could try everything if you had a Puppy Linux installed...
I am assuming you have not because of your
Notice, for example, that && is replaced by &&.
Cheers
L

EDIT:
...and please do NOT use that crazy automatic translations
Das Ziel des Cusco Travel Services ist, Reisende zu versorgen, zu und vom Cusco Gebiet von Peru, der beste Dienst der möglich ist für Ihre Bedürfnisse in Unterkunft, Passagiertransport und Touren wie zum die Inca-Spur, Beispiel Mountain-bike und Pferderücken Fahrend Ausflüge und Vorbehalte für Zugfahrkarten für die Züge der Inca Rail Bedienung zwischen Ollantaytambo und Machu Picchu und den Zügen der Peru Rail Bedienung zwischen Cusco (Poroy), Ollantaytambo und Machu Picchu und zwischen Cusco und Puno (See Titicaca).
:shock: :x

cusco-travel-services
Posts: 8
Joined: Mon 02 Mar 2015, 22:35

Re: Usage of [ -f $LOCALES ] && . $LOCALES

#69 Post by cusco-travel-services »

Hi L,

Thanks for your reply. I will continue modifying the script I want to interface with t12s and I'll let you know how it goes.
L18L wrote:...and please do NOT use that crazy automatic translations
Das Ziel des Cusco Travel Services ist, Reisende zu versorgen, zu und vom Cusco Gebiet von Peru, der beste Dienst der möglich ist für Ihre Bedürfnisse in Unterkunft, Passagiertransport und Touren wie zum die Inca-Spur, Beispiel Mountain-bike und Pferderücken Fahrend Ausflüge und Vorbehalte für Zugfahrkarten für die Züge der Inca Rail Bedienung zwischen Ollantaytambo und Machu Picchu und den Zügen der Peru Rail Bedienung zwischen Cusco (Poroy), Ollantaytambo und Machu Picchu und zwischen Cusco und Puno (See Titicaca).
:shock: :x
Google Translate and Im Translator are what I used to create the German Translations for my website. Is "Google Translate" what you are referring to regarding "automatic translations"?

How bad is the German?

I wonder if Google Translate is better now? :lol:

Thanks for visiting my website.

Hasta luego, Michael.

cusco-travel-services
Posts: 8
Joined: Mon 02 Mar 2015, 22:35

Re: Usage of [ -f $LOCALES ] && . $LOCALES

#70 Post by cusco-travel-services »

L18L wrote:Reloading the translation file is of course not always required,
-- only if the variable in the message has changed/another value than last loading.
-- and never required if command eval is used.
HI L18L,

Are you saying that eval can be placed in a translated message that contains a variable reference (e.g. $variable) to resolve the value of the variable?

Can you give an example of using eval in the context to which you are referring?

Thanks, Michael.

User avatar
L18L
Posts: 3479
Joined: Sat 19 Jun 2010, 18:56
Location: www.eussenheim.de/

Re: Usage of [ -f $LOCALES ] && . $LOCALES

#71 Post by L18L »

cusco-travel-services wrote:Are you saying that eval can be placed in a translated message that contains a variable reference (e.g. $variable) to resolve the value of the variable?

Can you give an example of using eval in the context to which you are referring?
eval is outside.
In the context of gettext:

Code: Select all

# #example
#echo "$(gettext 'hello $adjective world')"
hello $adjective world
# 
# 
# adjective='wonderful'
# echo "$(gettext 'hello $adjective world')"
hello $adjective world
# 
# echo "$(eval echo `gettext 'hello $adjective world'`)"
hello wonderful world
#
And in our "new" syntax:

Code: Select all

# echo "$eval ${_M_1:-hello $adjective world}"
 hello wonderful world
# 
Try it yourself please.
regards

EDIT
Converting from and to .po format can be done (and has be done) simply of course.

cusco-travel-services
Posts: 8
Joined: Mon 02 Mar 2015, 22:35

Re: Usage of [ -f $LOCALES ] && . $LOCALES

#72 Post by cusco-travel-services »

L18L wrote:eval is outside.
In the context of gettext:

Code: Select all

# #example
#echo "$(gettext 'hello $adjective world')"
hello $adjective world
# 
# 
# adjective='wonderful'
# echo "$(gettext 'hello $adjective world')"
hello $adjective world
# 
# echo "$(eval echo `gettext 'hello $adjective world'`)"
hello wonderful world
#
And in our "new" syntax:

Code: Select all

# echo "$eval ${_M_1:-hello $adjective world}"
 hello wonderful world
# 
Try it yourself please.
regards

EDIT
Converting from and to .po format can be done (and has be done) simply of course.
Hi L18L,

With a bit of online research and some trial and error, I actually managed to get eval working before I received your last reply although I don't understand all the mechanisms behind it. :)

Hence, I have discovered and implemented the following techniques.

In order for a variable to NOT be resolved, when used in a String assigned to another variable, it needs to be escaped with a Backslash (i.e. \) before the $ symbol.

For example, if we have a variable named var1 which has a value of 123 (var1=123), then, the following statement ...

Code: Select all

_M_01="This string references the variable var1 by using \$var1"
... will cause _M_01 to have a value of This string references the variable var1 by using $var1

Otherwise, if the following statement was used ...

Code: Select all

_M_01="This string references the variable var1 by using $var1"
... the resulting value of _M_01 would be This string references the variable var1 by using 123

A variable reference, within a String, needs to be escaped with a Backslash when you want to use the eval Command to evaluate the string stored by the Referencing variable (i.e. _M_01) to retrieve the current value for variables referenced by _M_01.

This is important to consider, when included Code Source files reference variables of the including Script file, as referenced variables will be resolved immediately, when a Code Source file is included, unless they are escaped with a Backslash.

In relation to the techniques proposed by technosaurus, using these methods allows the retrieval of the current value of a Referenced variable (e.g. var1) without the need to reload a Code Source file.

For eval and/or echo to correctly parse New Lines, variables declared in the executed Script file or a Code Source file need to use '\n' for a New Line (Line Break). Actual line breaks can't be used.

The following code is an example of what I have implemented.

Code Source file

Code: Select all

_M_01="Other language before new lines'\n\n'Other language after new lines \$var2"
Script file

Code: Select all

var1="Text before new lines'\n\n'Text after new lines \$var2"
var1Yad=`eval "echo "${_M_01:-$var1}""`
yad --title "Title" --width 580 --center --text "$var1Yad"
However, with t12s, when entering '\n' or '\n\n' for a message the Single Quotes ' were being changed to Apostrophes ´ and I couldn't determine a way to escape the Single Quotes to stop them from being changed to Apostrophes which made testing difficult.

So, I commented the following line to prevent it from being executed.

Code: Select all

#new_msgtr=`echo $new_msgtr | sed -e "s${delim}'${delim}´${delim}g"` # change quote to apostrophe
When restarting t12s, though, the Backslashes had been removed so that '\n\n' had become 'nn'.

Which line of code is causing that to happen? Could it be the following?

Code: Select all

sed -r "s${delim}="${delim}\n${delim}g;s${delim}"\$${delim}${delim}g" ${T}/${this_LANG}/${app} > /dev/shm/t # just for inspection
I also learnt that Round Brackets, ( and ), need to be escaped with a Backslash.

A bug I noticed with t12s is that when you click on the Ok button, to re-edit a message, the message is deleted and then you have to click on the Ok button again to re-enter text. :shock:

Thanks, Michael.

User avatar
L18L
Posts: 3479
Joined: Sat 19 Jun 2010, 18:56
Location: www.eussenheim.de/

#73 Post by L18L »

Hi Michael,

you really want to dig into that stuff!

I have reread this thread from start and played with code again.

Without yad, I do not like yad any more.
I have used text editor geany (you can use very long lines and have them wrapped in the text editor)
New line is not really needed: we translate words, phrases, paragraphs,...
New line can b e set outside of translation I am sure.

I hav e tried this little example in our momanager, see http://murga-linux.com/puppy/viewtopic. ... &start=245 please.

Code: Select all

# sample_i18n_t12s.pot
# created by /usr/sbin/t12s_xgettext 
# on Thu Mar 12 17:43:28 CET 2015 
# from /root/my-applications/bin/sample_i18n_t12s.sh
msgid ""
msgstr ""
"Project-Id-Version: sample_i18n_t12s\n"
"Language: de_BE\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Last-Translator: fido@localhost\n"
"Language-Team: none\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"

# _M_1
msgid "hello world"
msgstr "Hallo Welt"

# _M_2
msgid "crazy"
msgstr ""

# _M_3
msgid "hello $x world"
msgstr ""

# _M_11
msgid "Now an example with some text before a new line."
msgstr ""

# _M_12
msgid "Now the example with some text after the new line."
msgstr ""
and this the the script

Code: Select all

#!/bin/sh
export TEXTDOMAIN=sample_i18n_t12s
LOCALES=/usr/share/locales/${LANG%_*}/$TEXTDOMAIN
[ -f $LOCALES ] && . $LOCALES

x="${_M_2:-crazy}"

echo ${_M_1:-hello world}

[ -f $LOCALES ] && . $LOCALES
echo ${_M_3:-hello $x world}

echo ${_M_11:-Now an example with some text before a new line.}
echo ${_M_12:-Now the example with some text after the new line.}
 
echo "
that's it."
running (translation used)

Code: Select all

# ./s*t12*
Hallo Welt
Hallo verrückt Welt
Jetzt ein Beispiel mit Text vor einer neuen Zeile.
Jetzt das Beispiel mit Text nach der neuen Zeile.

that's it.
#
My resume: use of eval was wrong. keep \n off .
Translators know po files. Use them.

Unforeseen nasty things may happen here.
Gettext is on the safe side:
If you change your code the not updated translation is not wrong, just not translated.

Thank you for making me look into that old (pre-momanager) stuff.
L

cusco-travel-services
Posts: 8
Joined: Mon 02 Mar 2015, 22:35

#74 Post by cusco-travel-services »

Hi L,

Sorry for the late response.

Why do you not like yad any more?

What are you using instead of yad?

I think yad and zenity could use more formatting options. For example, fixed width columns that vertically expand for Form fields.

I visited the other topic and downloaded the momanager-20150309.pet file but I wasn't able to extract anything from it. How do I do that?

Are you using momanager to create the .pot files?

Is the Script, you posted, using gettext to read from a .mo file?

In the Script you have the following for example.

Code: Select all

${_M_1:-hello world}


Where is the _M_1 variable declared?

I don't understand, exactly, what you are trying to say with the following.
L18L wrote:My resume: use of eval was wrong. keep \n off.
I think the following is probably only a management issue. If you change text that needs an updated translation, then, a new template should be issued to the translators so that updated translations can be provided.
L18L wrote:If you change your code the not updated translation is not wrong, just not translated.
For example, a template could be like the following.

Code: Select all

#_M_01="Text to be translated"
_M_01=""

#_M_02="More text to be translated"
_M_02=""
In its present incarnation t12s can be used to help create a Source template file which you can edit directly for the translations instead of trying to enter the translations via the t12s interface.

With some extra "spit and polish" I think it would be a very worthwhile utility. :)

Thanks and hasta pronto, Michael.

Post Reply