eval_gettext example

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
User avatar
don570
Posts: 5528
Joined: Wed 10 Mar 2010, 19:58
Location: Ontario

eval_gettext example

#1 Post by don570 »


See rules of eval_gettext in final post


This example shows how to use eval_gettext.
eval_gettext is an app that returns a textstring along with the contents of
a variable

gettext can only return some text string.

Instructions:

Save this script as 'script_example'. Check that it is executable and
place in an executable path. When the script is run some windows will
pop up. Check how 'echo' command is handled in the terminal.

Note that near beginning of script you must have written the script name
to tell momanager the script name to search for.

export TEXTDOMAIN=script_example
export OUTPUT_CHARSET=UTF-8

Warning! Do Not Put In line '. gettext.sh '
The bash manual says to do that but it just causes substitution problems.

Code: Select all

#!/bin/bash
#
export TEXTDOMAIN=script_example
export OUTPUT_CHARSET=UTF-8

TIME=4;VERSION=6.3
PATHWAY="/usr/share/My folder"
DESTINATION="${PATHWAY##*/}"
echo -e "`eval_gettext \" DESTINATION = \\\${PATHWAY##*/} \n \"; echo`"

Xdialog --msgbox "`eval_gettext \" DESTINATION = \\\${PATHWAY##*/} \n \"; echo`" 0 0

gxmessage "`eval_gettext \"Application \\\$VERSION\"; echo`  $(gettext 'is running')"

echo -e "$( date )    `eval_gettext \"My Recorder \\\$VERSION\"; echo`  \n"
echo  "$( date )    `eval_gettext \"My Recorder \\\$VERSION\"; echo`"  > "/root/my_log"
Xdialog  --wrap --title "$(gettext 'Conversion underway.')"   \
--yesno "`eval_gettext \"\nTime of conversion:  \\\$TIME seconds.\"; echo`\n" 0 0

export EXAMPLE='
<window  title="'"`eval_gettext \"Tom's Recorder \\\$VERSION\"; echo`"'">
 
      <frame '"`eval_gettext \"Dick's Recorder \\\$VERSION\"; echo`"'>
      <text width-request="300">     
      <label>"'"`eval_gettext \"Harry's Recorder \\\$VERSION\"; echo`"'"</label>
      </text>
       <button>     
      <label>"'"`eval_gettext \"Charlie's Button \\\$VERSION\"; echo`"'"</label> 
         <action>Xdialog --msgbox "'"$(gettext "Mary's message") $(gettext 'costs \$10')"'" 0 0</action>
      </button>
      </frame>
</window>'

gtkdialog -p EXAMPLE 

_______________________________________________________

To check how it is translated into a foreign language

Instructions:

1) load devx file for your puppy
2) install poedit (optional)
3) set language for your computer , for example French
4) Launch momanager

Utilities > Momanager

5) Tick poedit box if you have installed it
6) Find the script name on list
7) Click 'Create'

Here is resulting poedit output. It's easy to translate now.
The 'script_example.mo' file is created when you quit poedit.

Tip: Be sure that your script contains only ascii characters.
Test you script with this command in the termiinal.
The line number where there is a bad character usually utf-8 will
be signalled.

Code: Select all

xgettext -L shell script_example -o -
Note:

1) To stop a substitution I put a slash in front of dollar sign($)
$(gettext 'costs \$10')


2) Note that eval_gettext works very well with the apostrophe

Image
Last edited by don570 on Mon 08 Dec 2014, 19:38, edited 7 times in total.

User avatar
vicmz
Posts: 1262
Joined: Sun 15 Jan 2012, 22:47

#2 Post by vicmz »

