Created
November 1, 2012 17:22
-
-
Save JustDevZero/3995159 to your computer and use it in GitHub Desktop.
code2pot allows you to extract yout gettext sensible strings into a pot file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# Copyright (c) 2012, Daniel Ripoll | |
# All rights reserved. | |
# | |
# Redistribution and use in source and binary forms, with or without | |
# modification, are permitted provided that the following conditions are | |
# met: | |
# | |
# * Redistributions of source code must retain the above copyright | |
# notice, this list of conditions and the following disclaimer. | |
# * Redistributions in binary form must reproduce the above | |
# copyright notice, this list of conditions and the following disclaimer | |
# in the documentation and/or other materials provided with the | |
# distribution. | |
# * Neither the name of the nor the names of its | |
# contributors may be used to endorse or promote products derived from | |
# this software without specific prior written permission. | |
# | |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
# | |
CODEVERSION="0.2" | |
if [[ ($# -eq 0) ]] | |
then | |
echo "code2pot $CODEVERSION" | |
echo "You have not selected any file." | |
echo "Type -h or --help for help." | |
exit 1 | |
fi | |
function print_verbose () { | |
# Detect if runing in verbose and print the message | |
if [[ $1 == "yes" ]]; then | |
echo $2 | |
fi | |
} | |
function extract_strings (){ | |
# Extract the translatable strings into a common .pot file | |
nameitem="$1" | |
currentname="$2" | |
extensionname="$3" | |
verbose="$4" | |
directory=`pwd` | |
if [[ -f "$directory/messages.pot" ]]; then | |
print_verbose $verbose "Using file messages.pot" | |
else | |
print_verbose $verbose "no existe" | |
echo ''>"$directory/messages.pot" | |
fi | |
if [[ $extensionname == "ui" ]]; then | |
print_verbose $verbose "A QT-UI file ($itemname) has been detected, converting to a temporal string-extractable file $currentname.h" | |
uic-qt4 -tr _ $currentname.ui -o $currentname.h | |
xgettext $currentname.h -j -o messages.pot --from-code=UTF-8 --keyword=_ | |
print_verbose $verbose "Extracting strings from temporal file to messages.pot" | |
rm $currentname.h | |
print_verbose $verbose "Deleting temporal file $currentname.h" | |
else | |
xgettext $nameitem -j -o messages.pot --from-code=UTF-8 --keyword=_ | |
print_verbose $verbose "Extracting strings from $nameitem to messages.pot" | |
fi | |
} | |
function checkextract () { | |
# Check if you have typed an extractable file | |
if [[ -f $1 ]]; then | |
extract_strings "$itemname" "$filename" "$extension" "$2" | |
else | |
echo "$item is not a valid file" | |
fi | |
} | |
function print_version() { | |
# Print version of the program | |
echo "code2pot $CODEVERSION" | |
echo 'Created for BulmaGés project.' | |
echo 'Checkout http://bulmages.es or http://danielripoll.es for more info.' | |
exit 0 | |
} | |
function print_license() { | |
# Print license | |
echo "code2pot $CODEVERSION" | |
echo 'Created for BulmaGés project.' | |
echo 'This script is licensed under New BSD License.' | |
exit 0 | |
} | |
function print_help() { | |
# Print help | |
echo "code2pot $CODEVERSION" | |
echo '-l/--license for see the license of the program' | |
echo '-v/--version for see the version of the program' | |
echo '-vb/--show in verbose mode' | |
echo '-h/--help show this help' | |
exit 0 | |
} | |
# Read the options | |
toread=$1 | |
case $toread in | |
"-v") | |
print_version;; | |
"--version") | |
print_version;; | |
"-l") | |
print_license;; | |
"--license") | |
print_license;; | |
"-h") | |
print_help;; | |
"--help") | |
print_help;; | |
"-vb") | |
echo 'Verbose mode activated' | |
isverbose="yes";; | |
"--verbose") | |
echo 'Verbose mode activated' | |
isverbose="yes";; | |
*) | |
isverbose="no" | |
esac | |
for item in $*;do | |
# Extract every item in the argument line | |
itemname=$item | |
extension="${itemname##*.}" | |
filename="${itemname%.*}" | |
if [[ ($item == "-vb") || ($item == "--verbose" ) ]]; then | |
echo "code2pot $CODEVERSION" | |
else | |
checkextract "$item" "$isverbose" | |
fi | |
done | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment