From 96a2bb583e614cdd721909ebff21f3c1522e1024 Mon Sep 17 00:00:00 2001 From: Struan Donald Date: Wed, 19 Mar 2014 10:06:06 +0000 Subject: enable generation of cobrand specific po files This is to stop the main FixMyStreet .po file being filled up with strings that only apply to a single Cobrand that are not relevant to people running their own fairly vanilla version. Restrict the list of templates that we pull strings out of by generating a list of all the ones in currently translated cobrands for minimal impact. Use that to create the FixMyStreet.po file in gettext-extract. I've left the existing cobrands in but at some point we should split these out. Add a specific script for generating Cobrand .po files which extracts strings from the Cobrand's templates. This is then translated and the translated file place in the locale/$lang/LC_MESSAGES. The the merge_cobrand_po script is run which will create an autoCobrand.po file which has the Cobrand strings and the FixMyStreet strings. This is then the domain used by the Cobrand. Any cobrand specific strings from the Perl code are still in the main FixMyStreet .po file but there should be relatively few of them so it's less of an issue. --- bin/generate_cobrand_po | 40 ++++++++++++++++++++++++++++++++++++++++ bin/gettext-extract | 27 +++++++++------------------ bin/merge_cobrand_po | 29 +++++++++++++++++++++++++++++ bin/update_po_header.bash | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 111 insertions(+), 18 deletions(-) create mode 100755 bin/generate_cobrand_po create mode 100755 bin/merge_cobrand_po create mode 100755 bin/update_po_header.bash (limited to 'bin') diff --git a/bin/generate_cobrand_po b/bin/generate_cobrand_po new file mode 100755 index 000000000..1770de7c2 --- /dev/null +++ b/bin/generate_cobrand_po @@ -0,0 +1,40 @@ +#!/usr/bin/env perl +use strict; + +=for instructions + +This script generates a po file that contains only the strings related to a cobrand. +Its purpose is to avoid having lots of cobrand specific strings in the +main FixMyStreet po file confusing translators. Once this is run the +generated po file can be used to translate the strings. The po files +with the translation should then be placed in the relevant language +directories and merge_cobrand_po run which will merge these with +the main FixMyStreet po files + +=cut + +my $cobrand = shift; + +die "Please provide a cobrand name\n" unless $cobrand; + +my $cobrand_lc = lc( $cobrand ); + +my $PO = "locale/$cobrand.po"; + +my $cobrand_module = "perllib/FixMyStreet/Cobrand/$cobrand.pm"; +my $web_templates = "templates/web/$cobrand_lc/"; + +my $opts = ''; +if ( -e $cobrand_module ) { + $opts .= " $cobrand_module"; +} + +if ( -e -d $web_templates ) { + $opts .= " --directory $web_templates"; +} + +# first we get the strings from the cobrand +system("xgettext.pl --gnu-gettext --verbose --output $PO --plugin perl=* --plugin tt2 $opts"); + +# now replace the template header elements +system("bin/update_po_header.bash $PO"); diff --git a/bin/gettext-extract b/bin/gettext-extract index 934bf1711..a38c17127 100755 --- a/bin/gettext-extract +++ b/bin/gettext-extract @@ -13,28 +13,19 @@ cd "$(dirname $(readlink -f $BASH_SOURCE))/.." PO=locale/FixMyStreet.po rm -f $PO +# we don't want to extract strings from all the cobrand templates so list +# the ones we care about +find templates/web/default templates/web/fixmystreet templates/web/zurich templates/web/fiksgatami templates/web/emptyhomes templates/web/fixmybarangay -name '*.html' > template_list + # Extract from Perl -xgettext.pl --gnu-gettext --verbose --output $PO --plugin perl=* --plugin tt2 --directory perllib --directory templates/web --directory db --directory bin +xgettext.pl --gnu-gettext --verbose --output $PO --plugin perl=* --plugin tt2 --directory perllib -f template_list --directory db --directory bin + +# remove temporary list of templates +rm template_list # Fix headers # no such thing as tempfile on OS X -TEMP=`tempfile 2>/dev/null || mktemp /tmp/gettext-extract.XXXXXX` -NOW=`date +"%Y-%m-%d %H:%M%z"` -# strictly POSIX sed on e.g. OS X doesn't let you used \n in replacements so we do this -nl=$'\n'; -cat $PO | sed " - s/SOME DESCRIPTIVE TITLE/FixMyStreet original .po file, autogenerated by gettext-extract/; - s/YEAR THE PACKAGE'S COPYRIGHT HOLDER/2011 UK Citizens Online Democracy/; - s/PACKAGE package/main FixMyStreet code/; - s/FIRST AUTHOR , YEAR./Matthew Somerville , 2011-06-03./; - - s/PACKAGE VERSION/1.0\\\n\"\\$nl\"Report-Msgid-Bugs-To: matthew@mysociety.org/; - s/POT-Creation-Date: YEAR-MO-DA HO:MI+ZONE/POT-Creation-Date: $NOW/; - s/LL@li.org/team@fixmystreet.com/; - s/charset=CHARSET/charset=UTF-8/; - s/8bit/8bit\\\n\"\\$nl\"Plural-Forms: nplurals=2; plural=n != 1;/; -" >> $TEMP -mv $TEMP $PO +bin/update_po_header.bash $PO echo "$( bin/gettext-nget-patch )" >> $PO diff --git a/bin/merge_cobrand_po b/bin/merge_cobrand_po new file mode 100755 index 000000000..2785162cf --- /dev/null +++ b/bin/merge_cobrand_po @@ -0,0 +1,29 @@ +#!/usr/bin/env perl +use strict; + +=for instructions + +This script is used to merge cobrand po files with the main +FixMyStreet po file. It should be run after generate_cobrand_po +and once the cobrand po files with translations are placed in the +language directories. + +It will then create an autoCobrand.po file for each language that +has a Cobrand.po + +=cut + +my $cobrand = shift; + +die "Please provide a cobrand name\n" unless $cobrand; + +# for each language create a .po file with an existing translations +for (glob( 'locale/*/LC_MESSAGES' ) ) { + my $fms = "$_/FixMyStreet.po"; + my $cobrand_po = "$_/$cobrand.po"; + my $out = "$_/auto$cobrand.po"; + if ( -e $cobrand_po and -e $fms ) { + print "$_\n"; + system("msgcat --no-wrap -o $out $fms $cobrand_po"); + } +} diff --git a/bin/update_po_header.bash b/bin/update_po_header.bash new file mode 100755 index 000000000..2f37ad5d3 --- /dev/null +++ b/bin/update_po_header.bash @@ -0,0 +1,33 @@ +#!/bin/bash +# +# fixmystreet/bin/gettext-extract +# Generate English language .po files from the source code and email templates, +# for FixMyStreet. Writes the output to appropriate .po files in locale/. +# +# Copyright (c) 2011 UK Citizens Online Democracy. All rights reserved. +# Email: matthew@mysociety.org; WWW: http://www.mysociety.org/ + +cd "$(dirname $(readlink -f $BASH_SOURCE))/.." + +# File to write to, clear it to start with +PO=$1 + +# Fix headers +# no such thing as tempfile on OS X +TEMP=`tempfile 2>/dev/null || mktemp /tmp/gettext-extract.XXXXXX` +NOW=`date +"%Y-%m-%d %H:%M%z"` +# strictly POSIX sed on e.g. OS X doesn't let you used \n in replacements so we do this +nl=$'\n'; +cat $PO | sed " + s/SOME DESCRIPTIVE TITLE/FixMyStreet original .po file, autogenerated by gettext-extract/; + s/YEAR THE PACKAGE'S COPYRIGHT HOLDER/2011 UK Citizens Online Democracy/; + s/PACKAGE package/main FixMyStreet code/; + s/FIRST AUTHOR , YEAR./Matthew Somerville , 2011-06-03./; + + s/PACKAGE VERSION/1.0\\\n\"\\$nl\"Report-Msgid-Bugs-To: matthew@mysociety.org/; + s/POT-Creation-Date: YEAR-MO-DA HO:MI+ZONE/POT-Creation-Date: $NOW/; + s/LL@li.org/team@fixmystreet.com/; + s/charset=CHARSET/charset=UTF-8/; + s/8bit/8bit\\\n\"\\$nl\"Plural-Forms: nplurals=2; plural=n != 1;/; +" >> $TEMP +mv $TEMP $PO -- cgit v1.2.3 From 1208af86126d5e030feda2756a0faf93e4366345 Mon Sep 17 00:00:00 2001 From: Struan Donald Date: Tue, 25 Mar 2014 17:00:52 +0000 Subject: script to import zerotb clinic data Closes #703 --- bin/zerotb_import_clinic_list.pl | 115 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100755 bin/zerotb_import_clinic_list.pl (limited to 'bin') diff --git a/bin/zerotb_import_clinic_list.pl b/bin/zerotb_import_clinic_list.pl new file mode 100755 index 000000000..359a63925 --- /dev/null +++ b/bin/zerotb_import_clinic_list.pl @@ -0,0 +1,115 @@ +#!/usr/bin/perl + +use strict; +use FixMyStreet::App; +use Text::CSV; +use Getopt::Long::Descriptive; + +use constant TITLE => 0; +use constant DESC => 1; +use constant LATLONG => 2; +use constant EMAIL => 3; + +my ($opt, $usage) = describe_options( + '%c %o', + ['file|f=s', "path to csv file with list of clinics", { required => 1 } ], + ['email|e=s', "default email address for the updates to be sent to", { required => 1 } ], + ['verbose|v', "print out all services as they are found"], + ['help', "print usage message and exit" ], +); +print($usage->text), exit if $opt->help; + +my $csv = Text::CSV->new ( { binary => 1 } ) # should set binary attribute. + or die "Cannot use CSV: ".Text::CSV->error_diag (); +open my $fh, "<:encoding(utf8)", $opt->file or die "Failed to open " . $opt->file . ": $!"; + +my $clinic_user = FixMyStreet::App->model('DB::User')->find_or_create({ + email => $opt->email +}); +if ( not $clinic_user->in_storage ) { + $clinic_user->insert; +} + +# throw away header line +my $title_row = $csv->getline( $fh ); + +while ( my $row = $csv->getline( $fh ) ) { + my $clinics = FixMyStreet::App->model('DB::Problem')->search({ + title => $row->[TITLE] + }); + + my ($lat, $long) = split(',', $row->[LATLONG]); + my $p; + my $count = $clinics->count; + if ( $count == 0 ) { + $p = FixMyStreet::App->model('DB::Problem')->create({ + title => $row->[TITLE], + latitude => $lat, + longitude => $long, + used_map => 1, + anonymous => 1, + state => 'unconfirmed', + name => '', + user => $clinic_user, + detail => '', + areas => '', + postcode => '' + }); + } elsif ( $count == 1 ) { + $p = $clinics->first; + } else { + printf "Too many matches for: %s\n", $row->[TITLE]; + next; + } + $p->detail( $row->[DESC] ); + $p->latitude( $lat ); + $p->longitude( $long ); + $p->confirm; + + if ( $p->in_storage ) { + printf( "Updating entry for %s\n", $row->[TITLE] ) if $opt->verbose; + $p->update; + } else { + printf( "Creating entry for %s\n", $row->[TITLE] ) if $opt->verbose; + $p->insert; + } + $p->discard_changes; + + # disabling existing alerts in case email addresses have changed + my $existing = FixMyStreet::App->model('DB::Alert')->search({ + alert_type => 'new_updates', + parameter => $p->id + }); + $existing->update( { confirmed => 0 } ); + + if ( $row->[EMAIL] ) { + my $u = FixMyStreet::App->model('DB::User')->find_or_new({ + email => $row->[EMAIL] + }); + $u->insert unless $u->in_storage; + create_update_alert( $u, $p, $opt->verbose ); + } + + create_update_alert( $clinic_user, $p, $opt->verbose ); +} + +sub create_update_alert { + my ( $user, $p, $verbose ) = @_; + my $a = FixMyStreet::App->model('DB::Alert')->find_or_new({ + alert_type => 'new_updates', + user => $user, + parameter => $p->id, + }); + + $a->confirmed(1); + + if ( $a->in_storage ) { + printf( "Updating update alert for %s on %s\n", $user->email, $p->title ) + if $verbose; + $a->update; + } else { + printf( "Creating update alert for %s on %s\n", $user->email, $p->title ) + if $verbose; + $a->insert; + } +} -- cgit v1.2.3