Skip to content
This repository has been archived by the owner on Jan 23, 2020. It is now read-only.

Latest commit

 

History

History
183 lines (139 loc) · 5.05 KB

File metadata and controls

183 lines (139 loc) · 5.05 KB

$L - is system object, that provides multilingual interface functionality, instance can be obtained in such way:

<?php
$L = \cs\Language::instance();

$L is used only because it is short for frequent usage

###[Up](#) Methods

$L object has next public methods:

  • url_language()
  • get()
  • set()
  • format()
  • time()
  • to_locale()
  • change()

Returns instance for simplified work with translations, when using common prefix

url_language($url = false : false|string) : false|string

Does URL have language prefix. If there is language prefix - language will be returned, false otherwise.

get($item : bool|string, $language = false : false|string, $prefix = '' : string) : string

Get translation.

<?php
$L           = \cs\Language::instance();
$module_name = $L->get('module_name');

Also there is simplified way to get translation - to get it as property of object:

<?php
$L           = \cs\Language::instance();
$module_name = $L->module_name;

set($item :array|string, $value = null : null|string)

Method is used to set translation.

format($name : string, $arguments : string[], $language = false : false|string, $prefix = '' : string) : string

Method is used for formatted translation. Example:

translation (in json file)

"hello":"Hello, %s!"

usage

<?php
$L = \cs\Language::instance();
$L->format('hello', ['my friend']);

Translation string should be formatted according to sprintf() PHP function.

Also there is simplified way to get formatted string - to get it as result of calling of object function:

<?php
$L = \cs\Language::instance();
$L->hello('my friend');

This way is more natural. You can specify as much arguments as you need.

time($in : int, $type : string) : string

Is used to convert seconds to string representation of specified type.

Of course, it works with string endings (to realize this properly, $L->time property is used).

to_locale($data : string|string[], $short_may = false : bool) : string

Converts date (only date as for now) obtained with date() PHP function to locale translation (translates months names and days of week).

change($language : string) : bool

Method for language changing (usually is called by system automatically, and set language to user specific).

###[Up](#) Properties

$L object has next public properties:

  • clanguage
  • time
  • clang*
  • cregion*
  • content_language*
  • _datetime_long*
  • _datetime*
  • _date*
  • _time*

* actually are not properties, they are translations, but are widely used and should be mentioned here.

Current language:

  • English
  • Russian
  • Ukrainian

time

May be set to closure to be used by $L->time() instead of built-in mechanism, to provide proper endings of words for some languages.

clang

ISO 639-1 language code:

  • en
  • ru
  • uk

cregion

ISO 3166-1 Alpha-2 region code:

  • us
  • ru
  • ua

content_language

Content language that is primarily used in HTTP header Content-Language, but might also be used in other places. If not given in translations, defaults to clang.

_datetime_long

Long format of date and time for date() PHP function

_datetime

Short format of date and time for date() PHP function

_date

Short format of date for date() PHP function

_time

Short format of time for date() PHP function

###[Up](#) Interfaces
  • JsonSerializable

Object implements JsonSerializable interface and allows getting of all translations of current language in JSON format (it is used by system in order to have access to translations in JavaScript).

###[Up](#) Events

$L object supports only one event:

  • System/Language/change/before
  • System/Language/change/after
  • System/Language/load

Is fired before language change

System/Language/change/after

Is fired after language change

System/Language/load

This event is fired when loading language translations to allow third-party components to add their own translations. Array:

[
    'clanguage'    => clanguage,
    'clang'        => clang,
    'cregion'      => cregion
]

is set as parameter for event, all necessary language and locale aspects are given with event to determine exact translation.

###[Up](#) \cs\Language\Prefix class

This class is used for simplified work with languages, when using common prefix. It includes methods for getting of translations just like cs\Language class, but automatically adds prefix specified in constructor to every item. In case of prefixed usage full keys are still available, but prefixed keys are preferred in case of conflict. Next methods are available:

  • get
  • format
  • time *
  • to_locale *

* prefixes are not added because there is no need for that.