Skip to content

Method overview

Michael Spiss edited this page Aug 17, 2017 · 2 revisions

Constructor

public function __construct(string $fallback_locale, string $resource_dir)

Arguments:

  • string $fallback_locale: The locale which is being used by default and when an unsupported locale is requested or set by using setLocale()
  • string $resource_dir: The directory containing all language files sorted by locale

Supports

public function supports(string $locale): bool

Finds whether or not a locale is supported.

Arguments:

  • string $locale: The locale to check support for

Returns:

Whether or not a subdirectory with the $locale's name exist


getLocale

public function getLocale(): string

A way to get the currently set locale.

Returns:

The currently set locale


setLocale

public function setLocale(string $locale): void

Sets a new locale. If not supported, the fallback locale is being used.

Arguments:

  • string $locale: The new locale

addLoader

public function addLoader(string $format, LoaderInterface $loader): void

Adds support for the specified file format.

Arguments:

  • string $format: The file extension the Loader should be used for
  • LoaderInterface $loader: An instance of a class implementing MichaelSpiss\Translation\LoaderInterface

has

public function has(string $key, string $locale=''): bool

Checks if a translation is available for the given $key

Arguments:

  • string $key: The identifier for the string to search for
  • string $locale: Optional. Allows you to check if the $key exists for the defined $locale.

Returns: Whether or not the $key exists


trans

public function trans(string $key, array $replace=[], string $locale=''): string

Returns the string matching the $key identifier.

Arguments:

  • string $key: The identifier for the string to search for.
  • array $replace: Optional. If the selected string contains placeholders, replacements can be defined here. For more information please see the (placeholders)[] entry.
  • string $locale: Optional. If you want to return the string in an other language than the currently active one, you can set it here.

Returns: If the string is found, it is returned with the placeholders already replaced. If it isn't, the $key is returned. This allows you to spot errors without breaking or throwing an exception.


transChoice

public function transChoice(string $key, $number, array $replace=[], string $locale='')

Returns the string matching the $key identifier, while respecting pluralization rules.

Arguments:

  • string $key: The identifier for the string to search for.
  • int|float $number: The number which is taken into account for pluralization
  • array $replace: Optional. If the selected string contains placeholders, replacements can be defined here. For more information please see the (placeholders)[] entry.
  • string $locale: Optional. If you want to return the string in an other language than the currently active one, you can set it here.

Returns: If the string is found, it is returned with the placeholders already replaced and pluralization applied. If it isn't, the $key is returned. This allows you to spot errors without breaking or throwing an exception.