I didn't understand it until I read your tutorial. Thank you, don570.
[url=http://murga-linux.com/puppy/viewtopic.php?t=76948]Puppy Linux en español[/url]

User avatar
ASRI éducation
Posts: 3197
Joined: Sat 09 May 2009, 12:10
Location: France
Contact:

#3 Post by ASRI éducation »

It is a good idea to give advice about "eval_gettext."
Users/contributors will appreciate.
Regards
Projet ASRI éducation => [url=http://asri-education.org/]Association[/url] | [url=http://forum.asri-education.org/]Forum[/url] | [url=http://dl01.asri-education.org/]Dépôt[/url] | [url=http://kids.asri-education.org/]Espace kids[/url]

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

eval_gettext example

#4 Post by L18L »

I have found that eval_gettext if really not needed.

Instead one can use

Code: Select all

eval echo gettext "text"
Tried your exemple (just Recorder3):

Code: Select all

#!/bin/sh

#. gettext.sh

TIME=4;VERSION=6.3 

export EXAMPLE='
<window  title="'"`eval echo gettext \"My Recorder1 \\\$VERSION\"; echo`"'">
 
      <frame '"`eval echo gettext \"My Recorder2 \\\$VERSION\"; echo`"'>
      <text width-request="300">     
      <label>'$(eval echo gettext "My Recorder3 $VERSION" )'</label>
      </text>
      </frame>
</window>'

gtkdialog -p EXAMPLE
see also http://www.murga-linux.com/puppy/viewtopic.php?t=92224 please

User avatar
don570
Posts: 5528
Joined: Wed 10 Mar 2010, 19:58
Location: Ontario

#5 Post by don570 »

To L18L: I can't get your script to work. The word 'gettext' is returned as text string

_____________________________________________________

I rewrote the script to show some new abilities of eval_gettext.
Click on the button to see the result. :lol:

1) button label
2) Launching Xdialog with a button. This can be tricky , especially with an apostrophe
3) some examples with apostrophes are given
4) I show how ${ ... } can be handled
_______________________________________________

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

#6 Post by L18L »

don570 wrote:To L18L: I can't get your script to work. The word 'gettext' is returned as text string
...
Sorry :oops:
You are right. 8)
Thanks for feed back :D

back ticks missing :!:
Attachments
eval_echo_gettext_without_.gettext.sh .png
(26 KiB) Downloaded 489 times

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

Re: eval_gettext example

#7 Post by L18L »

don570 wrote:Note:
2) I couldn't get ${PATHWAY##*/} to work with eval_gettext so I created
a new variable instead to represent the string
DESTINATION="${PATHWAY##*/}"
That's interesting.

I have been trying and came to this solution.
Attachments
eval_echo_gettext_without_.gettext.sh2.png
without .gettext.sh again
(17.01 KiB) Downloaded 464 times

User avatar
don570
Posts: 5528
Joined: Wed 10 Mar 2010, 19:58
Location: Ontario

#8 Post by don570 »

I have been trying and came to this solution.
However to get Momanager and poeditor to work properly with eval_gettext
a forward slash is needed in front of weak quote " and three slashes \\\
is needed in front of dollar sign.

Don't be fooled that the script seems to work. You have to check that it
truely works by running momanger , selecting script and clicking create.

______________________________________________________
Last edited by don570 on Sat 22 Nov 2014, 18:57, edited 1 time in total.

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

#9 Post by L18L »

don570 wrote:Don't be fooled that the script seems to work. You have to check that it
truely works by running momanger , selecting script and clicking create.
/root/my-applications/bin/MyFolder wrote:#!/bin/sh
export TEXTDOMAIN=MyFolder

TIME=4;VERSION=6.3
PATHWAY="/usr/share/$(gettext 'My folder')"
DESTINATION="${PATHWAY##*/}"
#echo -e "`eval_gettext " DESTINATION = \\\$DESTINATION \n "; echo`"

Xdialog --msgbox "$(eval echo `gettext 'DESTINATION = ${PATHWAY##*/}'`)" 0 0
/tmp/momanager/MyFolder.po wrote:msgid ""
msgstr ""
"Project-Id-Version: MyFolder VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2014-11-21 19:46+0100\n"
"PO-Revision-Date: 2014-11-21 19:37+0100\n"
"Last-Translator: fido@localhost\n"
"Language-Team: none\n"
"Language: de\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"

