aboutsummaryrefslogtreecommitdiffstats
path: root/spec/lib/i18n_interpolation.rb
blob: b07cf1e9adf46f8cb90d22156ad582715384d3f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

describe "when using i18n" do

    it "should not complain if we're missing variables from the string" do
        result = _('Hello', :dip => 'hummus')
        result.should == 'Hello'
        result = _('Hello {{dip}}', :dip => 'hummus')
        result.should == 'Hello hummus'
    end

    it "should assume that simple translations are always html safe" do
      _("Hello").should be_html_safe
    end
end

describe "n_" do
    it "should return the translated singular" do
      FastGettext.should_receive(:n_).with("Apple", "Apples", 1).and_return("Apfel")
      n_("Apple", "Apples", 1).should == "Apfel"
    end

    it "should return the translated plural" do
      FastGettext.should_receive(:n_).with("Apple", "Apples", 3).and_return("Äpfel")
      n_("Apple", "Apples", 3).should == "Äpfel"
    end

    it "should return the translated singular interpolated" do
      FastGettext.should_receive(:n_).with("I eat {{count}} apple", "I eat {{count}} apples", 1).
        and_return("Ich esse {{count}} Apfel")
      n_("I eat {{count}} apple", "I eat {{count}} apples", 1, :count => 1).should == "Ich esse 1 Apfel"
    end

    it "should return the translated plural interpolated" do
      FastGettext.should_receive(:n_).with("I eat {{count}} apple", "I eat {{count}} apples", 3).
        and_return("Ich esse {{count}} Äpfel")
      n_("I eat {{count}} apple", "I eat {{count}} apples", 3, :count => 3).should == "Ich esse 3 Äpfel"
    end

    it "should always be html safe when there is no interpolation" do
      FastGettext.should_receive(:n_).with("Apple", "Apples", 1).and_return("Apfel")
      n_("Apple", "Apples", 1).should be_html_safe
    end
end

describe "gettext_interpolate" do
    context "html unsafe string" do
        let(:string) { "Hello {{a}}" }

        it "should give an unsafe result" do
          result = gettext_interpolate(string, :a => "foo")
          result.should == "Hello foo"
          result.should_not be_html_safe
        end

        it "should give an unsafe result" do
          result = gettext_interpolate(string, :a => "foo".html_safe)
          result.should == "Hello foo"
          result.should_not be_html_safe
        end        
    end

    context "html safe string" do
        let(:string) { "Hello {{a}}".html_safe }

        it "should quote the input if it's unsafe" do
          result = gettext_interpolate(string, :a => "foo&")
          result.should == "Hello foo&"
          result.should be_html_safe
        end

        it "should not quote the input if it's safe" do
          result = gettext_interpolate(string, :a => "foo&".html_safe)
          result.should == "Hello foo&"
          result.should be_html_safe
        end        
    end
