Created
December 2, 2009 15:19
-
-
Save zerodogg/247272 to your computer and use it in GitHub Desktop.
Script to fetch and install translations from transifex
This file contains 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
#!/usr/bin/perl | |
# fetchTranslations | |
# Hack to retrieve and install translations from transifex | |
# Copyright (C) Eskild Hustvedt 2009 | |
# | |
# 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 3 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, see <http://www.gnu.org/licenses/>. | |
use strict; | |
use warnings; | |
use File::Copy qw(move); | |
use Fatal qw(chdir); | |
my $collection = 'xfce/xfce-4-6'; | |
my $domain = 'https://translations.xfce.org'; | |
my $baseUrl = '/languages/[LANG]/collection/'; | |
my $localeTarget = '/usr/share/locale/[LANG]/LC_MESSAGES/'; | |
my $language = shift; | |
$| = 1; | |
# Language is required | |
if(not $language) | |
{ | |
usage(); | |
exit(1); | |
} | |
# Fetch collection if supplied | |
$collection = shift(@ARGV) if @ARGV; | |
# Fetch localeTarget if supplied | |
$localeTarget = shift(@ARGV) if @ARGV; | |
fetchAndBuild(); | |
# Purpose: Fetch tarball and build mo files, then install them | |
# Usage: fetchAndBuild(); | |
sub fetchAndBuild | |
{ | |
$localeTarget =~ s/\[LANG\]/$language/g; | |
if(not -w $localeTarget) | |
{ | |
die("I don't have write permission to $localeTarget. Needed to install mo-files.\nMaybe you want: sudo $0 $language\n"); | |
} | |
mkdir('fetchTranslationsData') if not -d 'fetchTranslationsData'; | |
chdir('fetchTranslationsData'); | |
mkdir('lang-'.$language) if not -d 'lang-'.$language; | |
chdir('lang-'.$language); | |
print 'Fetching all po files for language "'.$language.'" in collection "'.$collection.'"'."..."; | |
downloadPoTarball(); | |
if(not -e 'pofiles.tar') | |
{ | |
die("failed\n"); | |
} | |
print "done\n"; | |
print 'Extracting tarball...'; | |
system('tar','-xf','pofiles.tar'); | |
unlink('pofiles.tar'); | |
print "done\n"; | |
print "Processing:\n"; | |
foreach my $poFile (glob('*/*/*/*.po')) | |
{ | |
if(not -e $poFile) | |
{ | |
die("something went wrong. No translations available?\n"); | |
} | |
printf(" %-40s: ",$poFile); | |
print 'building...'; | |
system('msgfmt',$poFile); | |
print 'done '; | |
print 'installing...'; | |
my $moName = getMoNameFromPoPath($poFile); | |
my $error; | |
move('messages.mo',$localeTarget.'/'.$moName) or $error = $!; | |
if ($error) | |
{ | |
unlink('messages.mo'); | |
print "failed: $error\n"; | |
} | |
else | |
{ | |
print "done\n"; | |
} | |
} | |
if ($ENV{SUDO_GID} && $ENV{SUDO_UID}) | |
{ | |
chdir('../../'); | |
if (-e 'fetchTranslationsData') | |
{ | |
print "Running under sudo, chowning downloaded files to original user..."; | |
system('chown','-R',$ENV{SUDO_UID}.':'.$ENV{SUDO_GID},'fetchTranslationsData'); | |
print "done\n"; | |
} | |
} | |
print "All done :)\n"; | |
} | |
# Purpose: Download the po tarball | |
# Usage: downloadPoTarball(); | |
sub downloadPoTarball | |
{ | |
my $url = $domain.$baseUrl; | |
$url =~ s/\[LANG\]/$language/g; | |
$url .= $collection.'/download_targz/'; | |
downloadToFile($url,'pofiles.tar'); | |
} | |
# Purpose: Download a URL to a file | |
# Usage: downloadToFile(URL,TARGET); | |
sub downloadToFile | |
{ | |
my $url = shift; | |
my $target = shift; | |
return system('curl','-s','-k','-o',$target,$url); | |
} | |
# Purpose: Convert a file path to a mo-name | |
# Usage: getMoNameFromPoPath(path); | |
sub getMoNameFromPoPath | |
{ | |
my $path = shift; | |
$path =~ s{/+}{/}g; | |
my @names = split(m{/},$path); | |
my $name = shift(@names); | |
my %convert = ( | |
'thunar' => 'Thunar', | |
'terminal' => 'Terminal', | |
); | |
if ($convert{$name}) | |
{ | |
$name = $convert{$name}; | |
} | |
return $name.'.mo'; | |
} | |
# Purpose: Output usage information | |
# Usage: usage(); | |
sub usage | |
{ | |
print "\n"; | |
print "Usage: $0 [language]\n"; | |
print " Ex: $0 nn\n"; | |
print " OR\n"; | |
print " $0 [language] [collection]\n"; | |
print " Ex: $0 nn xfce/xfce-master\n"; | |
print " OR\n"; | |
print " $0 [language] [collection] [locale directory]\n"; | |
print " Ex: $0 nn xfce/xfce-master ~/.local/share/locale/nn/LC_MESSAGES/\n\n"; | |
print "[collection] defaults to $collection if not supplied\n"; | |
print "[locale directory] defaults to $localeTarget if not supplied\n"; | |
print "\nWill create ./fetchTranslationsData to store the po-files.\nIt is safe to remove this directory afterwards if you want to.\n\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment