Download the PHP package userfrosting/i18n without Composer

On this page you can find all versions of the php package userfrosting/i18n. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package i18n

I18n module for UserFrosting 4

Latest Version Join the chat at https://chat.userfrosting.com/channel/support Donate

Branch Build Coverage Style
master
develop  

Louis Charette & Alexander Weissman, 2016-2019

The I18n module handles translation tasks for UserFrosting.

The translator uses a Dictionary, which is itself tied to a Locale. The basic structure of the system can be represented this way :

The Locale knows all there is to know about the locale itself. The name of the locale, the authors, the plural rules, etc.

The Dictionary is tied to a specific locale. It's purpose is to return a data matrix composed of keys shared between all locales (called message keys) and the translated string (called localized messages). The system uses a KEY and VALUE system, which can be stored in standard PHP files.

The translator use the data from the Dictionary to perform the actual translation, aka finding the proper key and replacing the placeholder with the specified values.


Table of Contents

Documentation

Basic usage

Step 1 - Set up language file(s).

A language file returns an array mapping message keys to localized messages. Messages may optionally have placeholders. For example:

locale/en_US/main.php

locale/es_ES/main.php

Step 2 - Set up the Locale:

The Locale class will load all configuration value for the specified locale. All you need to do is specify the locale identifier as a string :

This will load all the English (en_US) locale config. When passed to the Dictionary, this will load all the English definitions.

Step 3 - Set up UniformResourceLocator and the Dictionary:

The Dictionary will load all definitions for the specified locale. To achieve this, the dictionary will build a list of the files located in the locale identifier directory (eg. en_US) for the specified locator path. The UniformResourceLocator will be used to get the file list.

First, you'll need to setup the Locator. See the UniformResourceLocator documentation for more information on this.

With the locator and the locale, we can now create the Dictionary instance.

Step 4 - Initialize a Translator object:

The translator can now be initiated with the Dictionary. The Locale will be inherited from the Dictionary.

Step 5 - Do a translation!

Wrap up

Locale configuration file

Each locale have it's own configuration file. Theses options are required to be saved in a locale.yaml file, located in the locale folder, accessible accessible using the locale://en_US/locale.yaml URI.

The configuration file can contain multiple options. For example :

Config values

name

The name of the locale. Should be the English version of the name.

regional

The localized name of the locale. For example, for the French locale, the name of the locale in French.

authors

A list of authors for the locale.

plural_rule

The plural rule number associated with the locale. See Pluralization for more details.

parents

A list of parents locales for this locale. Each locale data will be loaded on top of the parents one, including all dictionary definitions.

For example, if the fr_CA locale has fr_FR as parent, all config and all keys not found in the CA translation will fallback to the FR one. If the fr_FR locale has en_US as parent itself, all keys not found in CA and FR will fallback to the English keys.

It is recommended all locale have at least en_US as a top parent, so undefined keys in your locale will fallback to the English version.

Pluralization

The plural system allow for easy pluralization of strings. This whole system is based on Mozilla plural rules. For a given language, there is a grammatical rule on how to change words depending on the number qualifying the word. Different languages can have different rules. For example, in English you say no cars (note the plural cars) while in French you say Aucune voiture (note the singular voiture).

The rule associated with a particular language (see link above) is defined in the locale configuration metadata. So for the english locale, you should find plural_rule: 1 and in the french file "plural_rule: 2.

Strings with plural forms are defined as sub arrays with the rules as the key. The right plural form is determined by the plural value passed as the second parameter of the translate function :

The plural value used to select the right form is defined by default in the plural placeholder. This means that $translator->translate("HUNGRY_CATS", 5) is equivalent to $translator->translate("HUNGRY_CATS", ['plural' => 5]). The plural placeholder can also be used in the string definition. Note that in this case, it is recommended to use the X_ prefix to indicate that the plural will be displayed :

In this example, you can see that 0 is used as a special rules to display No hungry cats instead of 0 hungry cats to create more user friendly strings. Note that the plural placeholder can be overwritten using handles.

When the first argument of the translate function points to a plural key in the language definition files and the second parameter is omitted, the plural value will be 1 by default unless a @TRANSLATION key is defined (See Handles). In the previous example, $translator->translate("X_HUNGRY_CATS", 1) is equivalent to $translator->translate("X_HUNGRY_CATS").

Plural value with placeholders

If you have more than one placeholder, you must then pass the plural value in the placeholders (no shortcut possible).

Multiple plural in a string

If a localized string contain more than more plural, for example 1 guest and 4 friends currently online, you can apply the plural rule to both guest and friends by nesting the ONLINE_GUEST and ONLINE_FRIEND keys into ONLINE_USERS:

