create-pot-files-extensions.sh 3.8 KB
Newer Older
1 2 3 4 5 6 7
#!/bin/bash

function usage() {
  cat <<EOT
create-pot-files-extensions.sh - builds .pot files for CiviCRM extensions.

Usage:
8
  ./bin/create-pot-files-extensions.sh [short name] [src dir] [dest dir]
9 10

Example:
11
  ./bin/create-pot-files-extensions.sh
12 13
  ./bin/create-pot-files-extensions.sh i18example ~/repository/ca.bidon.i18nexample/  ~/repository/ca.bidon.i18nexample/po/pots/

14 15 16 17 18 19 20 21
In the recommended use-case, you should run this command when your working
directory is the base directory of your extension, e.g. where the info.xml
is located.

The .pot file will be placed in l10n/pot/{extname}.pot.
Translations should be located in, for example, l10n/fr_FR/LC_MESSAGES/{extname}.po
and be compiled as l10n/fr_FR/LC_MESSAGES/{extname}.mo

22 23 24 25 26 27 28 29 30 31 32
NB: this program uses the "sponge" command, which you can get by installing the
"moreutils" package under Debian/Ubuntu.

EOT

  exit 1;
}

[ "$1" == "--help" ] && usage
[ "$1" == "-h" ] && usage

33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
extname="$1"
root="$2"
potdir="$3"

# In the typical use-case, we are invoked from the working directory of the extension
# and we can use the info.xml to get the extension name
if [ "$1" == "" ]; then
  test ! -f "info.xml" && echo "Error: Could not find info.xml in the current directory `pwd`" && usage

  # Extract the tag "<file>i18nexample</file>" from the info.xml
  # We first extract the line with the "file" tag, then extract the 'file' value.
  # the regexp is a bit more generous than necessary, in case everything is on one line.
  extname=`grep '<file>' info.xml | sed 's|\s*<file>\(.*\)</file>\s*|\1|'`

  test "$extname" == "" && echo 'Error: could not find the file tag from info.xml' && usage

  root="."
50 51 52

  # The automatic string extractor passes an environment variable to set the potdir.
  if [ -d "$POTDIR" ]; then
53
    potdir="$POTDIR/$extname/pot"
54 55 56
  else
    potdir="l10n/pot/"
  fi
57 58 59 60 61 62 63 64

  # At this point we can assume that we are in the right place, and can create
  # the pot target directory if necessary.
  if [ ! -d "$potdir" ]; then
    echo " * Creating target pot directory: $potdir"
    mkdir -p "$potdir"
  fi
fi
65

66 67 68
# Ensure that the source and target directory exist
test ! -e "$root" && echo "Error: $root source does not exist"  && usage
test ! -d "$root" && echo "Error: $root source not a directory" && usage
69

70 71
test ! -e "$potdir" && echo "Error: $potdir target does not exist (please create it if necessary)" && usage
test ! -d "$potdir" && echo "Error: $portidr target not a directory" && usage
72 73 74 75

# Test whether the sponge command is installed
which sponge > /dev/null
if [ $? -ne 0 ]; then
76
  echo "Could not find the sponge command. sudo apt-get install moreutils ?"
77 78 79 80 81 82
  usage
fi

header=`tempfile`
now=`date +'%F %R%z'`

83 84 85 86 87
# Replace the generation timestamp in the header
# and set the Project-Id-Version to that of the extension
sed -e "s/NOW/$now/" \
    -e "s/Project-Id-Version: CiviCRM/Project-Id-Version: $extname/" \
    `dirname $0`/header > $header
88

89 90
# create base POT file
echo " * Building ${extname}.pot"
91
cp $header $potdir/${extname}.pot
92
civistrings -ao "$potdir/${extname}.pot" "$root"
93 94 95
msguniq $potdir/${extname}.pot | sponge $potdir/${extname}.pot

rm $header
96 97 98

echo " * All done"

99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
if [ "$CIVI_KEEP_IT_QUIET" = "" ]; then
  echo ""
  echo "Generated file: $potdir/${extname}.pot"
  echo "Feel free to commit it to your git repository if your extension is not"
  echo "managed by the CiviCRM automatic extension distribution system."
  echo ""
  echo "To be included in the automatic distribution, post a request for review"
  echo "on the internationalisation forum:"
  echo "http://forum.civicrm.org/index.php/board,10.0.html"
  echo ""
  echo "For more information:"
  echo "http://wiki.civicrm.org/confluence/display/CRMDOC/Extension+translation"
  echo "http://wiki.civicrm.org/confluence/display/CRMDOC/Extension+Reference"
  echo "http://wiki.civicrm.org/confluence/display/CRMDOC/Create+an+Extension"
fi