#!/usr/bin/perl # unix-build_pot.pl program - Build All Language Files for YACS : PHP/MySQL Weblog CMS # Copyright (C) 2007 Nuxwin Team # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # @author : Laurent DECLERCQ use strict; use warnings; use Getopt::Long; $main::el_sep = "\t#\t"; @main::el = (); my $VERSION = "1.00"; ########### Options Begin ############# my $OPTIONS = <<"_OPTIONS"; $0 Ver $VERSION This script generates all language files for YACS. See http://www.yetanothercommunitysystem.com for more information about YACS Project. Usage: $0 [option] [option] [option] example : $0 --buildall=all --locale=fr -?, -h, --help display this help screen and exit -a, --availablesmod Display an list of modules availables -b=#, --buildall build all language files (*.pot, *.po, *.mo) for one or more specified modules. ex: --buildall=action for one module or --buildall=root-action for more modules Note: also update *.po files if they exist -G=#, --Genpot=# Generates all *.pot files for one or more specified modules ex: --Genpot=action for one module or --Genpot=action-categories for more modules Note: with ( all ) argv, this option will generate *.pot files for all modules -g=#, --genpo=# Generates all *.po files for one or more specified modules ex: --genpo=action (for one module) or --genpo=action-categories for more modules Note: The *.pot files must exist. -u=#, --updatepo=# Update all *.po files for one or more specified modules ex : --updatepo=action for one module or --updatepo=action-categories for more modules Note: with ( all ) argv, this option will update *.po files for all modules Attention: The *.po files must exist. -c=#, --compile Compile *.mo file for one or more specified modules ex: --compile=root ( for one module ) or --compile=root-action ( for more modules ) Note: with ( all ) argv, this option will compile *.mo files for all modules Attention: The *.po files must exist. -l=#, --locale=# build language files for specified locale(s) ex : --locale=fr for one locale or --locale=fr-es for more locales -d, -v, --debug enable debug mode General Note : By default, (execution without options) the script will be executed with the following parameters: --buildall=all --locale=en For all options, if the --locale option is not set, the default language selected is English (en). ! ! ! Do not execute this script with mixed options : (availablesmod, buildall, genpot, genpo, updatepo, compile). ! ! ! _OPTIONS sub usage { die @_,$OPTIONS; } my %opt = ( locale => "en", # Default selected localization ); Getopt::Long::Configure(qw(no_ignore_case)); GetOptions( \%opt, "help|h|?", "availablesmod|a", "buildall|b=s", "Genpot|G=s", "genpo|g=s", "updatepo|u=s", "compile|c=s", "locale|l=s", "debug|verbose|d|v" ) or print "Type --help for more information\n"and exit 1 ; usage("") if ($opt{help}); # Check unauthorized mixed options sub CheckMixedOpts { my $val; my @report; push_el(\@main::el, 'CheckMixedOpts()', 'Starting...'); my @UnauthMixedOpts = qw/availablesmod buildall Genpot genpo updatepo compile/; my @opt = keys %opt; foreach (@opt) { $val = $_; foreach (@UnauthMixedOpts) { push @report, $val if ($val eq $_); } } @report = reverse(@report); print "Unauthorized mixed options : @report\nType --help for more informations\n" and exit 1 if (@report > 1); push_el(\@main::el, "CheckMixedOpts()", 'Ending...'); } # Check syntaxe options sub CheckSyntOpts { push_el(\@main::el, 'CheckSyntOpts()', 'Starting...'); if ( defined(@ARGV) ) { foreach (@ARGV) { print "Syntaxe error: $_\nType --help for more informations\n" and exit 1 if (!(/^(-|--)/)); } } push_el(\@main::el, "CheckSyntOpts()", 'Ending...'); } CheckMixedOpts(); CheckSyntOpts(); ########### Options End ############# ########### Define YACS availables modules Begin ####### # NOTE TO DEVELOPPERS : # If you add or delete a module in the project YACS, # you must just edit the array below. # This array contain an list of all modules parsed by the default selected # action if the program is run without option or if option has been passed with (all) argv. # This table is also used to list the modules available with the option ( -- availablesmod ) our $MODULES = ["root", "actions", "agents", "articles", "behaviors", "categories", "codes", "collections", "comments", "control", "dates", "decisions", "feeds", "files", "i18n", "images", "letters", "links", "locations", "overlays", "scripts", "sections", "servers", "services", "shared", "skins", "smileys", "tools", "tables", "users", "versions"]; ########### Define YACS Modules List End ######### # Display an list of modules availables sub DisplayAvailablesMod { push_el(\@main::el, 'DisplayAvailablesMod()', 'Starting...'); print "\n"; print "The modules YACS availables are: \n"; print "\n"; print "@$MODULES"; print "\n\n"; push_el(\@main::el, 'DisplayAvailablesMod()', 'Ending...'); exit 0; } DisplayAvailablesMod() if ($opt{availablesmod}); ########### Define parameters Begin ############## # Check and Set Locales our $LOCALES = [ ]; sub SetLocales { push_el(\@main::el, 'SetLocales()', 'Starting...'); if($opt{locale} =~ /^[a-z]{2}$/) { # According RFC3066 ISO 639 - One locale @$LOCALES = $opt{locale}; } elsif ($opt{locale} =~ /^[a-z]{2}(-[a-z]{2})+$/) { # According RFC3066 ISO 639 - More locales @$LOCALES = split /-/, $opt{locale}; } else { #usage("Invalid --locale option"); print "Invalid argv ($opt{locale}) for --locale option\nType --help for more informations\n"; exit 1; } push_el(\@main::el, 'SetLocales()', 'Ending...'); } # Check and set module for selected option sub CheckSetMod { foreach (qw/buildall Genpot genpo updatepo compile/) { if ( defined($opt{$_}) && ($opt{$_} ne 'all') ) { @$MODULES = (); if ($opt{$_} =~ /^[a-z0-9]+$/) { @$MODULES = $opt{$_}; } elsif ($opt{$_} =~ /^[a-z0-9]+(-?[a-z0-9]+)+$/) { @$MODULES = split /-/, $opt{$_}; } else { print "Invalid argv (@$MODULES) for $_ option \n"; print "Type --help for more informations about valide argv for this option\n"; } } } } SetLocales(); CheckSetMod(); ########### Define parameters End ############## ############## Debug and System cmd Subroutines Begin ############## sub push_el { my ($el, $sub_name, $msg) = @_; push @$el, "$sub_name".$main::el_sep."$msg"; print STDOUT "DEBUG: push_el() sub_name: $sub_name, msg: $msg\n" if ($opt{debug}); } sub sys_command { my ($cmd) = @_; push_el(\@main::el, 'sys_command()', 'Starting...'); my $result = system($cmd); my $exit_value = $? >> 8; my $signal_num = $? & 127; my $dumped_core = $? & 128; if ($exit_value == 0) { push_el(\@main::el, "sys_command('$cmd')", 'Ending...'); return 0; } else { push_el(\@main::el, 'sys_command()', "ERROR: External command '$cmd' returned '$exit_value' status !"); return -1; } } ############## Debug and System cmd Subroutines End ############## ############## Process Subroutines Begin ############## our ($module, $locale); sub Process { my ($rs, $index); my $b = 0; push_el(\@main::el, "Process()", 'Starting...'); my $count = @$LOCALES; my $G = @$MODULES; my $i = 0; for ($index = 0; $index < $count ; $index++) { $locale = shift(@$LOCALES); foreach (@$MODULES) { $module = $_; if ( defined($opt{Genpot}) && $i < $G ) { mkdir ("i18n/templates") if (! -e 'i18n/templates'); $rs = GenPot(); return $rs if ($rs != 0); $i++; $b++; } if ( defined($opt{genpo}) ) { mkdir ("i18n/locale/$locale") if (! -e "i18n/locale/$locale"); if (! -e "i18n/locale/$locale/$module.po") { $rs = GenPo(); return $rs if ($rs != 0); $b++; } else { $rs = UpdatePo(); return $rs if ($rs != 0); $b++; } } if ( defined($opt{updatepo}) ) { $rs = UpdatePo(); return $rs if ($rs != 0); $b++; } if ( defined($opt{compile}) ) { $rs = CompileMo(); return $rs if($rs != 0); $b++; } if ($b == 0) { if ($i < $G) { mkdir ("i18n/templates") if (! -e 'i18n/templates'); $rs = GenPot(); return $rs if ($rs != 0); $i++; } mkdir ("i18n/locale/$locale") if (! -e "i18n/locale/$locale"); if (! -e "i18n/locale/$locale/$module.po") { $rs = GenPo(); return $rs if ($rs != 0); } else { $rs = UpdatePo(); return $rs if ($rs != 0); } $rs = CompileMo(); return $rs if($rs != 0); } } } push_el(\@main::el, "Process()", 'Ending...'); return 0; } sub GenPot { my $rs; push_el(\@main::el, 'GenPot()', 'Starting...'); $rs = xgettext(); return $rs if($rs != 0); push_el(\@main::el, "GenPot()", 'Ending...'); return 0; } sub GenPo { my $rs; push_el(\@main::el, 'GenPo()', 'Starting...'); $rs = msginit(); return $rs if($rs != 0); push_el(\@main::el, "GenPo()", 'Ending...'); return 0; } sub UpdatePo { my $rs; push_el(\@main::el, 'GenPo()', 'Starting...'); $rs = msgmerge(); return $rs if($rs != 0); push_el(\@main::el, "GenPo()", 'Ending...'); return 0; } sub CompileMo { my $rs; push_el(\@main::el, 'CompileMo()', 'Starting...'); $rs = msgfmt(); return $rs if($rs != 0); push_el(\@main::el, "CompileMo()", 'Ending...'); return 0; } ############## Process Subroutines End ############## ############### Gettext Subroutines Begin ########## sub xgettext { my $rs; push_el(\@main::el, 'xgettext()', 'Starting...'); if($module eq 'root') { $rs = sys_command("xgettext *.php --output=i18n/templates/$module.pot --default-domain=$module --keyword=c --keyword=nc:1,2 --keyword=s --keyword=ns:1,2 --language=php"); return $rs if($rs != 0); } else { $rs = sys_command("xgettext $module/*.php --output=i18n/templates/$module.pot --default-domain=$module --keyword=c --keyword=nc:1,2 --keyword=s --keyword=ns:1,2 --language=php"); return $rs if($rs != 0); } push_el(\@main::el, "xgettext()", 'Ending...'); return 0; } sub msginit { my $rs; push_el(\@main::el, 'msginit()', 'Starting...'); $rs = sys_command("msginit --input=i18n/templates/$module.pot --output=i18n/locale/$locale/$module.po --locale=$locale --no-translator"); return $rs if($rs != 0); push_el(\@main::el, "msginit()", 'Ending...'); return 0; } sub msgmerge { my $rs; push_el(\@main::el, 'msgmerge()', 'Starting...'); $rs = sys_command("msgmerge i18n/locale/$locale/$module.po i18n/templates/$module.pot --update --backup=none"); return $rs if($rs != 0); push_el(\@main::el, "msgmerge()", 'Ending...'); return 0; } sub msgfmt { my $rs; push_el(\@main::el, 'msgfmt()', 'Starting...'); $rs = sys_command("msgfmt i18n/locale/$locale/$module.po --output=i18n/locale/$locale/$module.mo --statistics"); return $rs if($rs != 0); push_el(\@main::el, "msgfmt()", 'Ending...'); return 0; } ############### Gettext Subroutines End ########## # ## MAIN # chdir ".." or die "Unable to go in root yacs directory :$!"; my $rs = undef; $rs = Process(); if ($rs != 0) { exit 1; } exit 0;