#: /root/my-applications/bin/MyFolder:5
msgid "My folder"
msgstr "Mein Ordner"

#: /root/my-applications/bin/MyFolder:9
msgid "DESTINATION = ${PATHWAY##*/}"
msgstr "ZIELORT = ${PATHWAY##*/}"
Check was successful

Sorry of being off topic
topic is: eval_gettext
... and I am writing about eval `gettext`which does not need gettext.sh
Attachments
Forget_eval_gettext.png
(4.44 KiB) Downloaded 412 times

User avatar
don570
Posts: 5528
Joined: Wed 10 Mar 2010, 19:58
Location: Ontario

#10 Post by don570 »

I changed the first example by getting rid of the line . gettext.sh

I checked and the line

Code: Select all

. gettext.sh 
shouldn't be used. It was causing the substitution problem.

I got that line from the Advanced bash manual by Mendel Cooper
but he was wrong.

... and L18L has simpler method. However watch out for apostrophe
if you use L18L method since strong quotes are in use by gettext application

Code: Select all

gettext 'DESTINATION = ${PATHWAY##*/}'
Putting an apostrophe in an test expression which uses strong quotes is a NO-NO.

I like to practise this by opening a terminal and typing

Code: Select all

echo 'Harry's Button'
and the output will be wrong.

Code: Select all

echo "Harry's Button"
Now the output will be right.


______________________________________________
Last edited by don570 on Wed 03 Dec 2014, 23:24, edited 1 time in total.

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

#11 Post by L18L »

don570 wrote:... and L18L has simpler method.
simpler does not mean better!

"My" method is just borrowed from shinobar's

Code: Select all

eval_gettext () {

  local myMESSAGE=$(gettext "$1")

  eval echo "$myMESSAGE"

}
But look inside /usr/bin/gettext.sh:

Code: Select all

# eval_gettext MSGID
# looks up the translation of MSGID and substitutes shell variables in the
# result.
eval_gettext () {
  gettext "$1" | (export PATH `envsubst --variables "$1"`; envsubst "$1")
}
.
.
.
# Note: This use of envsubst is much safer than using the shell built-in 'eval'
# would be.
.
.
eval echo ... is unsafe :cry:

See also all comments on Simplifying translations please.

Thus stay safely with eval_gettext :wink:

User avatar
don570
Posts: 5528
Joined: Wed 10 Mar 2010, 19:58
Location: Ontario

#12 Post by don570 »

Good page Simplifying Translations

I had missed that somehow.
_____________________________________________________

User avatar
don570
Posts: 5528
Joined: Wed 10 Mar 2010, 19:58
Location: Ontario

#13 Post by don570 »

I am working on a script by xanada --> pupmemo - keeps dated memos

I noticed that the line
. gettext.sh

is needed when using eval_gettext in the gtkdialog variable ---> MEMO

Here's the code

Code: Select all

#! /bin/bash
#  dec 2  -written by xanada modified by don

VERSION=1.0
mkdir -p /var/memo
lf=`wc -l /var/memo/memo.txt  2>/dev/null | cut -c -3`; lf=$(($lf-30))
tail -30 /var/memo/memo.txt  2>/dev/null | sort > /var/memo/simple 
head -$lf /var/memo/memo.txt  2>/dev/null | sort > /var/memo/resto 

export TEXTDOMAIN=pupmemo.sh
export OUTPUT_CHARSET=UTF-8
. gettext.sh

HEADING="$(eval echo `gettext 'Welcome to PupMemo $VERSION'`)"

