Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Fri 19 Jul 2019, 23:15
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
eval_gettext example
Post new topic   Reply to topic View previous topic :: View next topic
Page 1 of 2 [19 Posts]   Goto page: 1, 2 Next
Author Message
don570


Joined: 10 Mar 2010
Posts: 5343
Location: Ontario

PostPosted: Tue 04 Nov 2014, 20:16    Post subject:  eval_gettext example  


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:

#!/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:
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


Last edited by don570 on Mon 08 Dec 2014, 15:38; edited 7 times in total
Back to top
View user's profile Send private message 
vicmz


Joined: 15 Jan 2012
Posts: 1268

PostPosted: Wed 05 Nov 2014, 09:41    Post subject:  

I didn't understand it until I read your tutorial. Thank you, don570.
_________________
Puppy Linux en español
Back to top
View user's profile Send private message 
ASRI éducation


Joined: 09 May 2009
Posts: 3203
Location: France

PostPosted: Wed 05 Nov 2014, 14:21    Post subject:  

It is a good idea to give advice about "eval_gettext."
Users/contributors will appreciate.
Regards

_________________
Projet ASRI éducation => Association | Forum | Dépôt | Espace kids
Back to top
View user's profile Send private message Visit poster's website 
L18L

Joined: 19 Jun 2010
Posts: 3447
Location: www.eussenheim.de/

PostPosted: Wed 05 Nov 2014, 15:53    Post subject: eval_gettext example
Subject description: eval echo gettext
 

I have found that eval_gettext if really not needed.

Instead one can use
Code:
eval echo gettext "text"


Tried your exemple (just Recorder3):
Code:
#!/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
Back to top
View user's profile Send private message 
don570


Joined: 10 Mar 2010
Posts: 5343
Location: Ontario

PostPosted: Sat 08 Nov 2014, 17:37    Post subject:  

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. Laughing

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
_______________________________________________
Back to top
View user's profile Send private message 
L18L

Joined: 19 Jun 2010
Posts: 3447
Location: www.eussenheim.de/

PostPosted: Sun 09 Nov 2014, 04:31    Post subject:  

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

Sorry Embarassed
You are right. Cool
Thanks for feed back Very Happy

back ticks missing Exclamation
eval_echo_gettext_without_.gettext.sh .png
 Description   
 Filesize   26 KB
 Viewed   472 Time(s)

eval_echo_gettext_without_.gettext.sh .png

Back to top
View user's profile Send private message 
L18L

Joined: 19 Jun 2010
Posts: 3447
Location: www.eussenheim.de/

PostPosted: Sun 09 Nov 2014, 05:10    Post subject: Re: eval_gettext example
Subject description: eval echo gettext
 

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.
eval_echo_gettext_without_.gettext.sh2.png
 Description   without .gettext.sh again
 Filesize   17.01 KB
 Viewed   444 Time(s)

eval_echo_gettext_without_.gettext.sh2.png

Back to top
View user's profile Send private message 
don570


Joined: 10 Mar 2010
Posts: 5343
Location: Ontario

PostPosted: Thu 20 Nov 2014, 18:55    Post subject:  

Quote:
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, 14:57; edited 1 time in total
Back to top
View user's profile Send private message 
L18L

Joined: 19 Jun 2010
Posts: 3447
Location: www.eussenheim.de/

PostPosted: Fri 21 Nov 2014, 14:58    Post subject:  

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
Forget_eval_gettext.png
 Description   
 Filesize   4.44 KB
 Viewed   395 Time(s)

Forget_eval_gettext.png

Back to top
View user's profile Send private message 
don570


Joined: 10 Mar 2010
Posts: 5343
Location: Ontario

PostPosted: Wed 26 Nov 2014, 20:10    Post subject:  

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

I checked and the line
Code:
. 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:
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:
echo 'Harry's Button'


and the output will be wrong.


Code:
echo "Harry's Button"


Now the output will be right.


______________________________________________

Last edited by don570 on Wed 03 Dec 2014, 19:24; edited 1 time in total
Back to top
View user's profile Send private message 
L18L

Joined: 19 Jun 2010
Posts: 3447
Location: www.eussenheim.de/

PostPosted: Sun 30 Nov 2014, 09:04    Post subject:  

don570 wrote:
... and L18L has simpler method.

simpler does not mean better!

"My" method is just borrowed from shinobar's
Code:
eval_gettext () {

  local myMESSAGE=$(gettext "$1")

  eval echo \"$myMESSAGE\"

}


But look inside /usr/bin/gettext.sh:
Code:
# 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 Crying or Very sad

See also all comments on Simplifying translations please.

Thus stay safely with eval_gettext Wink
Back to top
View user's profile Send private message 
don570


Joined: 10 Mar 2010
Posts: 5343
Location: Ontario

PostPosted: Wed 03 Dec 2014, 19:28    Post subject:  

Good page Simplifying Translations

I had missed that somehow.
_____________________________________________________
Back to top
View user's profile Send private message 
don570


Joined: 10 Mar 2010
Posts: 5343
Location: Ontario

PostPosted: Wed 03 Dec 2014, 19:36    Post subject:  

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

I noticed that the line
Quote:
. gettext.sh

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

Here's the code

Code:

#! /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>



PupMemo-mod-1.0.pet
Description  memo taking app to test eval_gettext
pet

 Download 
Filename  PupMemo-mod-1.0.pet 
Filesize  12.97 KB 
Downloaded  254 Time(s) 
Back to top
View user's profile Send private message 
don570


Joined: 10 Mar 2010
Posts: 5343
Location: Ontario

PostPosted: Mon 08 Dec 2014, 15:11    Post subject: Rules of eval_gettext  

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:
`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:
<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:
HEADING="$(eval echo `gettext 'Welcome to PupMemo $VERSION'`)"


____________________________________________
Back to top
View user's profile Send private message 
don570


Joined: 10 Mar 2010
Posts: 5343
Location: Ontario

PostPosted: Mon 26 Jan 2015, 16:05    Post subject:  

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...

Back to top
View user's profile Send private message 
Display posts from previous:   Sort by:   
Page 1 of 2 [19 Posts]   Goto page: 1, 2 Next
Post new topic   Reply to topic View previous topic :: View next topic
 Forum index » Off-Topic Area » Programming
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.0949s ][ Queries: 12 (0.0152s) ][ GZIP on ]