[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

SF.net SVN: ledger-smb: [1308] trunk



Revision: 1308
          http://svn.sourceforge.net/ledger-smb/?rev=1308&view=rev
Author:   tetragon
Date:     2007-06-22 12:04:24 -0700 (Fri, 22 Jun 2007)

Log Message:
-----------
Template handling adjustments

Modified Paths:
--------------
    trunk/LedgerSMB/Template/HTML.pm
    trunk/LedgerSMB/Template/PDF.pm
    trunk/LedgerSMB/Template/PS.pm
    trunk/LedgerSMB/Template/TXT.pm
    trunk/LedgerSMB/Template.pm
    trunk/t/04-template-handling.t

Added Paths:
-----------
    trunk/LedgerSMB/Template/TTI18N.pm
    trunk/t/data/04-gettext.html
    trunk/t/data/04-gettext.tex

Modified: trunk/LedgerSMB/Template/HTML.pm
===================================================================
--- trunk/LedgerSMB/Template/HTML.pm	2007-06-22 18:57:50 UTC (rev 1307)
+++ trunk/LedgerSMB/Template/HTML.pm	2007-06-22 19:04:24 UTC (rev 1308)
@@ -42,6 +42,7 @@
 use Error qw(:try);
 use CGI;
 use Template;
+use LedgerSMB::Template::TTI18N;
 
 sub get_template {
     my $name = shift;
@@ -54,6 +55,7 @@
     my $type = ref $rawvars;
 
     #XXX fix escaping function
+    return $rawvars if $type =~ /^LedgerSMB::Locale/;
     if ( $type eq 'ARRAY' ) {
         for (@{$rawvars}) {
             push @{$vars}, preprocess( $_ );
@@ -83,7 +85,9 @@
 		}) || throw Error::Simple Template->error(); 
 	if (not $template->process(
 		get_template($parent->{template}), 
-		$cleanvars, "$parent->{outputfile}.html", binmode => ':utf8')) {
+		{%$cleanvars, %$LedgerSMB::Template::TTI18N::ttfuncs,
+			'escape' => \&preprocess},
+		"$parent->{outputfile}.html", binmode => ':utf8')) {
 		throw Error::Simple $template->error();
 	}
 	$parent->{mimetype} = 'text/html';

Modified: trunk/LedgerSMB/Template/PDF.pm
===================================================================
--- trunk/LedgerSMB/Template/PDF.pm	2007-06-22 18:57:50 UTC (rev 1307)
+++ trunk/LedgerSMB/Template/PDF.pm	2007-06-22 19:04:24 UTC (rev 1308)
@@ -40,6 +40,7 @@
 
 use Error qw(:try);
 use Template::Latex;
+use LedgerSMB::Template::TTI18N;
 
 sub get_template {
 	my $name = shift;
@@ -51,6 +52,7 @@
 	my $vars;
 	my $type = ref $rawvars;
 
+	return $rawvars if $type =~ /^LedgerSMB::Locale/;
 	if ($type eq 'ARRAY') {
 		for (@{$rawvars}) {
 			push @{$vars}, preprocess($_);
@@ -83,7 +85,9 @@
 
 	if (not $template->process(
 		get_template($parent->{template}), 
-		$cleanvars, "$parent->{outputfile}.pdf", binmode => 1)) {
+		{%$cleanvars, %$LedgerSMB::Template::TTI18N::ttfuncs,
+			'escape' => \&preprocess},
+		"$parent->{outputfile}.pdf", binmode => 1)) {
 		throw Error::Simple $template->error();
 	}
 	$parent->{mimetype} = 'application/pdf';

Modified: trunk/LedgerSMB/Template/PS.pm
===================================================================
--- trunk/LedgerSMB/Template/PS.pm	2007-06-22 18:57:50 UTC (rev 1307)
+++ trunk/LedgerSMB/Template/PS.pm	2007-06-22 19:04:24 UTC (rev 1308)
@@ -40,6 +40,7 @@
 
 use Error qw(:try);
 use Template::Latex;
+use LedgerSMB::Template::TTI18N;
 
 sub get_template {
 	my $name = shift;
@@ -69,7 +70,9 @@
 
 	if (not $template->process(
 		get_template($parent->{template}), 
-		$cleanvars, "$parent->{outputfile}.ps", binmode => ':utf8')) {
+		{%$cleanvars, %$LedgerSMB::Template::TTI18N::ttfuncs,
+			'escape' => \&preprocess},
+		"$parent->{outputfile}.ps", binmode => 1)) {
 		throw Error::Simple $template->error();
 	}
 	$parent->{mimetype} = 'application/postscript';

Added: trunk/LedgerSMB/Template/TTI18N.pm
===================================================================
--- trunk/LedgerSMB/Template/TTI18N.pm	                        (rev 0)
+++ trunk/LedgerSMB/Template/TTI18N.pm	2007-06-22 19:04:24 UTC (rev 1308)
@@ -0,0 +1,53 @@
+
+=head1 NAME
+
+LedgerSMB::Template::TTI18N  Template Toolkit i18n support functions
+
+=head1 SYNOPSIS
+
+Various functions for Template Toolkit templates for internationalisation
+support.
+
+=head1 METHODS
+
+=over
+
+=item <?lsmb gettext(locale, 'string [_1]', param) ?>
+
+Output the gettext translation for the string in the given locale.  If
+locale is a LedgerSMB::Locale object, it uses it.  If it is a string, the
+locale is loaded, cached, and used.
+
+=back
+
+=head1 Copyright (C) 2007, The LedgerSMB core team.
+
+This work contains copyrighted information from a number of sources all used
+with permission.  
+
+It is released under the GNU General Public License Version 2 or, at your 
+option, any later version.  See COPYRIGHT file for details.  For a full list 
+including contact information of contributors, maintainers, and copyright 
+holders, see the CONTRIBUTORS file.
+=cut
+
+package LedgerSMB::Template::TTI18N;
+
+use LedgerSMB::Locale;
+
+my %locales; # Cache string-loaded locales
+our $ttfuncs = {};
+
+$ttfuncs->{gettext} = sub {
+	my $locale = shift;
+	if (ref $locale) {
+		return $locale->text(@_);
+	} elsif ($locales{$locale}) {
+		return $locales{$locale}->text(@_);
+	} else {
+		$locales{$locale} = LedgerSMB::Locale->get_handle($locale);
+		return $locales{$locale}->text(@_);
+	}
+};
+
+

Modified: trunk/LedgerSMB/Template/TXT.pm
===================================================================
--- trunk/LedgerSMB/Template/TXT.pm	2007-06-22 18:57:50 UTC (rev 1307)
+++ trunk/LedgerSMB/Template/TXT.pm	2007-06-22 19:04:24 UTC (rev 1308)
@@ -40,6 +40,7 @@
 
 use Error qw(:try);
 use Template;
+use LedgerSMB::Template::TTI18N;
 
 sub get_template {
     my $name = shift;
@@ -65,7 +66,9 @@
 
 	if (not $template->process(
 		get_template($parent->{template}), 
-		$cleanvars, "$parent->{outputfile}.txt", binmode => ':utf8')) {
+		{%$cleanvars, %$LedgerSMB::Template::TTI18N::ttfuncs,
+			'escape' => \&preprocess},
+		"$parent->{outputfile}.txt", binmode => ':utf8')) {
 		throw Error::Simple $template->error();
 	}
 	$parent->{mimetype} = 'text/plain';

Modified: trunk/LedgerSMB/Template.pm
===================================================================
--- trunk/LedgerSMB/Template.pm	2007-06-22 18:57:50 UTC (rev 1307)
+++ trunk/LedgerSMB/Template.pm	2007-06-22 19:04:24 UTC (rev 1308)
@@ -11,13 +11,16 @@
 
 =over
 
-=item new(user => \%myconfig, template => $string, format => 'HTML', [language => $string,] [include_path => $path]);
+=item new(user => \%myconfig, template => $string, format => 'HTML', [language => $string,] [include_path => $path], [no_auto_output => $bool], [method => $string] );
 
 This command instantiates a new template:
 template is the file name of the template to be processed.
 format is the type of format to be used.  Currently only HTML is supported
 language (optional) specifies the language for template selection.
 include_path allows one to override the template directory and use this with user interface templates.
+no_auto_output disables the automatic output of rendered templates.
+method is the output method to use, defaults to HTTP
+media is a synonym for method
 
 =item render($hashref)
 
@@ -63,10 +66,13 @@
 			"${LedgerSMB::Sysconfig::tempdir}/$args{outputfile}";
 	} else {
 		$self->{outputfile} =
-			"${LedgerSMB::Sysconfig::tempdir}/$args{template}-output";
+			"${LedgerSMB::Sysconfig::tempdir}/$args{template}-output-$$";
 	}
 	$self->{include_path} = $args{path};
 	$self->{locale} = $args{locale};
+	$self->{noauto} = $args{noauto};
+	$self->{method} = $args{method};
+	$self->{method} ||= $args{media};
 
 	bless $self, $class;
 
@@ -112,13 +118,18 @@
 	}
 
 	$format->can('process')->($self, $cleanvars);
-	return $format->can('postprocess')->($self);
+	#return $format->can('postprocess')->($self);
+	my $post = $format->can('postprocess')->($self);
+	if (!$self->{'noauto'}) {
+		$self->output;
+	}
+	return $post;
 }
 
 sub output {
 	my $self = shift;
 	my %args = @_;
-	my $method = $args{method} || $args{media};
+	my $method = $self->{method} || $args{method} || $args{media};
 
 	if ('email' eq lc $method) {
 		$self->_email_output;

Modified: trunk/t/04-template-handling.t
===================================================================
--- trunk/t/04-template-handling.t	2007-06-22 18:57:50 UTC (rev 1307)
+++ trunk/t/04-template-handling.t	2007-06-22 19:04:24 UTC (rev 1308)
@@ -18,8 +18,8 @@
 use LedgerSMB::Locale;
 use LedgerSMB::Template;
 use LedgerSMB::Template::HTML;
+use LedgerSMB::Template::PDF;
 use LedgerSMB::Template::PS;
-use LedgerSMB::Template::PDF;
 use LedgerSMB::Template::TXT;
 
 $LedgerSMB::Sysconfig::tempdir = 't/var';
@@ -193,7 +193,7 @@
 
 $template = undef;
 $template = new LedgerSMB::Template('user' => $myconfig, 'format' => 'HTML', 
-	'template' => '04-template-2');
+	'template' => '04-template-2', 'no_auto_output' => 1);
 ok(defined $template, 
 	'Template, new: Object creation with non-existent template');
 throws_ok{$template->render({'login' => 'foo'})} qr/not found/,
@@ -201,7 +201,7 @@
 
 $template = undef;
 $template = new LedgerSMB::Template('user' => $myconfig, 'format' => 'TODO', 
-	'template' => '04-template');
+	'template' => '04-template', 'no_auto_output' => 1);
 ok(defined $template, 
 	'Template, new: Object creation with non-existent format');
 throws_ok{$template->render({'login' => 'foo'})} qr/Can't locate/,
