Page 1 of 1

How to get the right current locale decimal mark?

Posted: Wed 07 Dec 2011, 11:07
by Argolance
Hello,
I need to get the decimal mark currently used by the system for a script to run properly according to the locale settings. French, German, Spanish... are using comma, English, Japan... point. The problem is that, while setting my locale as French for instance, I get the wrong mark using these simple code lines to get it:
# a=$(echo "scale=2;100/3"|bc)
# echo $a
33.33
#
:shock:

I already tried others ways to solve the problem like getting the current locale and, while listing all locales supposed to use point, conditionally run the script with, and with comma if not listed... Of course, this doesn't match every time and I thought it should be easier to get the current mark directly.
How could I solve this? As far as I know, this problem has, at least, to be solved for all scripts using gtk hscale or vscale!

Best regards.

Re: How to get the right current decimal mark?

Posted: Wed 07 Dec 2011, 13:06
by L18L
# grep -A4 ^LC_NUMERIC /usr/share/i18n/locales/${LANG%.*}
LC_NUMERIC
decimal_point "<U002C>"
thousands_sep "<U002E>"
grouping 3;3
END LC_NUMERIC
#

2C is ,
2E is .
:)

Or just:
grep ^decimal_point /usr/share/i18n/locales/${LANG%.*}

Posted: Thu 08 Dec 2011, 04:08
by Karl Godt
I would probably simply use

Code: Select all

echo $LANG
LANG=C dc 1000 3 \/ p
echo $LANG
This should set the locale just for the line/command , not for the whole script .

Posted: Thu 08 Dec 2011, 10:22
by Argolance
Hello,
Thank you Karl Godt and L18L for replying.
@L18L
I think what you suggested is a good way to get the decimal mark and contribute to find a solution to a problem that concerns all scripts using non monetary decimal values (Please see=> JMW theme configuration)
A bit more complicated though, because all locales files don't give the decimal_point value directly but through another locale config file that is "copied" instead:
LC_NUMERIC
copy "de_BE"
[...]
... for instance.
I add code lines to this hscale script based on the original (from gtkdialog(4) svn repository by thunor):
#!/bin/sh

GTKDIALOG=gtkdialog
#get current Decimal Mark
DM_tmp=$(grep ^decimal_point /usr/share/i18n/locales/${LANG%.*} | cut -d'<' -f2 | sed 's/>"//')
if [[ -z $DM_tmp ]]; then
echo $(grep -A4 ^LC_NUMERIC /usr/share/i18n/locales/${LANG%.*}) > /tmp/LC_N_temp
locale=$(cat /tmp/LC_N_temp | cut -d '"' -f2)
DM_tmp=$(grep ^decimal_point /usr/share/i18n/locales/$locale | cut -d'<' -f2 | sed 's/>"//')
fi

if [[ $DM_tmp = "U002C" ]]; then
DM=","
else
DM="."
fi

export MAIN_DIALOG='
<window title="Decimal Mark Test" resizable="true">
<vbox>
<frame hscale widget>
<hscale space-expand="true" space-fill="true"
width-request="200" height-request="20"
value-pos="0"
range-min="0'$DM'3" range-max="30'$DM'7"
range-step="0'$DM'1" range-value="15'$DM'5">
<variable>HSCALE</variable>
</hscale>
</frame>
<hbox homogeneous="true">
<button ok></button>
</hbox>
</vbox>
<action signal="hide">exit:Exit</action>
</window>
'

$GTKDIALOG --program=MAIN_DIALOG
This should probably be polished up because (no use to say!) I am not an expert in code lines :oops: but it works fine (seems to work?) with any locale and this was the purpose! :D

Best regards.

Posted: Thu 08 Dec 2011, 15:20
by Argolance
Hello Karl Godt,
Karl Godt wrote:I would probably simply use

Code: Select all

echo $LANG
LANG=C dc 1000 3 \/ p
echo $LANG
This should set the locale just for the line/command , not for the whole script .
I was wondering how could this be done taking the script above as example?
Thank you a lot.

Regards

Posted: Tue 13 Dec 2011, 19:16
by L18L
Maybe this is faster without copy, I donĀ“t know

# pwd
/usr/lib/locale/de_DE
# cat LC_NUMERIC
"$(,0,.,.ISO-8859-1#

# pwd
/usr/lib/locale/en_US
# cat LC_NUMERIC
"$(,0.,.,ISO-8859-1#