\champlatesDictionary

Class Dictionary Handles translation of strings. The translations are provided by external JSON files in the format:

{ "KEY": "VALUE", "KEY2": "VALUE2", ... }

The JSON files must end in .json and start with the language identifier followed by a hyphen. Each file can only contain data for a single language

Summary

Methods
Properties
Constants
__construct()
crawlForTranslations()
determineLanguages()
readTranslations()
isJsonFile()
get()
translate()
No public properties found
No constants found
No protected methods found
No protected properties found
N/A
No private methods found
No private properties found
N/A

Methods

__construct()

__construct(\champlates\string  $translationFiles, \champlates\string  $translationIdentifier = "@{KEY}") 

Dictionary constructor.

Automatically constructs the translations from the provided translation file locations.

Parameters

\champlates\string $translationFiles
\champlates\string $translationIdentifier

Throws

\champlates\InvalidArgumentException:

If there is a problem with the input

crawlForTranslations()

crawlForTranslations(  $translationFiles) : \champlates\array:

Recursively parses a directory/file for .json files and returns them in an array.

Parameters

$translationFiles

Throws

\champlates\InvalidArgumentException:

If the argument passed was neither a file path or a directory path

Returns

\champlates\array: —

The array of JSON files

determineLanguages()

determineLanguages(  $translationFiles) : \champlates\array:

Determines which languages are translated by which files

Parameters

$translationFiles

Returns

\champlates\array: —

An associative array mapping the language identifiers to corresponding JSON files.

readTranslations()

readTranslations(array  $languages) : \champlates\array:

Reads the content of the JSON files and maps their data to the individual languages

Parameters

array $languages

Throws

\champlates\InvalidArgumentException:

If duplicate keys exist between the JSON files for a language

Returns

\champlates\array: —

An associative array mapping the languages to the JSON data

isJsonFile()

isJsonFile(\champlates\string  $file) : \champlates\bool:

Makes sure that a file is a JSON file

Parameters

\champlates\string $file

Returns

\champlates\bool: —

true of the file is a JSON file, false otherwise

get()

get(\champlates\string  $key, \champlates\string  $language) : string

Translates a given key using the given language

Parameters

\champlates\string $key
\champlates\string $language

Returns

string

translate()

translate(\champlates\string  $text, \champlates\string  $language) : \champlates\string:

Translates a string using the translation data. Keys in the text are found using a simple replace operation. Which is why the keyIdentifier can be used to minimize false positives

Parameters

\champlates\string $text
\champlates\string $language

Throws

\champlates\InvalidArgumentException:

If the language specified does not exist.

Returns

\champlates\string: —

The translated string