PHPackages                             gossi/propel-l10n-behavior - 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. gossi/propel-l10n-behavior

ActivePropel-behavior[Localization &amp; i18n](/categories/localization)

gossi/propel-l10n-behavior
==========================

v0.1(10y ago)02.7k21MITPHPPHP &gt;=5.4

Since Jan 12Pushed 10y ago1 watchersCompare

[ Source](https://github.com/gossi/propel-l10n-behavior)[ Packagist](https://packagist.org/packages/gossi/propel-l10n-behavior)[ RSS](/packages/gossi-propel-l10n-behavior/feed)WikiDiscussions master Synced today

READMEChangelog (1)Dependencies (3)Versions (2)Used By (1)

Propel l10n Behavior
====================

[](#propel-l10n-behavior)

[![Build Status](https://camo.githubusercontent.com/0aac4d12dda71891ce36c877220ff07c6ad935e9664afd9a0c5e229b2ab9129f/68747470733a2f2f7472617669732d63692e6f72672f676f7373692f70726f70656c2d6c31306e2d6265686176696f722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/gossi/propel-l10n-behavior)

The propel-l10n-behavior is an extension to propels own i18n behavior. Basically, it puts an API in front of the i18n behavior and let's you use propels default API but with localized content. You provide the localization you want to use once (globally) and you are ready to go.

Installation
------------

[](#installation)

Install via composer:

```
{
    "require": {
        "gossi/propel-l10n-behavior": "~0"
    }
}
```

Locale and Dependencies
-----------------------

[](#locale-and-dependencies)

When working with locales, you should know about the locales and dependencies you can define for the propel-l10n-behavior. There are three mechanisms:

- Locale (That's the default locale, when retrieving localized content from a propel object)
- Dependencies (This is a dependency chain, when a field is not available in the default locale)
- Fallback (If nothing is found, try the fallback locale)

### Set Default locale

[](#set-default-locale)

Default locale is set to `en` but of course you can change this:

```
PropelL10n::setLocale('de');

echo PropelL10n::getLocale(); // de
```

The default locale can be overwritten per object, e.g. you have a `Book` model, this is how it works:

```
$book = new Book();
$book->setLocale('ja');
```

Now, the locale for book is japanese, while for all others it stays german (as seen in the example above). You can reset this object-related overwrite by setting the locale to `null`:

```
$book->setLocale(null);
```

The object default locale can be overwritten per call, e.g.

```
PropelL10n::setLocale('it');
$book = BookQuery::create() // locale: it
	->setLocale('ja') // locale: ja
	->findOneByTitle('Herr der Ringe', null, 'de') // locale: de
;
$book->setLocale('ja');
$book->setTitle('Lord of the Rings', 'en');
```

### Set Fallback locale

[](#set-fallback-locale)

Fallback locale also is defaulted to `en`, yet you can change that:

```
PropelL10n::setFallback('de');

echo PropelL10n::getFallback(); // de
```

### Working with Dependencies

[](#working-with-dependencies)

There is some API to work with dependencies:

```
// get and set one dependency
PropelL10n::addDependency('de-CH', 'de');
echo PropelL10n::getDependency('de-CH'); // de

// get and set all dependencies
PropelL10n::setDependencies([
	'de'	=> 'en',
	'de-CH' => 'de',
	'de-AT' => 'de'
]);
print_r(PropelL10n::getDependencies()); // outputs the array from above

// check if dependencies exist
PropelL10n::hasDependency('de'); // true
PropelL10n::hasDepdedency('ja'); // false

// count depdencies for a given locale
PropelL10n::setDependencies(['de' => 'en', 'de-CH' => 'de']);
PropelL10n::countDependencies('de-CH'); // 2
```

### Retrieving a localized Field

[](#retrieving-a-localized-field)

Whenever you retrieve a localized field, the behavior will use the following algorithm to find the contents for the field in the most recent locale:

1. Set default locale as locale.
2. Try to get the field in the set locale
3. If empty, check if the locale has a dependency and if yes a. if the primary language of the dependency and the current locale are different, work down the language-tag-chain of the current locale b. set dependency as new locale, continue with step 2
4. If no dependecy is set for that locale, work down the language-tag-chain
5. If primary language is empty, use fallback as new locale and continue with step 2
6. Last step: giving up, return null

Language-tag-chain:

Given the following language-tag: `de-DE-1996` it consists of three subtags. When working down the language-tag-chain it means, the last subtag is dropped and it will be tried to getting the content of a field for the remaining language-tag until there is only the primary language left.

Usage
-----

[](#usage)

### In your schema.xml

[](#in-your-schemaxml)

The usage in your schema.xml is very similar to the [i18n](http://propelorm.org/documentation/behaviors/i18n.html) behavior.

```

```

The parameters are equal to the [i18n parameters](http://propelorm.org/documentation/behaviors/i18n.html#parameters), except `default_locale` doesn't exist.

### Using the API

[](#using-the-api)

There is three things you need to do once for your app and you are ready to go and use propel as if they weren't any l10n/i18n behaviors used at all.

1. Set the default locale
2. Set a fallback locale
3. Set your dependencies

Example:

```
PropelL10n::setLocale('de'); // this is mostly the language a user decided to use
PropelL10n::setFallback('en'); // that's the default language of your app
PropelL10n::setDependencies([ // that's the locales you have available
	'de'	=> 'en'
	'de-CH' => 'de',
	'de-AT' => 'de',
	'ja' => 'en'
]);
```

And you are ready to go.

#### Retrieving from Objects

[](#retrieving-from-objects)

```
$book = new Book();
$book->setTitle('Lord of the Rings');
$book->setLocale('de');
$book->setTitle('Herr der Ringe');
$book->setLocale(null);

echo $book->getTitle(); // Lord of the Rings - using the default locale (`en`)
echo $book->getTitle('de-DE'); // Herr der Ringe
$book->setLocale('de-DE');
echo $book->getTitle(); // Herr der Ringe
echo $book->getTitle('ja-JP'); // Lord of the Rings - using fallback locale (`en`)
```

#### Querying the Database

[](#querying-the-database)

You can use your well known methods to query the database:

```
// default locale: de

// contains all books with a german title starting with 'Harry Potter'
$books = BookQuery::create()
	->filterByTitle('Harry Potter%')
	->find();
$books = ...;

// the book lord of the rings as searched with an english title name
$book = BookQuery::create()
	->findOneByTitle('Lord of the Rings', null, 'en');
$book = ...;

// all harry potter books searched with the japanese title
$books = BookQuery::create()
	->setLocale('ja') 				// overwrites query default locale
	->findByTitle('Harī Pottā%');
$books = ...;

// find lord of the rings with the japanese title, overwrite locale with the filter method
$book = BookQuery::create()
	->findOneByTitle('Yubiwa Monogatari', null, 'ja');
$book = ...;
```

Best Practices
--------------

[](#best-practices)

**Use the shortest locale possible!** Only use a longer language-tag, when it is necessary to be more specific. If there is no need, just go with `de` instead of `de-DE` (which is kind of redundant anyway). However, go with `de` and `de-CH` as there might be content available which is different for people in germany or switzerland (e.g. a contact address, one for germany the other for switzerland).

Performance
-----------

[](#performance)

I'm pretty sure this is a performance nightmare. Only propel API methods are used, means no manual queries so far. Performance optimization can begin after propel will merge the `data-mapper` branch into `master`. Suggestions are welcome, please post the to the issue tracker.

References
----------

[](#references)

There's a lot material about localization, language-tags. Sometimes it is about finding the right subtag, which can be complicated enough. Here are some good references:

- [Language Tags](http://www.w3.org/International/articles/language-tags/)
- [Choosing a Language Tag](http://www.w3.org/International/questions/qa-choosing-language-tags)
- [IANA Language Subtag Registry](https://www.iana.org/assignments/language-subtag-registry/language-subtag-registry)
- [Language subtag lookup tool](https://r12a.github.io/apps/subtags/)

Related Specifications:

- [RCP 5646 - Tags for Identifying Languages](https://tools.ietf.org/html/rfc5646)
- [BCP 47 - Tags for Identifying Languages](https://www.rfc-editor.org/bcp/bcp47.txt)

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity18

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity48

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

3772d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/0b1ca3beb32834f0f6a4b66d7a695ecc01397aa92e9a97bd0ba5dd8aa14e0c6e?d=identicon)[gossi](/maintainers/gossi)

---

Top Contributors

[![gossi](https://avatars.githubusercontent.com/u/283700?v=4)](https://github.com/gossi "gossi (17 commits)")

---

Tags

l10nBehaviorpropel

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/gossi-propel-l10n-behavior/health.svg)

```
[![Health](https://phpackages.com/badges/gossi-propel-l10n-behavior/health.svg)](https://phpackages.com/packages/gossi-propel-l10n-behavior)
```

###  Alternatives

[moment/moment

Parse, validate, manipulate, and display dates in JavaScript.

48.1k2.1M37](/packages/moment-moment)[symfony/intl

Provides access to the localization data of the ICU library

2.6k199.8M1.1k](/packages/symfony-intl)[timrwood/moment

Parse, validate, manipulate, and display dates in JavaScript.

48.0k17.2k](/packages/timrwood-moment)[gettext/languages

gettext languages with plural rules

7530.3M11](/packages/gettext-languages)[aplus/language

Aplus Framework Language Library

2351.7M15](/packages/aplus-language)[punic/punic

PHP-Unicode CLDR

1542.9M29](/packages/punic-punic)

PHPackages © 2026

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