MilkCore

Lang
in package

Internationalization and Localization Class

This class provides a simple translation system with two main functions:

  • set: to define a string and its translation
  • get: to retrieve the translation for a string

The class supports area-specific translations, allowing different translations for the same string in different parts of the application.

Tags
example
// Set translations
Lang::set('Hello', 'Bonjour', 'french');
Lang::set('Hello', 'Hola', 'spanish');

// Get translations
echo Lang::get('Hello', 'french'); // Outputs: Bonjour
echo Lang::get('Hello', 'spanish'); // Outputs: Hola
echo Lang::get('Hello'); // Outputs: Hello (falls back to original if no translation in 'all' area)

Table of Contents

Methods

get()  : string
Gets the translation for a string
load_ini_file()  : bool
Upload a translation file
set()  : void
Sets a string and its translation

Methods

get()

Gets the translation for a string

public static get(string $string[, string $area = 'all' ]) : string

This method retrieves the translation for a specific string in the specified area. If no translation is found in the specified area, it falls back to the 'all' area. If no translation is found at all, it returns the original string.

Parameters
$string : string

The original string to translate

$area : string = 'all'

The area to look for the translation in (default: 'all')

Tags
example
// Get a translation in a specific area
$translated = Lang::get('Submit', 'forms');

// Get a global translation
$translated = Lang::get('Cancel');
Return values
string

The translated string or the original if no translation is found

load_ini_file()

Upload a translation file

public static load_ini_file(string $file[, string $area = 'all' ]) : bool
Parameters
$file : string
$area : string = 'all'
Return values
bool

set()

Sets a string and its translation

public static set(string $string, string $translation[, string $area = 'all' ]) : void

This method defines a translation for a specific string. Translations can be limited to specific areas of the application by specifying the area parameter.

Parameters
$string : string

The original string to translate

$translation : string

The translated string

$area : string = 'all'

The area to limit this translation to (default: 'all')

Tags
example
// Global translation
Lang::set('Save', 'Enregistrer');

// Area-specific translation
Lang::set('Post', 'Article', 'blog');

        
On this page

Search results