end
$1" su postgres -c "dropdb $dbname" } pgsql_remove_user() { dbuser="$1" su postgres -c "dropuser $dbuser" } postgis_bootstrap() { case "$codename" in lenny) POSTGIS_SQL_PATH=/usr/share/postgresql-8.3-postgis POSTGISSQL=lwpostgis.sql ;; *) POSTGIS_SQL_PATH=/usr/share/postgresql/8.4/contrib/postgis-1.5 POSTGISSQL=postgis.sql ;; esac su postgres -c "createdb -E UTF8 template_postgis" su postgres -c "createlang -d template_postgis plpgsql" su postgres -c "psql -d postgres -c \"UPDATE pg_database SET datistemplate='true' WHERE datname='template_postgis';\"" # Loading the PostGIS SQL routines su postgres -c "psql -d template_postgis -f $POSTGIS_SQL_PATH/$POSTGISSQL" su postgres -c "psql -d template_postgis -f $POSTGIS_SQL_PATH/spatial_ref_sys.sql" # Enabling users to alter spatial tables. su postgres -c "psql -d template_postgis -c \"GRANT ALL ON geometry_columns TO PUBLIC;\"" su postgres -c "psql -d template_postgis -c \"GRANT ALL ON spatial_ref_sys TO PUBLIC;\"" } postgis_remove() { # Change template status to make it possible to drop the database su postgres -c "psql -d postgres -c \"UPDATE pg_database SET datistemplate='false' WHERE datname='template_postgis';\"" su postgres -c "dropdb template_postgis" } apache_config() { cat > /etc/apache2/sites-available/fixmystreet <<EOF <VirtualHost *:80> ServerName $servername ServerAlias fiksgatami.no www.fiksgatami.no DocumentRoot $basedir/fixmystreet/web/ # 5 MB limit to allow large images to be uploaded FcgidMaxRequestLen 5242880 RewriteEngine on RewriteCond %{HTTP_HOST} ^fiksgatami\.no RewriteRule ^(.*)$ http://www.fiksgatami.no$1 [R=permanent,L] RewriteCond %{HTTP_HOST} ^fiksgatami\.nuug\.no RewriteRule ^(.*)$ http://www.fiksgatami.no$1 [R=permanent,L] # Pull in the specific config Include $basedir/fixmystreet/conf/httpd.conf <Directory $basedir/fixmystreet/web> # You also need to enable cgi files to run as CGI scripts. For example: # on production servers these are run under fastcgi Options +ExecCGI AddHandler fcgid-script .cgi </Directory> <Location /admin> Options +ExecCGI AddHandler fcgid-script .cgi AllowOverride AuthConfig Order Allow,Deny Allow From All AuthName "Fiksgatami Admin Access" AuthType Basic AuthUserFile /etc/apache2/htpasswd.fixmystreet.users require valid-user </Location> Alias /admin/ $basedir/fixmystreet/web-admin/ Alias /jslib/ $basedir/fixmystreet/commonlib/jslib/ </VirtualHost> EOF ( cd $basedir sed s/mapit.mysociety.org/mapit.nuug.no/ \ < fixmystreet/conf/httpd.conf-example \ > fixmystreet/conf/httpd.conf ) htpwdfile=/etc/apache2/htpasswd.fixmystreet.users if [ ! -e $htpwdfile ] ; then htpasswd -cmb $htpwdfile admin "$webpassword" fi case "$codename" in lenny) djangodir=/var/lib/python-support/python2.5/django ;; *) djangodir=/var/lib/python-support/python2.6/django ;; esac cat > /etc/apache2/sites-available/mapit <<EOF <VirtualHost *:80> ServerName mapit.nuug.no ServerAlias mapit-dev.nuug.no DocumentRoot $basedir/mapit/web/ WSGIDaemonProcess mapit.nuug.no user=www-data group=www-data processes=5 threads=1 display-name=mapit.nuug.no WSGIProcessGroup mapit.nuug.no WSGIScriptAlias / $basedir/mapit/web/django.wsgi Alias /media $djangodir/contrib/admin/media Alias /static $basedir/mapit/mapit/static <Directory $basedir/mapit/web> # You also need to enable cgi files to run as CGI scripts. For example: # on production servers these are run under fastcgi Options +ExecCGI AddHandler fcgid-script .cgi </Directory> </VirtualHost> EOF a2dissite default a2ensite fixmystreet a2ensite mapit a2enmod proxy rewrite a2enmod proxy_http a2enmod expires /etc/init.d/apache2 restart } apache_stop() { /etc/init.d/apache2 stop } apache_remove() { a2dissite fixmystreet a2dissite mapit /etc/init.d/apache2 restart rm /etc/apache2/sites-available/fixmystreet rm /etc/apache2/sites-available/mapit } fetch_git_source() { for gitmodule in fixmystreet mapit ; do if [ ! -d $gitmodule ] ; then git clone git://github.com/petterreinholdtsen/$gitmodule.git -b $servername || \ git clone https://github.com/mysociety/$gitmodule.git (cd $gitmodule && git submodule update --init) else (cd $gitmodule && git pull && git submodule update && git diff) | cat fi done # Update translation files (cd fixmystreet && commonlib/bin/gettext-makemo) # Generate CSS files (cd fixmystreet && bin/make_css) # Not quite sure why this directory is needed mkdir -p fixmystreet/perl-external/local-lib } update_fixmystreet_config() { dbuser="$1" fgmdbpassword="$2" dbname="$3" cat > $basedir/fixmystreet/conf/general.yml <<EOF # general-example.yml: # Example values for the "general" config file. # # Configuration parameters, in YAML syntax. # # Copy this file to one called "general.yml" in the same directory. Or # have multiple config files and use a symlink to change between them. FMS_DB_HOST: 'localhost' FMS_DB_PORT: '5432' FMS_DB_NAME: '$dbname' FMS_DB_USER: '$dbuser' FMS_DB_PASS: '$fgmdbpassword' BASE_URL: 'http://$servername' EMAIL_DOMAIN: 'nuug.no' CONTACT_EMAIL: '' CONTACT_NAME: 'FiksGataMi' STAGING_SITE: 1 UPLOAD_DIR: '/var/lib/fixmystreet/upload/' GEO_CACHE: '/var/lib/fixmystreet/cache/' GOOGLE_MAPS_API_KEY: '' MAPIT_URL: 'http://mapit.nuug.no' MAP_TYPE: 'OSM' GAZE_URL: 'http://gaze.mysociety.org/gaze' # empty = use /usr/sbin/sendmail' SMTP_SMARTHOST: '' AUTH_SHARED_SECRET: '' HEARFROMYOURMP_BASE_URL: '' ANDROID_URL: 'http://wiki.nuug.no/grupper/fiksgatami/android' ALLOWED_COBRANDS: 'fiksgatami' # How many items are returned in the GeoRSS feeds by default RSS_LIMIT: '200' OSM_GUESS_OPERATOR: 1 # Should problem reports link to the council summary pages? AREA_LINKS_FROM_PROBLEMS: 1 EOF if [ ! -d /var/lib/fixmystreet ] ; then ( mkdir /var/lib/fixmystreet && \ cd /var/lib/fixmystreet && \ mkdir cache && \ mkdir upload && \ chown www-data cache && \ chown www-data upload ) fi } update_mapit_config() { dbuser="$1" dbpassword="$2" dbname="$3" if [ -e $basedir/mapit/conf/general ] ; then mv $basedir/mapit/conf/general $basedir/mapit/conf/general.old-$$ fi cat > $basedir/mapit/conf/general.yml <<EOF # Connection details for database MAPIT_DB_NAME: '$dbname' MAPIT_DB_USER: '$dbuser' MAPIT_DB_PASS: '$dbpassword' MAPIT_DB_HOST: null MAPIT_DB_PORT: null # Country is currently one of GB, NO, or KE COUNTRY: 'NO' # An EPSG code for what the areas are stored as. 27700 is OSGB, 4326 for WGS84. AREA_SRID: 4326 STAGING: 0 RATE_LIMIT: [] BUGS_EMAIL: '' DJANGO_SECRET_KEY: '' EOF } mapit_manage="$basedir/mapit/project/manage.py" load_mapit_n5000() { N5000SHP="$basedir/N5000 shape/N5000_AdministrativFlate.shp" su www-data -c "$mapit_manage generation_create --commit --desc 'N5000 Norway'" su www-data -c "$mapit_manage import_norway_n5000 --commit '$N5000SHP'" # su www-data -c "$mapit_manage find_parents" su www-data -c "$mapit_manage generation_activate --commit" } load_mapit_osm() { python $basedir/mapit/bin/osm_to_kml # This require changes in the code if [ new-generation = "$1" ] ; then su www-data -c "$mapit_manage generation_create --commit --desc 'OpenStreetmap.org'" fi su www-data -c "$mapit_manage import_norway_osm --commit $basedir/mapit/data/cache/*.kml" # su www-data -c "$mapit_manage find_parents" # su www-data -c "$mapit_manage import_area_unions --commit data/norway/regions.csv" if [ new-generation = "$1" ] ; then su www-data -c "$mapit_manage generation_activate --commit" fi } load_mapit_postcodes() { rm -f postnummer-utf8.txt postnummer-ekstra.csv wget http://www.erikbolstad.no/nedlasting/postnummer-utf8.txt wget http://www.nuug.no/prosjekt/fiksgatami/postnummer-ekstra.csv su www-data -c "$mapit_manage import_bolstad_postcodes postnummer-utf8.txt" su www-data -c "$mapit_manage import_bolstad_postcodes postnummer-ekstra.csv" } # This one need to run as user www-data load_mapit() { su www-data -c "$mapit_manage syncdb --noinput" su www-data -c "$mapit_manage migrate" su www-data -c "$mapit_manage loaddata norway" #load_mapit_n5000 load_mapit_osm new-generation load_mapit_postcodes || true } setup_locale() { cat > /etc/locale.gen <<EOF en_GB.UTF-8 UTF-8 en_US.UTF-8 UTF-8 nb_NO.UTF-8 UTF-8 nn_NO.UTF-8 UTF-8 EOF locale-gen } flush_memcache() { (echo flush_all; sleep 1) | telnet localhost 11211 || true } append_if_missing() { file="$1" string="$2" if [ -e "$file" ] ; then if ! grep -qxF "$string" "$file" ; then echo "Appending '$string' to $file." echo "$string" >> $file fi fi } tune_postgresql() { conf=/etc/postgresql/8.4/main/postgresql.conf append_if_missing $conf "effective_cache_size = 256MB" append_if_missing $conf "shared_buffers = 128MB" append_if_missing $conf "log_min_duration_statement = 250" touch /etc/sysctl.d/postgresql.conf append_if_missing /etc/sysctl.d/postgresql.conf "kernel.shmmax = 136314880" sysctl -w kernel.shmmax=$(( 130 * 1024 * 1024 )) /etc/init.d/postgresql reload } cd /srv/$servername/ case "$1" in bootstrap) fetch_git_source install_debs if [ yes = "$CATALYST" ] ; then install_dev_catalyst fi # Not yet ready, should adjust dynamically based on memory size # tune_postgresql # Password to use when connecting to the postgresql database. fgmdbpassword="$(pwgen -1)" midbpassword="$(pwgen -1)" webpassword="$(pwgen -1)" postgis_bootstrap pgsql_createuser www-data "$fgmdbpassword" pgsql_createuser mapit "$midbpassword" pgsql_fixmystreet_bootstrap www-data fixmystreet update_fixmystreet_config www-data "$fgmdbpassword" fixmystreet pgsql_mapit_bootstrap www-data mapit update_mapit_config www-data "$midbpassword" mapit load_mapit setup_locale apache_config ;; remove) apache_stop flush_memcache pgsql_remove_db fixmystreet || true pgsql_remove_db mapit || true pgsql_remove_user mapit || true pgsql_remove_user www-data || true postgis_remove || true apache_remove || true rm $basedir/mapit/data/cache/relation-* ;; update) fetch_git_source fgmdbpassword=$(grep OPTION_FMS_DB_PASS $basedir/fixmystreet/conf/general | cut -d\' -f4) midbpassword=$(grep OPTION_MAPIT_DB_PASS $basedir/mapit/conf/general | cut -d\' -f4) update_fixmystreet_config www-data $fgmdbpassword fixmystreet update_mapit_config www-data $midbpassword mapit apache_config flush_memcache ;; update_map) rm $basedir/mapit/data/cache/relation-* load_mapit_osm flush_memcache ;; esac