Bugzilla – Attachment 81737 Details for
Bug 15395
Internationalization: plural forms, context, and more
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 15395: Add unit tests for extract_messages_from_templates
Bug-15395-Add-unit-tests-for-extractmessagesfromte.patch (text/plain), 5.70 KB, created by
Jonathan Druart
on 2018-10-31 15:02:07 UTC
(
hide
)
Description:
Bug 15395: Add unit tests for extract_messages_from_templates
Filename:
MIME Type:
Creator:
Jonathan Druart
Created:
2018-10-31 15:02:07 UTC
Size:
5.70 KB
patch
obsolete
>From d5c9fdf86da7dafe8bb69e87bf0944ba2e0197aa Mon Sep 17 00:00:00 2001 >From: Julian Maurice <julian.maurice@biblibre.com> >Date: Thu, 25 Oct 2018 13:44:43 +0200 >Subject: [PATCH] Bug 15395: Add unit tests for extract_messages_from_templates > >Because LangInstaller use FindBin to define some paths, the test >produces warnings about a missing directory. I'm not sure how we can >avoid that. > >Test plan: > 1. prove t/LangInstaller.t > >Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org> >--- > misc/translator/LangInstaller.pm | 7 ++- > t/LangInstaller.t | 108 ++++++++++++++++++++++++++++++++++++ > t/LangInstaller/templates/simple.tt | 34 ++++++++++++ > 3 files changed, 148 insertions(+), 1 deletion(-) > create mode 100644 t/LangInstaller.t > create mode 100644 t/LangInstaller/templates/simple.tt > >diff --git a/misc/translator/LangInstaller.pm b/misc/translator/LangInstaller.pm >index e247ab4e23..deff3abf2d 100644 >--- a/misc/translator/LangInstaller.pm >+++ b/misc/translator/LangInstaller.pm >@@ -579,7 +579,12 @@ sub extract_messages_from_templates { > my $keyword = $node->content; > $keyword =~ s/^'t(.*)'$/__$1/; > >- say $fh "$keyword(" . join(', ', @args) . ");"; >+ # Only keep required args to have a clean output >+ my @required_args = shift @args; >+ push @required_args, shift @args if $keyword =~ /n/; >+ push @required_args, shift @args if $keyword =~ /p/; >+ >+ say $fh "$keyword(" . join(', ', @required_args) . ");"; > } > > } >diff --git a/t/LangInstaller.t b/t/LangInstaller.t >new file mode 100644 >index 0000000000..4075791c1a >--- /dev/null >+++ b/t/LangInstaller.t >@@ -0,0 +1,108 @@ >+use Modern::Perl; >+ >+use FindBin '$Bin'; >+use lib "$Bin/../misc/translator"; >+ >+use Test::More tests => 39; >+use File::Temp qw(tempdir); >+use File::Slurp; >+use Locale::PO; >+ >+use t::lib::Mocks; >+ >+use_ok('LangInstaller'); >+ >+my $installer = LangInstaller->new(); >+ >+my $tempdir = tempdir(CLEANUP => 0); >+t::lib::Mocks::mock_config('intranetdir', "$Bin/LangInstaller/templates"); >+my @files = ('simple.tt'); >+$installer->extract_messages_from_templates($tempdir, @files); >+ >+ok(-e "$tempdir/simple.tt", 'it has created a temporary file simple.tt'); >+SKIP: { >+ skip "simple.tt does not exist", 37 unless -e "$tempdir/simple.tt"; >+ >+ my $output = read_file("$tempdir/simple.tt"); >+ my $expected_output = <<'EOF'; >+__('hello'); >+__x('hello {name}'); >+__n('item', 'items'); >+__nx('{count} item', '{count} items'); >+__p('context', 'hello'); >+__px('context', 'hello {name}'); >+__np('context', 'item', 'items'); >+__npx('context', '{count} item', '{count} items'); >+__npx('context', '{count} item', '{count} items'); >+__x('status is {status}'); >+__('active'); >+__('inactive'); >+__('Inside block'); >+EOF >+ >+ is($output, $expected_output, "Output of extract_messages_from_templates is as expected"); >+ >+ my $xgettext_cmd = "xgettext -L Perl --from-code=UTF-8 " >+ . "--package-name=Koha --package-version='' " >+ . "-k -k__ -k__x -k__n:1,2 -k__nx:1,2 -k__xn:1,2 -k__p:1c,2 " >+ . "-k__px:1c,2 -k__np:1c,2,3 -k__npx:1c,2,3 " >+ . "-o $tempdir/Koha.pot -D $tempdir simple.tt"; >+ >+ system($xgettext_cmd); >+ my $pot = Locale::PO->load_file_asarray("$tempdir/Koha.pot"); >+ >+ my @expected = ( >+ { >+ msgid => '"hello"', >+ }, >+ { >+ msgid => '"hello {name}"', >+ }, >+ { >+ msgid => '"item"', >+ msgid_plural => '"items"', >+ }, >+ { >+ msgid => '"{count} item"', >+ msgid_plural => '"{count} items"', >+ }, >+ { >+ msgid => '"hello"', >+ msgctxt => '"context"', >+ }, >+ { >+ msgid => '"hello {name}"', >+ msgctxt => '"context"', >+ }, >+ { >+ msgid => '"item"', >+ msgid_plural => '"items"', >+ msgctxt => '"context"', >+ }, >+ { >+ msgid => '"{count} item"', >+ msgid_plural => '"{count} items"', >+ msgctxt => '"context"', >+ }, >+ { >+ msgid => '"status is {status}"', >+ }, >+ { >+ msgid => '"active"', >+ }, >+ { >+ msgid => '"inactive"', >+ }, >+ { >+ msgid => '"Inside block"', >+ }, >+ ); >+ >+ for (my $i = 0; $i < @expected; $i++) { >+ for my $key (qw(msgid msgid_plural msgctxt)) { >+ my $expected = $expected[$i]->{$key}; >+ my $expected_str = defined $expected ? $expected : 'not defined'; >+ is($pot->[$i + 1]->$key, $expected, "$i: $key is $expected_str"); >+ } >+ } >+} >diff --git a/t/LangInstaller/templates/simple.tt b/t/LangInstaller/templates/simple.tt >new file mode 100644 >index 0000000000..5d8fb1084b >--- /dev/null >+++ b/t/LangInstaller/templates/simple.tt >@@ -0,0 +1,34 @@ >+[% t('hello') %] >+[% tx('hello {name}', { name = 'Bob' }) %] >+[% tn('item', 'items', count) %] >+[% tnx('{count} item', '{count} items', count, { count = count }) %] >+[% tp('context', 'hello') %] >+[% tpx('context', 'hello {name}', { name = 'Bob' }) %] >+[% tnp('context', 'item', 'items', count) %] >+[% tnpx('context', '{count} item', '{count} items', count, { count = count }) %] >+ >+[% # it also works on multiple lines >+ tnpx ( >+ 'context', >+ '{count} item', >+ '{count} items', >+ count, >+ { >+ count = count, >+ } >+ ) >+%] >+ >+[% # and t* calls can be nested >+ tx('status is {status}', { >+ status = active ? t('active') : t('inactive') >+ }) >+%] >+ >+[%# but a TT comment won't get picked >+ t('not translatable') >+%] >+ >+[% BLOCK %] >+ [% t('Inside block') %] >+[% END %] >-- >2.11.0
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 15395
:
45797
|
45798
|
45799
|
46337
|
50023
|
50024
|
50025
|
50026
|
61873
|
61874
|
61875
|
61876
|
61934
|
61935
|
61977
|
62008
|
62041
|
62326
|
62327
|
62328
|
62329
|
62330
|
62331
|
62332
|
62333
|
62335
|
62709
|
65894
|
70974
|
70975
|
70976
|
71940
|
71941
|
71942
|
71943
|
71944
|
71945
|
71946
|
71947
|
71948
|
71949
|
71950
|
71951
|
71952
|
73901
|
73902
|
73903
|
73904
|
73905
|
73906
|
73907
|
73908
|
73909
|
73910
|
73911
|
73912
|
73913
|
76328
|
76329
|
76330
|
77475
|
77476
|
81152
|
81153
|
81154
|
81155
|
81156
|
81157
|
81158
|
81159
|
81167
|
81734
|
81735
|
81736
| 81737 |
81738
|
81739
|
81741
|
81742
|
81743