2017 © Pedro Peláez
 

library smarty-gettext

Gettext plugin enabling internationalization in Smarty Package files

image

smarty-gettext/smarty-gettext

Gettext plugin enabling internationalization in Smarty Package files

  • Friday, May 12, 2017
  • by glen
  • Repository
  • 8 Watchers
  • 54 Stars
  • 47,791 Installations
  • PHP
  • 6 Dependents
  • 0 Suggesters
  • 16 Forks
  • 0 Open issues
  • 11 Versions
  • 10 % Grown

The README.md

Gettext support for Smarty2/Smarty3/Smarty4

License Build Status Scrutinizer Quality Score Latest Stable Version Total Downloads Latest Unstable Version, (*1)

smarty-gettext provides gettext (i18n) support for Smarty, the popular PHP templating engine, to implement an NLS (Native Language Support) API which can be used to internationalize and translate your PHP applications., (*2)

This README assumes that you already know what is gettext and how to use it with PHP., (*3)

If you don't, please visit the following websites before trying to use this package:, (*4)

  1. http://www.php.net/gettext
  2. http://www.onlamp.com/pub/a/php/2002/06/13/php.html

If you encounter problems when using the native gettext extension, you may want to try the php-gettext module, which emulates the behavior of the C extension, but is written in pure PHP., (*5)

This package has two parts:, (*6)

  1. block.t.php, function.locale.php - The Smarty plugins.
  2. tsmarty2c.php - A command line utility that rips gettext strings from smarty source files and converts them to .pot (PO-Template).

Installation

With Composer:, (*7)

  • Add the "smarty-gettext/smarty-gettext" into the require section of your composer.json:
composer require smarty-gettext/smarty-gettext

Manually:, (*8)

  • Copy block.t.php and function.locale.php to your Smarty plugins directory.

function.locale.php

Lets you set the locales path in master smarty template., (*9)

In order to use it put {locale path="PATH_TO_TRANSLATIONS_RELATIVE_TO_TEMPLATES_DIRECTORY" domain="YOUR_TRANSLATIONS_DOMAIN"} somewhere in the top of your master template., (*10)

block.t.php

The Smarty plugin, (*11)

Usage, (*12)

The content of the block function is the string that you want to translate. For example, for translating Hello World, use: {t}Hello World{/t}., (*13)

If you have dynamic parameters that should be set inside the string, pass them to the block function, and they will be replaced with %n, where n is 1 for the 1st parameter and so on., (*14)

For example, {t name="sagi"}my name is %1{/t} will replace %1 with sagi., (*15)

The parameter name is ignored, unless it is one of the reserved names (see below). Only the parameters order matters., (*16)

Example for using multiple parameters:, (*17)

{t 1='one' 2='two' 3='three'}The 1st parameter is %1, the 2nd is %2 and the 3rd %3.{/t}

NOTE: I decided to use numeric arguments instead of sprintf(), mainly because its syntax is simpler for the translators (especially when wanting to change the parameter order)., (*18)

You can also use this method in your PHP code, by using the smarty_gettext_strarg() function. It is only loaded after block.t.php is included, so you probably want to copy it elsewhere., (*19)

I usually name the global version of this function strarg(), and use it like this:, (*20)

echo strarg(_('hi %1'), $name [,..]);

By default, all the translated strings will be automatically HTML escaped. You may control this by setting the escape parameter. Possible values:, (*21)

  • html for HTML escaping, this is the default.
  • js for javascript escaping.
  • url for url escaping.
  • no/off/0 - disables escaping.

Example:, (*22)

{t escape=no url="http://www.php.net/" name="PHP website"}
<a href="%1">%2</a>
{/t}

Using variables, (*23)

Sometimes you need translated block passed as variable. This can be achieved with capture block:, (*24)

{capture assign="extra_title"}{t}Weekly report{/t}{/capture}
{include file="header.tpl.html" extra_title=$extra_title}

Plural support

The package also provides support for plural forms (see ngettext)., (*25)

To provide a plural form:, (*26)

  1. Set a parameter named plural with the plural version of the string.
  2. Set a parameter named count with the variable count.

Plural and count are special parameters, and therefore, are not available as numeric arguments. If you wish to use the count value inside the string, you will have to set it again, as a numeric argument., (*27)

Example:, (*28)

{t count=$files|@count 1=$files|@count plural="%1 files"}One file{/t}

Modifier support

A Smarty modifier support is not provided by this package., (*29)

I believe variables should be translated in the application level and provided after translation to the template., (*30)

If you need it anyway, it is easy to create such modifier, by registering the PHP gettext command as one., (*31)

tsmarty2c.php - the command line utility

NOTE: The tool is no longer supported here, please see new project for the tool: https://github.com/smarty-gettext/tsmarty2c, (*32)

This utility will scan templates for {t}...{/t} placeholders for translation strings and output a .pot file (.po template)., (*33)

Usage:, (*34)

./tsmarty2c.php -o template.pot <filename or directory> <file2> <...>