export MESSAGE="
$(gettext "Enter the date and write your message.

Click 'Insert Date' will sort the dates, then write the
memo entry next to the date.

Save the message if you want to read it later.
")
"
export MEMO='
<window title="'"`eval_gettext \"PupMemo \\\$VERSION\"; echo`"'">
    <vbox height-request="500">
    <hbox homogeneous="true">             
             <text use-markup="true"><label>"<b>'$HEADING'</b>"</label></text>              
             <pixmap>
			 <height>40</height>
			 <width>40</width>
             <input file>/usr/share/icons/woofq.png</input>
             </pixmap>              
    </hbox>         
    <frame>
            <edit name="*retto" left-margin="3" right-margin="3" justification="0" wrap-mode="2">
     <variable>EDITOR</variable>
     <input file>/var/memo/simple</input>
     </edit>
     </frame>
    <hbox>
    <button space-expand="false" space-fill="false" tooltip-text=" '$(gettext 'Help')' ">
    <input file stock="gtk-help"></input>
     <action>Xdialog --title "$(gettext "Help")"  --left --msgbox " $MESSAGE " 0 0 &</action>
	 </button>
	 <text space-expand="true" space-fill="true">
            <label>""</label> 
       </text>
     <button><label>"'"$(gettext "Insert Date")"'"</label>
     <action>printf "`date +%Y.%m.%d\ \ \ %X`" >> /var/memo/simple</action>
     <action>sort -d /var/memo/simple -o /var/memo/simple</action>
     <action>refresh:EDITOR</action>
     </button>
     <button use-stock="true" label="gtk-clear">
     <action function="clear">EDITOR</action>
	 </button>
     <button use-stock="true" label="gtk-save">
     <action>printf "$EDITOR" | sort -d > /var/memo/simple</action>
     <action>sort -m -u /var/memo/simple /var/memo/resto -o /var/memo/memo.txt</action>
     <action>refresh:EDITOR</action>
     </button>
     <button use-stock="true" label="gtk-quit">
     </button>
</hbox>

</vbox>
</window>'

gtkdialog -c --program=MEMO
I was able to get the script to localize properly including the bold text.
I used the variable 'HEADING'

HEADING="$(eval echo `gettext 'Welcome to PupMemo $VERSION'`)"

Then I displayed it as bold text with the line
<text use-markup="true"><label>"<b>'$HEADING'</b>"</label>/text>

Image

Image
Attachments
PupMemo-mod-1.0.pet
memo taking app to test eval_gettext
(12.97 KiB) Downloaded 324 times

User avatar
don570
Posts: 5528
Joined: Wed 10 Mar 2010, 19:58
Location: Ontario

Rules of eval_gettext

#14 Post by don570 »

Here are the rules of eval_gettext as I currently understand them.
Note that eval_gettext is used when the programmer wants translations
of a text string combined with a variable's contents at the same time.

Rules of eval_gettext

1) Avoid using the line

. gettext.sh

in most situations. However it must be used if a gtkdialog window is to
be formed in the following manner...

By trial and error I came up with this rule.....

. gettext is needed by eval_gettext for the window heading and
the top frame text.

It wasn't needed for the bold text and the button launching Xdialog



2) The basic rule is one backslash in front of a weak quote and
three backslashes in front of dollar sign ($)

Code: Select all

`eval_gettext \"PupMemo \\\$VERSION\"; echo`
3) When using Markup text , define a variable at beginning of script and
then use strong quotes around variable name in the text widget.

Code: Select all

<text use-markup="true"><label>"<b>'$HEADING'</b>"</label></text>
4) An alternative to eval_gettext is eval echo. Here is the example in pupmemo.
This has the advantage of displaying many characters
correctly however NOT the apostrophe character '.
It's especially useful with bold or colored text. Color text examples

Code: Select all

HEADING="$(eval echo `gettext 'Welcome to PupMemo $VERSION'`)" 
____________________________________________

User avatar
don570
Posts: 5528
Joined: Wed 10 Mar 2010, 19:58
Location: Ontario

#15 Post by don570 »

I've discovered that when eval_gettext is used the translator must be
careful when using an apostrophe.

Two forward slashes are needed in front of apostrophe

Here is an example that I found in my script
create-m3u

The French language often uses apostrophe...

Image

User avatar
ASRI éducation
Posts: 3197
Joined: Sat 09 May 2009, 12:10
Location: France
Contact:

#16 Post by ASRI éducation »

