PHPackages                             fenghuilee/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. fenghuilee/smarty-gettext

ActiveLibrary

fenghuilee/smarty-gettext
=========================

Gettext plugin enabling internationalization in Smarty Package files

1.2.0(10y ago)028LGPL-2.1+PHPPHP &gt;=5.2.1

Since Oct 31Pushed 10y ago1 watchersCompare

[ Source](https://github.com/fenghuilee/smarty-gettext)[ Packagist](https://packagist.org/packages/fenghuilee/smarty-gettext)[ Docs](https://github.com/smarty-gettext/smarty-gettext)[ RSS](/packages/fenghuilee-smarty-gettext/feed)WikiDiscussions master Synced today

READMEChangelogDependenciesVersions (7)Used By (0)

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

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

[![License](https://camo.githubusercontent.com/c77a7fc047f675e307fdb97fc163493a1bc3abde3f0056693edca72dfcb92e57/68747470733a2f2f706f7365722e707567782e6f72672f736d617274792d676574746578742f736d617274792d676574746578742f6c6963656e73652e706e67)](https://packagist.org/packages/smarty-gettext/smarty-gettext)[![Build Status](https://camo.githubusercontent.com/7566c907fa12d71f6db23d2a947c31420cc2d308427cd98cdca0d912808756fc/68747470733a2f2f7472617669732d63692e6f72672f736d617274792d676574746578742f736d617274792d676574746578742e706e673f6272616e63683d6d6173746572)](http://travis-ci.org/smarty-gettext/smarty-gettext)[![Scrutinizer Quality Score](https://camo.githubusercontent.com/4201925efd6886083dfc562fae70f9ca15c92a9da94c5e53925b6d0c4160f43b/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f736d617274792d676574746578742f736d617274792d676574746578742f6261646765732f7175616c6974792d73636f72652e706e673f733d62643761326336313939313336626466303536636562303935643933363032636332386435643733)](https://scrutinizer-ci.com/g/smarty-gettext/smarty-gettext/)[![Latest Stable Version](https://camo.githubusercontent.com/7de6793f849bfb988eed55f9b92af9a03df939874f2a9a1f04e2a9ad9ac365d3/68747470733a2f2f706f7365722e707567782e6f72672f736d617274792d676574746578742f736d617274792d676574746578742f762f737461626c652e706e67)](https://packagist.org/packages/smarty-gettext/smarty-gettext)[![Total Downloads](https://camo.githubusercontent.com/b3591f69a95a8ca78cd8d12158aceca98ad24ffc67a334b15844635c1411a565/68747470733a2f2f706f7365722e707567782e6f72672f736d617274792d676574746578742f736d617274792d676574746578742f646f776e6c6f6164732e706e67)](https://packagist.org/packages/smarty-gettext/smarty-gettext)[![Latest Unstable Version](https://camo.githubusercontent.com/eef02fc52f4f7c0332a713f1a3fda9665e45644ee4932d466bb9eb1d31ed1c66/68747470733a2f2f706f7365722e707567782e6f72672f736d617274792d676574746578742f736d617274792d676574746578742f762f756e737461626c652e706e67)](https://packagist.org/packages/smarty-gettext/smarty-gettext)

[smarty-gettext](https://github.com/smarty-gettext/smarty-gettext) provides gettext (i18n) support for [Smarty](http://www.smarty.net/), 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.

If you encounter problems when using the native [gettext](http://php.net/manual/en/book.gettext.php) extension, you may want to try the [php-gettext](https://launchpad.net/php-gettext/) module, which emulates the behavior of the C extension, but is written in pure PHP.

This package has two parts:

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**
----------------

[](#installation)

With Composer:

- Add the `"fenghuilee/smarty-gettext": "dev-master"` into the `require` section of your `composer.json`:

```
composer require fenghuilee/smarty-gettext=dev-master

```

- Run `composer install`.

Manually:

- Simply copy `block.t.php` and `function.locale.php` to your Smarty plugins directory.

**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)

- Original Author: Sagi Bashari ,
- Current maintainer: Elan Ruusamäe

**Copyright**
-------------

[](#copyright)

Copyright (c) 2004-2005 Sagi Bashari
Copyright (c) 2010-2015 Elan Ruusamäe

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.

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.

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

###  Health Score

27

—

LowBetter than 51% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity62

Established project with proven stability

 Bus Factor1

Top contributor holds 92.9% 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

Every ~178 days

Total

4

Last Release

3970d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/7ca8ad30dd0dc9b06d71acd73951c038d88a36383e6f7be0d49ae68ee876e62f?d=identicon)[fenghuilee](/maintainers/fenghuilee)

---

Top Contributors

[![glensc](https://avatars.githubusercontent.com/u/199095?v=4)](https://github.com/glensc "glensc (91 commits)")[![fenghuilee](https://avatars.githubusercontent.com/u/2135391?v=4)](https://github.com/fenghuilee "fenghuilee (7 commits)")

### Embed Badge

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

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

PHPackages © 2026

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