Main Koha release repository https://koha-community.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

187 lines
4.3 KiB

  1. package Koha::Template::Plugin::I18N;
  2. # Copyright BibLibre 2015
  3. #
  4. # This file is part of Koha.
  5. #
  6. # Koha is free software; you can redistribute it and/or modify it
  7. # under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # Koha is distributed in the hope that it will be useful, but
  12. # WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with Koha; if not, see <http://www.gnu.org/licenses>.
  18. use Modern::Perl;
  19. use base qw( Template::Plugin );
  20. use C4::Context;
  21. use Koha::I18N;
  22. =head1 NAME
  23. Koha::Template::Plugin::I18N - Translate strings in templates
  24. =head1 SYNOPSIS
  25. [% PROCESS 'i18n.inc' %]
  26. . . .
  27. [% I18N.t("Hello!") %]
  28. [% I18N.tx("Hello {name}", { name = name }) %]
  29. [% I18N.tn("Hello friend", "Hello friends", count) %]
  30. [% I18N.tnx("Hello my {count} friend", "Hello my {count} friends", count, { count = count }) %]
  31. [% I18N.tp('verb', 'Item') # to order %]
  32. [% I18N.tnp('bibliographic material', "item", "items", count) %]
  33. [% I18N.tnpx('bibliographic material', "{count} item", "{count} items", count, { count = count }) %]
  34. Do not use this plugin directly. Add the following directive
  35. [% PROCESS 'i18n.inc' %]
  36. and use the macros defined.
  37. See: https://wiki.koha-community.org/wiki/Internationalization,_plural_forms,_context,_and_more_(RFC)
  38. for further context.
  39. =head1 METHODS
  40. =head2 t
  41. [% I18N.t("hello") %]
  42. Translate - The simplest type of translatable string where
  43. there are no variables and not pluralisations to consider.
  44. =cut
  45. sub t {
  46. my ($self, $msgid) = @_;
  47. return __($msgid);
  48. }
  49. =head2 tx
  50. [% I18N.tx("hello {name}", { name = name }) %]
  51. Translate with variable - A translatable string that
  52. includes a variable
  53. =cut
  54. sub tx {
  55. my ($self, $msgid, $vars) = @_;
  56. return __x($msgid, %$vars);
  57. }
  58. =head2 tn
  59. [% I18N.tn("item", "items", count) %]
  60. Translate with plural - A translatable string that needs
  61. singular and plural forms
  62. =cut
  63. sub tn {
  64. my ($self, $msgid, $msgid_plural, $count) = @_;
  65. return __n($msgid, $msgid_plural, $count);
  66. }
  67. =head2 tnx
  68. [% I18N.tnx("{count} item", "{count} items", count, { count = count }) %]
  69. Translate with plural and variable - A translatable string
  70. that needs singular and plural forms and includes a variable
  71. =cut
  72. sub tnx {
  73. my ($self, $msgid, $msgid_plural, $count, $vars) = @_;
  74. return __nx($msgid, $msgid_plural, $count, %$vars);
  75. }
  76. =head2 txn
  77. Alias of tnx
  78. =cut
  79. sub txn {
  80. my ($self, $msgid, $msgid_plural, $count, $vars) = @_;
  81. return __xn($msgid, $msgid_plural, $count, %$vars);
  82. }
  83. =head2 tp
  84. [% I18N.tp("context", "hello") %]
  85. Translate with context - A translatable string where a
  86. context hint would be helpful to translators.
  87. An example would be where in english a single word may be
  88. be used as both a verb and a noun. You may want to add a
  89. note to distinguish this particular use case so translators
  90. can understand the context correctly.
  91. =cut
  92. sub tp {
  93. my ($self, $msgctxt, $msgid) = @_;
  94. return __p($msgctxt, $msgid);
  95. }
  96. =head2 tpx
  97. [% I18N.tpx("context", "hello {name}", { name = name }) %]
  98. Translate with context and variable - A translatable string
  99. that needs both a contextual hint and includes a variable.
  100. =cut
  101. sub tpx {
  102. my ($self, $msgctxt, $msgid, $vars) = @_;
  103. return __px($msgctxt, $msgid, %$vars);
  104. }
  105. =head2 tnp
  106. [% I18N.tnp("context", "item", "items", count) %]
  107. Translate with context and plural - A translatable string
  108. that needs both a contextual hints and singular and plural
  109. forms.
  110. =cut
  111. sub tnp {
  112. my ($self, $msgctxt, $msgid, $msgid_plural, $count) = @_;
  113. return __np($msgctxt, $msgid, $msgid_plural, $count);
  114. }
  115. =head2 tnpx
  116. [% I18N.tnpx("context", "{count} item", "{count} items", count, { count = count }) %]
  117. Translate with context, plural and variables - A translatable
  118. string that needs contextual hints, singular and plural forms
  119. and also includes variables.
  120. =cut
  121. sub tnpx {
  122. my ($self, $msgctxt, $msgid, $msgid_plural, $count, $vars) = @_;
  123. return __npx($msgctxt, $msgid, $msgid_plural, $count, %$vars);
  124. }
  125. 1;