Note that nested translations can be used when faced with long sentence using multiples sub strings or plural form, but those should be avoided when possible. Shorter or multiple sentences should be preferred instead. Specials handles can also be useful in those cases.

Numbers are rules, not limits !

REALLY IMPORTANT : The number defined in the language files IS NOT related to the plural value, but to the plural rule. So this is completely WRONG :

One last thing about pluralization...

In some cases, it could be faster and easier to directly access the plural value. For example, when the string will always be plural. Consider the following example :

In this example, $translator->translate("COLOR", 2); and $translator->translate("COLORS"); will return the same value. This is true for English, but not necessarily for all languages. While languages without any form of plural definitions will define something like "COLOR" => "Color" and "COLORS" => "Color", some will have even more complicated rules. That's why it's always best to avoid keys like COLORS if you plan to translate to more than one language. This is also true with the 0 value that can be different across different language, but can also be handle differently depending of the message you want to display (Ex.: No colors instead of 0 colors).

Sub keys

Sub keys can be defined in language files for easier navigation of lists or to distinguish two items with common keys. For example:

Sub keys can be accessed using dot syntax. So $translator->translate('COLOR.BLACK') will return black. Sub keys are also useful when multiple master keys share the same sub keys:

Of courses, sub keys and plural rules can live together inside the same master key :

Handles

Some special handles can be defined in the languages files to modify the default behavior of the translator. These handle uses the @ prefix.

@TRANSLATION

If you want to give a value for the top level key, you can use the @TRANSLATION (handle)[#handles] which will create an alias TOP_KEY and point it to TOP_KEY.@TRANSLATION:

N.B.: When @TRANSLATION is used with plural rules, omitting the second argument of the translate function will change the result. 1 will not be used as a plural value to determine which rule we chose. The @TRANSLATION value will be returned instead. For example:

With @TRANSLATION define above, $translator->translate("X_HUNGRY_CATS"); will return Hungry cats. Remove the @TRANSLATION handle and the same $translator->translate("X_HUNGRY_CATS"); will now return 1 hungry cat.

@PLURAL

The default plural default placeholder can be overwritten by the @PLURAL handle in the language files. This may be useful if you pass an existing array to the translate function.

The & placeholder

When a placeholder name starts with the & character in translation files or the value of a placeholder starts with this same & character, it tells the translator to directly replace the placeholder with the message mapped by that message key (if found). Note that this is CASE SENSITIVE and, as with the other handles, all placeholders defined in the main translation function are passed to all child translations. This is useful when you don't want to translate the same word over and over again in the same language file or with complex translations with plural values. Be careful when using this with plurals as the plural value is passed to all child translation and can cause conflict (See Example of a complex translation).

Example:

In this example, {{&MY_CATS}} gets replaced with the MY_CATS and since there's 3 cats, the n° 2 rule is selected. So the string becomes I love my {{plural}} cats which then becomes I love my 3 cats.

N.B.: Since this is the last thing handled by the translator, this behavior can be overwritten by the function call:

Since the other placeholders, including the plural value(s) are also be passed to the sub translation, it can be useful for languages like french where the adjectives can also be "pluralizable". Consider this sentence : I have 3 white catS. In french, we would say J'ai 3 chatS blancS. Notice the S on the color blanc? One developer could be tempted to do this in an English context :

While this would work in english because the color isn't "pluralizable", it won't in french. We'll end up with J'ai 3 chatS blanc (No S on the color). What we need is the php code to call the translation and passing the color key as a placeholder using the & prefix : $translator->translate('MY_CATS', ["plural" => 3, "color" => "&COLOR.WHITE"]);. The languages files for both languages in this case would be:

English

French

Since the placeholders (["plural" => 3, "color" => "&COLOR.WHITE"]) will be passed to the translate function when the COLOR.WHITE key is translated, the correct plural form will be returned for the color in french, giving us J'ai 3 chatS blancS. Even without any plural value, this is still shorter to use that defining both translate function inside the php code :

Vs.

Finally, if the sub translated key is missing in the translation file, we simply end up with the placeholder not being translated (which is something you may want in certain context) : I have 3 COLOR.WHITE cats.

Example of a complex translation

Language file

Translate function

Result

Style Guide

Testing

API docs

To build docs :


All versions of i18n with dependencies

PHP Build Version
Package Version
Requires php Version >=7.1
twig/twig Version ^2.11
userfrosting/support Version ~4.5.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package userfrosting/i18n contains the following files

Loading the files please wait ....