@@ -213,80 +213,136 @@
 
 $template = undef;
 $template = new LedgerSMB::Template('user' => $myconfig, 'format' => 'PDF', 
-	'template' => '04-template');
+	'template' => '04-template', 'no_auto_output' => 1);
 ok(defined $template, 
 	'Template, new (PDF): Object creation with format and template');
 isa_ok($template, 'LedgerSMB::Template', 
 	'Template, new (PDF): Object creation with format and template');
 is($template->{include_path}, 't/data',
 	'Template, new (PDF): Object creation with format and template');
-is($template->render({'login' => 'foo&bar'}), 't/var/04-template-output.pdf',
+is($template->render({'login' => 'foo&bar'}), "t/var/04-template-output-$$.pdf",
 	'Template, render (PDF): Simple PDF template, default filename');
-ok(-e 't/var/04-template-output.pdf', 'Template, render (PDF): File created');
-is(unlink('t/var/04-template-output.pdf'), 1,
+ok(-e "t/var/04-template-output-$$.pdf",
+	'Template, render (PDF): File created');
+is(unlink("t/var/04-template-output-$$.pdf"), 1,
 	'Template, render (PDF): removing testfile');
-ok(!-e 't/var/04-template-output.pdf',
+ok(!-e "t/var/04-template-output-$$.pdf",
 	'Template, render (PDF): testfile removed');
 
 $template = undef;
 $template = new LedgerSMB::Template('user' => $myconfig, 'format' => 'PS', 
-	'template' => '04-template');
+	'template' => '04-template', 'no_auto_output' => 1);
 ok(defined $template, 
 	'Template, new (PS): Object creation with format and template');
 isa_ok($template, 'LedgerSMB::Template', 
 	'Template, new (PS): Object creation with format and template');
 is($template->{include_path}, 't/data',
 	'Template, new (PS): Object creation with format and template');
-is($template->render({'login' => 'foo\&bar'}), 't/var/04-template-output.ps',
+is($template->render({'login' => 'foo\&bar'}),
+	"t/var/04-template-output-$$.ps",
 	'Template, render (PS): Simple Postscript template, default filename');
-ok(-e 't/var/04-template-output.ps', 'Template, render (PS): File created');
-is(unlink('t/var/04-template-output.ps'), 1,
+ok(-e "t/var/04-template-output-$$.ps", 'Template, render (PS): File created');
+is(unlink("t/var/04-template-output-$$.ps"), 1,
 	'Template, render (PS): removing testfile');
-ok(!-e 't/var/04-template-output.ps',
+ok(!-e "t/var/04-template-output-$$.ps",
 	'Template, render (PS): testfile removed');
 
 $template = undef;
 $template = new LedgerSMB::Template('user' => $myconfig, 'format' => 'TXT', 
-	'template' => '04-template');
+	'template' => '04-template', 'no_auto_output' => 1);
 ok(defined $template, 
 	'Template, new (TXT): Object creation with format and template');
 isa_ok($template, 'LedgerSMB::Template', 
 	'Template, new (TXT): Object creation with format and template');
 is($template->{include_path}, 't/data',
 	'Template, new (TXT): Object creation with format and template');
-is($template->render({'login' => 'foo&bar'}), 't/var/04-template-output.txt',
+is($template->render({'login' => 'foo&bar'}),
+	"t/var/04-template-output-$$.txt",
 	'Template, render: Simple text template, default filename');
-ok(-e 't/var/04-template-output.txt', 'Template, render (TXT): File created');
-open($FH, '<', 't/var/04-template-output.txt');
+ok(-e "t/var/04-template-output-$$.txt",
+	'Template, render (TXT): File created');
+open($FH, '<', "t/var/04-template-output-$$.txt");
 @r = <$FH>;
 close($FH);
 chomp(@r);
 is(join("\n", @r), "I am a template.\nLook at me foo&bar.", 
 	'Template, render (TXT): Simple TXT template, correct output');
-is(unlink('t/var/04-template-output.txt'), 1,
+is(unlink("t/var/04-template-output-$$.txt"), 1,
 	'Template, render (TXT): removing testfile');
-ok(!-e 't/var/04-template-output.html',
+ok(!-e "t/var/04-template-output-$$.txt",
 	'Template, render (TXT): testfile removed');
 
 $template = undef;
 $template = new LedgerSMB::Template('user' => $myconfig, 'format' => 'HTML', 
-	'template' => '04-template');
+	'template' => '04-template', 'no_auto_output' => 1);
 ok(defined $template, 
 	'Template, new (HTML): Object creation with format and template');
 isa_ok($template, 'LedgerSMB::Template', 
 	'Template, new (HTML): Object creation with format and template');
 is($template->{include_path}, 't/data',
 	'Template, new (HTML): Object creation with format and template');
-is($template->render({'login' => 'foo&bar'}), 't/var/04-template-output.html',
+is($template->render({'login' => 'foo&bar'}),
+	"t/var/04-template-output-$$.html",
 	'Template, render (HTML): Simple HTML template, default filename');
-ok(-e 't/var/04-template-output.html', 'Template, render (HTML): File created');
-open($FH, '<', 't/var/04-template-output.html');
+ok(-e "t/var/04-template-output-$$.html",
+	'Template, render (HTML): File created');
+open($FH, '<', "t/var/04-template-output-$$.html");
 @r = <$FH>;
 close($FH);
 chomp(@r);
 is(join("\n", @r), "I am a template.\nLook at me foo&amp;bar.", 
 	'Template, render (HTML): Simple HTML template, correct output');
-is(unlink('t/var/04-template-output.html'), 1,
+is(unlink("t/var/04-template-output-$$.html"), 1,
 	'Template, render (HTML): removing testfile');
-ok(!-e 't/var/04-template-output.html',
+ok(!-e "t/var/04-template-output-$$.html",
 	'Template, render (HTML): testfile removed');
+
+$template = undef;
+$template = new LedgerSMB::Template('user' => $myconfig, 'format' => 'HTML', 
+	'template' => '04-gettext', 'outputfile' => '04-gettext',
+	'no_auto_output' => 1);
+ok(defined $template, 
+	'Template, new (HTML): Object creation with outputfile');
+isa_ok($template, 'LedgerSMB::Template', 
+	'Template, new (HTML): Object creation with outputfile');
+is($template->{include_path}, 't/data',
+	'Template, new (HTML): Object creation with outputfile');
+is($template->render({'month' => 'June', 'login' => 'foo&bar', 
+	'fr' => $locale}), 't/var/04-gettext.html',
+	'Template, render (HTML): Gettext HTML template');
+ok(-e 't/var/04-gettext.html', 'Template, render (HTML): File created');
+open($FH, '<', 't/var/04-gettext.html');
..hidden.. = <$FH>;
+close($FH);
+chomp(@r);
+is(join("\n", @r), 
+	"I am a foo&amp;bar.\nLook at me Juin.\njuni\nAan foo&amp;bar", 
+	'Template, render (HTML): Gettext HTML template, correct output');
+is(unlink('t/var/04-gettext.html'), 1,
+	'Template, render (HTML): removing testfile');
+ok(!-e 't/var/04-gettext.html',
+	'Template, render (HTML): testfile removed');
+
+## XeTeX test, requires PDFLATEX to be xelatex and modified Template::Latex
+SKIP: {
+	skip 'XeTeX and modified Template::Latex requiring PDF tests';
+	$template = undef;
+	$template = new LedgerSMB::Template('user' => $myconfig,
+		'format' => 'PDF', 'template' => '04-gettext',
+		'no_auto_output' => 1);
+	ok(defined $template, 
+		'Template, new (PDF): XeTeX template creation');
+	isa_ok($template, 'LedgerSMB::Template', 
+		'Template, new (PDF): XeTeX template creation');
+	is($template->{include_path}, 't/data',
+		'Template, new (PDF): XeTeX template creation');
+	is($template->render({'login' => 'foo&bar'}),
+		"t/var/04-gettext-output-$$.pdf",
+		'Template, render (PDF): XeTeX PDF template, default filename');
+	ok(-e "t/var/04-gettext-output-$$.pdf",
+		'Template, render (PDF): File created');
+	is(unlink("t/var/04-gettext-output-$$.pdf"), 1,
+		'Template, render (PDF): removing testfile');
+	ok(!-e "t/var/04-gettext-output-$$.pdf",
+		'Template, render (PDF): testfile removed');
+}

Added: trunk/t/data/04-gettext.html
===================================================================
--- trunk/t/data/04-gettext.html	                        (rev 0)
+++ trunk/t/data/04-gettext.html	2007-06-22 19:04:24 UTC (rev 1308)
@@ -0,0 +1,4 @@
+I am a <?lsmb login ?>.
+Look at me <?lsmb gettext(fr, "June") ?>.
+<?lsmb gettext('nl', "June") ?>
+<?lsmb gettext('nl', "To [_1]", login) ?>

Added: trunk/t/data/04-gettext.tex
===================================================================
--- trunk/t/data/04-gettext.tex	                        (rev 0)
+++ trunk/t/data/04-gettext.tex	2007-06-22 19:04:24 UTC (rev 1308)
@@ -0,0 +1,12 @@
+<?lsmb FILTER latex ?>
+\documentclass{scrartcl}
+\usepackage[utf8]{inputenc}
+\font\han="STSong" at 16pt
+\font\rom="Gentium" at 16pt
+\begin{document}
+I am a <?lsmb login ?>.
+test: {\han <?lsmb gettext('zh_CN', "June") ?>.}
+Foo
+\rom <?lsmb escape(gettext('fr', "Goods & Services")) ?>
+\end{document}
+<?lsmb END ?>


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.