PHPackages                             vbridier/zf2-smarty-gettext - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Localization &amp; i18n](/categories/localization)
4. /
5. vbridier/zf2-smarty-gettext

ActiveLibrary[Localization &amp; i18n](/categories/localization)

vbridier/zf2-smarty-gettext
===========================

Gettext plugin enabling internationalization in Smarty Package files

v1.0.0(8y ago)024PHPPHP &gt;=5.2.1

Since Jul 17Pushed 8y ago1 watchersCompare

[ Source](https://github.com/vbridier/zf2-smarty-gettext)[ Packagist](https://packagist.org/packages/vbridier/zf2-smarty-gettext)[ RSS](/packages/vbridier-zf2-smarty-gettext/feed)WikiDiscussions master Synced yesterday

READMEChangelogDependenciesVersions (2)Used By (0)

**Gettext support for Smarty2/Smarty3 with ZF2**
================================================

[](#gettext-support-for-smarty2smarty3-with-zf2)

[zf2-smarty-gettext](https://github.com/vbridier/zf2-smarty-gettext) provides gettext (i18n) support for [Smarty](http://www.smarty.net/) with Zend Framework 2, the popular PHP templating engine, to implement an NLS (Native Language Support) API which can be used to internationalize and translate your PHP applications.

This README assumes that you already know what is [gettext](https://www.gnu.org/software/gettext/) and how to use it with PHP.

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

2.
3.

This package has two parts:

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

**Installation**
----------------

[](#installation)

With Composer:

- Add the `"vbridier/zf2-smarty-gettext": "1.0.0"` into the `require` section of your `composer.json`:

```
composer require vbridier/zf2-smarty-gettext=1.0.0

```

- Run `composer install`.

**function.locale.php**
-----------------------

[](#functionlocalephp)

Lets you set the locales path in master smarty template.

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.

**block.t.php**
---------------

[](#blocktphp)

The Smarty plugin

**Usage**

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}`.

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.

For example, `{t name="sagi"}my name is %1{/t}` will replace `%1` with sagi.

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

Example for using multiple parameters:

```
{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()](http://php.net/manual/en/function.sprintf.php), mainly because its syntax is simpler for the translators (especially when wanting to change the parameter order).

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.

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

```
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:

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

Example:

```
{t escape=no url="http://www.php.net/" name="PHP website"}
%2
{/t}

```

Using variables

Sometimes you need translated block passed as variable. This can be achieved with `capture` block:

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

```

Plural support
--------------

[](#plural-support)

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

To provide a plural form:

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.

Example:

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

```

Modifier support
----------------

[](#modifier-support)

A Smarty modifier support is not provided by this package.

I believe variables should be translated in the application level and provided after translation to the template.

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

**tsmarty2c.php - the command line utility**
--------------------------------------------

[](#tsmarty2cphp---the-command-line-utility)

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

Usage:

```
./tsmarty2c.php -o template.pot

```

If a parameter is a directory, the template files within will be parsed, recursively.

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

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:

```
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:

```
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](https://github.com/eventum/eventum/blob/master/localization/Makefile) project.

**Authors**
-----------

[](#authors)

- This ZF2 Plugin: Vincent BRIDIER
- Original Author: Sagi Bashari ,
- Current maintainer: Elan Ruusamäe

###  Health Score

25

—

LowBetter than 36% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

3270d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/16559661?v=4)[Vincent BRIDIER](/maintainers/vbridier)[@vbridier](https://github.com/vbridier)

---

Top Contributors

[![vbridier](https://avatars.githubusercontent.com/u/16559661?v=4)](https://github.com/vbridier "vbridier (3 commits)")

### Embed Badge

![Health badge](/badges/vbridier-zf2-smarty-gettext/health.svg)

```
[![Health](https://phpackages.com/badges/vbridier-zf2-smarty-gettext/health.svg)](https://phpackages.com/packages/vbridier-zf2-smarty-gettext)
```

###  Alternatives

[smmoosavi/php-gettext

Wrapper for php-gettext by danilo segan. This library provides PHP functions to read MO files even when gettext is not compiled in or when appropriate locale is not present on the system.

1926.6k1](/packages/smmoosavi-php-gettext)[laradevs/spanish

labels translated to spanish

166.7k](/packages/laradevs-spanish)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
