PHPackages                             surcoufx83/php-i18n - 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. surcoufx83/php-i18n

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

surcoufx83/php-i18n
===================

Simple i18n class for PHP

4.1.4(5y ago)0535MITPHPPHP &gt;= 7.1

Since Feb 12Pushed 5y agoCompare

[ Source](https://github.com/surcoufx83/php-i18n)[ Packagist](https://packagist.org/packages/surcoufx83/php-i18n)[ Docs](https://github.com/surcoufx83/php-i18n)[ RSS](/packages/surcoufx83-php-i18n/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (5)Dependencies (2)Versions (9)Used By (0)

PHP i18n
========

[](#php-i18n)

[![Build Status](https://camo.githubusercontent.com/a522ebff3b7970d9872a6f417f7e2467e619200a6597ed9a875645553bc8c3d0/68747470733a2f2f7472617669732d63692e6f72672f737572636f75667838332f7068702d6931386e2e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/surcoufx83/php-i18n)

This is a simple i18n class for PHP forked from [Philipp15b/php-i18n](https://github.com/Philipp15b/php-i18n). Nothing fancy, but fast, because it uses caching and it is easy to use. Try it out!

Some of its features:

- Translation strings in `.ini`/`.properties`, `.json` or `.yaml` format
- Caching
- Simple API: `L::category_stringname`
- Built-in support for [vsprintf](http://php.net/manual/en/function.vsprintf.php) formatting: `L::name($par1)`
- Automatic user language detection
- Simplicity ;)

Requirements
------------

[](#requirements)

- Write permissions in cache directory
- PHP 7.1 and above
- PHP SPL extension (installed by default)

Setup
-----

[](#setup)

There are examples in the `example-ini.php` and the `example-yml.php` files. You just have to follow these steps:

### 1. Load sources

[](#1-load-sources)

You can fetch the sources of this project either by downloading the individual files (`i18n.class.php` is the only requirement) or by running composer:

```
composer require surcoufx83/php-i18n

```

### 2. Create language files

[](#2-create-language-files)

To use this class, you need to create translation files with your translated strings. They can be `.ini`/`.properties`, `.json` or `.yml`/`.yaml` files. This could look like this:

`lang_en.ini` (English)

```
greeting = "Hello World!"

[category]
somethingother = "Something other..."

```

`lang_de.ini` (German)

```
greeting = "Hallo Welt!"

[category]
somethingother = "Etwas anderes..."

```

Save both files in the directory you will set in step 4. The files must be named according to the filePath setting, where '{LANGUAGE}' will be replaced by the user's language, e.g. 'en' or 'de'. Find more examples in the [lang folder](https://github.com/surcoufx83/php-i18n/tree/master/lang) of this repository.

### 3. Include and initialize the class

[](#3-include-and-initialize-the-class)

```

```

### 4. Set some settings if necessary

[](#4-set-some-settings-if-necessary)

The possible settings are:

- Language file path (default: `./lang/lang_{LANGUAGE}.yml`)
- Cache file path (default: `./langcache/`)
- The fallback language, if no one of the user languages is available (default: `en`)
- A 'prefix', the compiled class name (default `L`)
- A forced language, if you want to force a language (default: none)
- The section separator: this is used to seperate the sections in the language class. If you set the separator to `_abc_` you could access your localized strings via `L::category_abc_stringname` if you use categories in your ini. (default: `_`)
- Merge keys from the fallback language into the current language

```

  ...

	$i18n = new i18n();

	$i18n->setCachePath('./tmp/cache');
	$i18n->setFilePath('./langfiles/lang/lang_{LANGUAGE}.ini'); // language file path
	$i18n->setFallbackLang('en');
	$i18n->setPrefix('lang');
	$i18n->setForcedLang('en'); // force english, even if another user language is available
	$i18n->setSectionSeparator('_');
	$i18n->setMergeFallback(false); // make keys available from the fallback language

```

One can use chained methods for setters too:

```

  ...

	$i18n = new i18n();

	$i18n->setCachePath('./tmp/cache')
	     ->setFilePath('./langfiles/lang/lang_{LANGUAGE}.ini')
	     ->setFallbackLang('en')
	     ->setPrefix('lang')
	     ->setForcedLang('en')
	     ->setSectionSeparator('_')
	     ->setMergeFallback(false);

	...

```

#### Shorthand

[](#shorthand)

There is also a shorthand for that: you can set all settings in the constructor.

```
  ...

	$i18n = new i18n('lang/lang_{LANGUAGE}.ini', 'langcache/', 'en');

	...

```

The (all optional) parameters are:

1. the language file path (the ini files)
2. the language cache path
3. fallback language
4. the prefix/compiled class name

### 5. Call the `init()` method to load all files and translations

[](#5-call-the-init-method-to-load-all-files-and-translations)

Call the `init()` file to instruct the class to load the appropriate language file, load the cache file or generate it if it doesn't exist and make the `L` class available so you can access your localizations.

```
	...

	$i18n->init();

	...

```

### 6. Use the localizations

[](#6-use-the-localizations)

To call your localizations, simply use the `L` class and a class constant for the string.

In this example, we use the translation string seen in step 1.

```
  ...

	echo L::greeting;
	// If 'en' is applied: 'Hello World'

	echo L::category_somethingother;
	// If 'en' is applied: 'Something other...'

	echo L::last_modified("today");
	// Could be: 'Last modified: today'

	echo L($string);
	// Outputs a dynamically chosen static property

	echo L($string, $args);
	// Same as L::last_modified("today");

	...

```

As you can see, you can also call the constant as a function. It will be formatted with [vsprintf](http://php.net/manual/en/function.vsprintf.php).

Also, like in the two last examples, a helper function with the same name as the class makes it easier to dynamically access the constants if ever needed.

Thats it!

How the user language detection works
-------------------------------------

[](#how-the-user-language-detection-works)

This class tries to detect the user's language by trying the following sources in this order:

1. Forced language (if set)
2. GET parameter 'lang' (`$_GET['lang']`)
3. SESSION parameter 'lang' (`$_SESSION['lang']`)
4. HTTP\_ACCEPT\_LANGUAGE (can be multiple languages) (`$_SERVER['HTTP_ACCEPT_LANGUAGE']`)
5. Fallback language

php-i18n will remove all characters that are not one of the following: A-Z, a-z or 0-9 to prevent [arbitrary file inclusion](https://en.wikipedia.org/wiki/File_inclusion_vulnerability). After that the class searches for the language files. For example, if you set the GET parameter 'lang' to 'en' without a forced language set, the class would try to find the file `lang/lang_en.ini` (if the setting `langFilePath` was set to default (`lang/lang_{LANGUAGE}.ini`)). If this file doesn't exist, php-i18n will try to find the language file for the language defined in the session variable and so on.

### How to change this implementation

[](#how-to-change-this-implementation)

You can change the user detection by extending the `i18n` class and overriding the `getUserLangs()` method:

```

	require_once 'i18n.class.php';

	class My_i18n extends i18n {

		public function getUserLangs() {
			$userLangs = new array();

			$userLangs[] = $_GET['language'];

			$userLangs[] = $_SESSION['userlanguage'];

			return $userLangs;
		}

	}

	$i18n = new My_i18n();
	// [...]

```

This very basic extension only uses the GET parameter 'language' and the session parameter 'userlanguage'. You see that this method must return an array.

**Note that this example function is insecure**: `getUserLangs()` also has to escape the results or else i18n will [include arbitrary files](https://en.wikipedia.org/wiki/File_inclusion_vulnerability). The default implementation is safe.

Fork it!
--------

[](#fork-it)

Contributions are always welcome.

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity12

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity63

Established project with proven stability

 Bus Factor1

Top contributor holds 63.2% 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 ~281 days

Recently: every ~0 days

Total

8

Last Release

2136d ago

Major Versions

v3.0 → v4.02018-03-21

PHP version history (2 changes)v4.0PHP &gt;= 5.3

4.1.0PHP &gt;= 7.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/4c2b712a048e63115ea1420498f663885dab29ddedc2d8f9fbc84e8d4af7ce83?d=identicon)[surcoufx83](/maintainers/surcoufx83)

---

Top Contributors

[![Philipp15b](https://avatars.githubusercontent.com/u/425358?v=4)](https://github.com/Philipp15b "Philipp15b (60 commits)")[![surcoufx83](https://avatars.githubusercontent.com/u/1303974?v=4)](https://github.com/surcoufx83 "surcoufx83 (17 commits)")[![computator](https://avatars.githubusercontent.com/u/3962958?v=4)](https://github.com/computator "computator (7 commits)")[![bgarret](https://avatars.githubusercontent.com/u/231680?v=4)](https://github.com/bgarret "bgarret (2 commits)")[![PerWiklander](https://avatars.githubusercontent.com/u/391989?v=4)](https://github.com/PerWiklander "PerWiklander (2 commits)")[![serhii-kotyk-sm](https://avatars.githubusercontent.com/u/39482270?v=4)](https://github.com/serhii-kotyk-sm "serhii-kotyk-sm (2 commits)")[![pabloharger](https://avatars.githubusercontent.com/u/8269931?v=4)](https://github.com/pabloharger "pabloharger (1 commits)")[![bocall](https://avatars.githubusercontent.com/u/21174250?v=4)](https://github.com/bocall "bocall (1 commits)")[![crnjakovic](https://avatars.githubusercontent.com/u/101252188?v=4)](https://github.com/crnjakovic "crnjakovic (1 commits)")[![eduardo-marcolino](https://avatars.githubusercontent.com/u/808889?v=4)](https://github.com/eduardo-marcolino "eduardo-marcolino (1 commits)")[![JurgenNL](https://avatars.githubusercontent.com/u/4716167?v=4)](https://github.com/JurgenNL "JurgenNL (1 commits)")

### Embed Badge

![Health badge](/badges/surcoufx83-php-i18n/health.svg)

```
[![Health](https://phpackages.com/badges/surcoufx83-php-i18n/health.svg)](https://phpackages.com/packages/surcoufx83-php-i18n)
```

###  Alternatives

[symfony/translation

Provides tools to internationalize your application

6.6k836.5M2.0k](/packages/symfony-translation)[nesbot/carbon

An API extension for DateTime that supports 281 different languages.

169661.4M4.8k](/packages/nesbot-carbon)[joedixon/laravel-translation

A tool for managing all of your Laravel translations

717911.4k11](/packages/joedixon-laravel-translation)[illuminate/translation

The Illuminate Translation package.

6936.4M491](/packages/illuminate-translation)[philipp15b/php-i18n

Simple i18n class for PHP

320124.4k7](/packages/philipp15b-php-i18n)[larswiegers/laravel-translations-checker

Make sure your laravel translations are checked and are included in all languages.

256423.2k2](/packages/larswiegers-laravel-translations-checker)

PHPackages © 2026

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