don570 wrote:I've discovered that when eval_gettext is used the translator must be
careful when using an apostrophe.

Two forward slashes are needed in front of apostrophe
I often use eval_gettext and I did not need to add slashes in front of apostrophe.

EDIT
An example:

Code: Select all

#: /usr/local/ppi/ppi-bin:897
#, sh-format
msgid ""
"Free space in download folder ($DOWNLOAD_DIRECTORY = $FREESPACE MB) is smaller than size required to download $GETFILES ($PKGSIZE MB), so your request is not possible.\n"
"Note: size required to download $GETFILES ($PKGSIZE MB) = $PETSIZE (size of file requested) + $SECURITYMARGIN (security margin)"
msgstr ""
"L'espace libre dans le dossier de téléchargement ($DOWNLOAD_DIRECTORY = $FREESPACE Mo) est plus petit que l'espace nécessaire pour le téléchargement de $GETFILES ($PKGSIZE Mo), par conséquent votre demande n'est pas acceptée.\n"
"Remarque : la taille nécessaire pour télécharger $GETFILES ($PKGSIZE Mo) = $PETSIZE (taille du fichier demandé) + $SECURITYMARGIN (marge de sécurité)"
I will look for script create-m3u.
Regards
Last edited by ASRI éducation on Mon 26 Jan 2015, 20:50, edited 1 time in total.
Projet ASRI éducation => [url=http://asri-education.org/]Association[/url] | [url=http://forum.asri-education.org/]Forum[/url] | [url=http://dl01.asri-education.org/]Dépôt[/url] | [url=http://kids.asri-education.org/]Espace kids[/url]

User avatar
don570
Posts: 5528
Joined: Wed 10 Mar 2010, 19:58
Location: Ontario

#17 Post by don570 »

Try looking at my script create-m3u
I couldn't get the French text in one line containing eval_gettext to show
unless I used the double slash.

I did it both ways ---> eval_gettext and eval gettext

I'd appreciate you testing and commenting.

_____________________________________________

User avatar
ASRI éducation
Posts: 3197
Joined: Sat 09 May 2009, 12:10
Location: France
Contact:

#18 Post by ASRI éducation »

don570 wrote:Image
I just tested.
It need to modify /usr/bin/create-m3u

First: replace

Code: Select all

export TEXTDOMAIN=create-m3u
export OUTPUT_CHARSET=UTF-8
by

Code: Select all

export TEXTDOMAIN=create-m3u
export TEXTDOMAINDIR=/usr/share/locale
export OUTPUT_CHARSET=UTF-8
. gettext.sh
Then: replace

Code: Select all

Xdialog --title "$(gettext 'Warning message')" --rc-file=/root/.config/pclock/orangebox.rc  --wrap --screen-center --left --no-buttons --msgbox  "$(eval echo `gettext 'No files with $EXTENSION extension were found.'`)"  0 0 100  &
by

Code: Select all

Xdialog --title "$(gettext 'Warning message')" --rc-file=/root/.config/pclock/orangebox.rc  --wrap --screen-center --left --no-buttons --msgbox  "$(eval_gettext 'No files with $EXTENSION extension were found.')"  0 0 100  &
I'll send you modified files via personnal message.
Regards
Attachments
eval_gettext_create-m3u_02.png
(7.85 KiB) Downloaded 90 times
eval_gettext_create-m3u_01.png
(7.45 KiB) Downloaded 93 times
Projet ASRI éducation => [url=http://asri-education.org/]Association[/url] | [url=http://forum.asri-education.org/]Forum[/url] | [url=http://dl01.asri-education.org/]Dépôt[/url] | [url=http://kids.asri-education.org/]Espace kids[/url]

User avatar
don570
Posts: 5528
Joined: Wed 10 Mar 2010, 19:58
Location: Ontario

#19 Post by don570 »

Drat! I found that using '. gettext.sh' would restrict my using curly brackets

${ var } for variable content . That was why I was avoiding it's use.

_____________________________________________________

Got your email. I'm going home now. Bye.

_____________________________________

Post Reply