If a parameter is a directory, the template files within will be parsed, recursively., (*35)

In output special PO tags are added that inform about location of extracted translation. Most of the PO edit tools can respect that information., (*36)

If you wish to scan also .php or .phtml files for native gettext calls, you may wish to combine result of tsmarty2c and xgettext calls:, (*37)

tsmarty2c -o smarty.pot ...
xgettext --add-comments=TRANSLATORS: --keyword=gettext --keyword=_  --output=code.pot ...
msgcat -o template.pot code.pot smarty.pot
rm -f code.pot smarty.pot

By default tsmarty2c scans for .tpl files, if you wish to use other files, you can use xargs in unix:, (*38)

find templates -name '*.tpl.html' -o -name '*.tpl.text' -o -name '*.tpl.js' -o -name '*.tpl.xml' | xargs tsmarty2c.php -o smarty.pot

See how it's done in Eventum project., (*39)

Authors

Copyright (c) 2004-2005 Sagi Bashari
Copyright (c) 2010-2023 Elan Ruusamäe, (*40)

This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version., (*41)

This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details., (*42)

You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA, (*43)

The Versions

12/05 2017

dev-master

9999999-dev https://github.com/smarty-gettext/smarty-gettext

Gettext plugin enabling internationalization in Smarty Package files

  Sources   Download

LGPL-2.1+

The Requires

  • ext-gettext *
  • ext-pcre *
  • php ~5.3|~7.0

 

The Development Requires

by Sagi Bashari

12/05 2017

1.5.1

1.5.1.0 https://github.com/smarty-gettext/smarty-gettext

Gettext plugin enabling internationalization in Smarty Package files

  Sources   Download

LGPL-2.1+

The Requires

  • ext-gettext *
  • ext-pcre *
  • php ~5.3|~7.0

 

The Development Requires

by Sagi Bashari

10/05 2017

dev-tsmarty2c-rewrite2

dev-tsmarty2c-rewrite2 https://github.com/smarty-gettext/smarty-gettext

Gettext plugin enabling internationalization in Smarty Package files

  Sources   Download

LGPL-2.1+

The Requires

  • ext-gettext *
  • ext-pcre *
  • php ~5.3|~7.0

 

The Development Requires

by Sagi Bashari

03/11 2016

v1.5.0

1.5.0.0 https://github.com/smarty-gettext/smarty-gettext

Gettext plugin enabling internationalization in Smarty Package files

  Sources   Download

LGPL-2.1+

The Requires

  • ext-gettext *
  • ext-pcre *
  • php ~5.3|~7.0

 

The Development Requires

by Sagi Bashari

13/06 2016

1.4.0

1.4.0.0 https://github.com/smarty-gettext/smarty-gettext

Gettext plugin enabling internationalization in Smarty Package files

  Sources   Download

LGPL-2.1+

The Requires

  • ext-gettext *
  • ext-pcre *
  • php ~5.3|~7.0

 

The Development Requires

by Sagi Bashari

14/11 2015

1.3.0

1.3.0.0 https://github.com/smarty-gettext/smarty-gettext

Gettext plugin enabling internationalization in Smarty Package files

  Sources   Download

LGPL-2.1+

The Requires

  • ext-gettext *
  • ext-pcre *
  • php >=5.2.1

 

The Development Requires

by Sagi Bashari

20/10 2015

dev-travis

dev-travis https://github.com/smarty-gettext/smarty-gettext

Gettext plugin enabling internationalization in Smarty Package files

  Sources   Download

LGPL-2.1+

The Requires

  • ext-gettext *
  • ext-pcre *
  • php >=5.2.1

 

by Sagi Bashari

11/05 2015

1.2.0

1.2.0.0 https://github.com/smarty-gettext/smarty-gettext

Gettext plugin enabling internationalization in Smarty Package files

  Sources   Download

LGPL-2.1+

The Requires

  • ext-gettext *
  • ext-pcre *
  • php >=5.2.1

 

by Sagi Bashari

12/11 2014

1.1.1

1.1.1.0 https://github.com/smarty-gettext/smarty-gettext

Gettext plugin enabling internationalization in Smarty Package files

  Sources   Download

LGPL-2.1+

The Requires

  • ext-gettext *
  • ext-pcre *
  • php >=5.2.1

 

by Sagi Bashari

26/12 2013

1.1.0

1.1.0.0 https://github.com/smarty-gettext/smarty-gettext

Gettext plugin enabling internationalization in Smarty Package files

  Sources   Download

LGPL-2.1+

The Requires

  • ext-gettext *
  • ext-pcre *
  • php >=5.2.1

 

by Sagi Bashari

21/11 2013

1.0.1

1.0.1.0 https://github.com/glensc/smarty-gettext

Gettext plugin enabling internationalization in Smarty Package files

  Sources   Download

LGPL-2.1+

The Requires

  • ext-gettext *
  • ext-pcre *
  • php >=5.2.1

 

by Sagi Bashari