LINUX.IE, website of the Irish Linux Users' Group
Tux rules!

   
Home
New Users
Articles
Download
Projects
Community
Vendors

  Print Version
 
Archives:


planetILUG

Recent News

News Archive


Join the
ILUG
on FaceBook


Join the
ILUG
on LinkedIn


Join the
ILUG SETI
Group



















 
 :: Mailing Lists

[ILUG] [TOTD] querying the locale database

[ILUG] [TOTD] querying the locale database

Conor Daly conor.daly at oceanfree.net
Fri Oct 1 01:26:01 IST 2004


On Thu, Sep 30, 2004 at 12:50:59PM +0100 or so it is rumoured hereabouts, 
P at draigBrady.com thought:
> There is a very useful database of "country" specific
> information, located on all linux systems.
> 
> I have found it difficult to interact with this
> locale database though. In the above example
> how does one know how to specify the record (fr_FR)
> or to select the field (int_prefix). so...

Thanks Padraig!

I've scripted that up just for fun.  Here it is.  Call it what you wish,
I've called it 'loc'

Conor

################ start here ###################
#!/bin/bash

#################################################################
#
# Name		loc
# Author	Conor Daly
# Date		01-Oct-2004
# License	GPL
#
#################################################################

useage() {
cat << USE

Useage:	$0 <country> [<field>] | <country_code> [<field>] | country 
	$0 -? [<lc_type>] | -h

	Display locale information for the designated country.

	In the first format, '`basename $0`' displays information from the 
	locale database for the specified country, country_code or lists
	the countries in the database.  All arguments are used as regexps.
	The first argument to the command is interpreted as follows:

	The word "country" causes the listing of countries. 
	An underscore '_' in the word is interpreted as a country_code
	Otherwise it is interpreted as a country name.

	In the second format, the available lc_types are displayed or, if
	a particular lc_type is specified, the fields for that lc_type are
	displayed.

Options:
	-?	Display the possible lc_types or fields

	-h	Display this help message


USE
}

fields() { # Display lc_types or fields
  for LOC in `locale | cut -d= -f1`; do
    if [ $TWO ]; then # Fields
      echo $LOC | grep -i $TWO >/dev/null
      if [ $? -eq 0 ]; then
        locale -kc $LOC | cut -d= -f1
      fi
    else # lc_types
      echo $LOC
    fi
  done
}

country() { # Query database
  local GOTONE=0
  local COUNTRYASKED=$1

# check for a country_code or name
  echo $ONE | grep _ >/dev/null
  if [ $? -eq 0 ]; then # country_code
    for lang in `locale -a | grep _ | grep -Ev [.@] | grep -i $ONE`; do
# for each matching country_code
      if [ $2 ]; then
# display fields
        TWO=
        echo
        echo -n `LANG=$lang locale territory`": $lang: "
        LANG=$lang locale title
        for LC in `fields`; do
          TWO=$LC
          for FIELD in `fields | grep $2`; do
            echo -n "$lang $FIELD: "; LANG=$lang locale $FIELD
          done
        done
      else
# Just say which country this code applies to
        echo -n "$lang "`LANG=$lang locale territory`": "
        LANG=$lang locale title
      fi
    done
    GOTONE=1
  else # country name
    for lang in `locale -a | grep _ | grep -Ev [.@]`; do
# for each matching country_code
      COUNTRY=`LANG=$lang locale territory`
      echo $COUNTRY | grep -i "$COUNTRYASKED" >/dev/null
      if [ $? -eq 0 ]; then
        if [ $2 ]; then
# display fields
          TWO=
          echo
          echo -n `LANG=$lang locale territory`": $lang: "
          LANG=$lang locale title
          for LC in `fields`; do
            TWO=$LC
            for FIELD in `fields | grep $2`; do
              echo -n "$lang $FIELD: "; LANG=$lang locale $FIELD
            done
          done
        else
# Just say which country this code applies to
          echo -n "$lang "`LANG=$lang locale territory`": "
          LANG=$lang locale title
        fi
      GOTONE=1
      fi
    done
  fi
  if [ $GOTONE -eq 0 ] && [ $# -eq 1 ]; then
# Neither country name or code, assume default country
  for LC in `fields`; do
# display fields
    TWO=$LC
    for FIELD in `fields | grep $ONE`; do
      echo -n `locale | grep LANG | cut -d= -f2 | cut -d. -f1`" $FIELD: "; locale $FIELD
    done
  done
  fi
}

countries() { # List available countries
  locale -a  | grep _ | grep -Ev '[.@]' | while read lang; do     LANG=$lang locale territory; done | sort | uniq | less -eF
}

if [ $# -lt 1 ] || [ "$1" == "-h" ]; then
  useage
  exit 1
fi

if [ $# -gt 0 ]; then
  ONE=$1
fi


if [ "$1" == "-?" ]; then
  if [ $# -gt 1 ]; then
    TWO=$2
  fi
  fields
  exit 0
fi

if [ "$1" == "country" ]; then
  countries
  exit 0
fi

if [ $# -lt 3 ]; then
  country $*
  exit 0
fi
################  end  here ###################

-- 
Conor Daly <conor.daly at oceanfree.net>

Domestic Sysadmin :-)
---------------------
Faenor.cod.ie
  1:31am  up 34 days,  5:24,  0 users,  load average: 0.02, 0.01, 0.00
Hobbiton.cod.ie
 01:23:12  up 13 days,  2:54,  1 user,  load average: 0.00, 0.00, 0.00



More information about the ILUG mailing list
Read this without the formatting.
                                                                                                    

 

Hosted by HEAnet


Maintained by the ILUG website team. The aim of Linux.ie is to support and help commercial and private users of Linux in Ireland. You can display ILUG news in your own webpages, read backend information to find out how. Networking services kindly provided by HEAnet, server kindly donated by Dell. Linux is a trademark of Linus Torvalds, used with permission. No penguins were harmed in the production or maintenance of this highly praised website. Looking for the Indian Linux Users' Group? Try here. If you've read all this and aren't a lawyer: you should be!
RSS Version
Powered by Dell