Internationalization API: Localizing Your Views

This API allows developers to make their applications support internationalization and localization of response based on client's preferred locale.

How does it work?

This API chooses a pure PHP implementation of internationalization requirements, using JSON files to store translations. Each JSON file is found on disk at folder/locale/domain.extension path where:

Structure of that file is a dictionary where key is a short keyword that identifies each entity to be translated while value is translation text that will replace keyword when view is compiled. This means for each domain, JSON file must contain same keywords, only with different values specific to locale/language. If a keyword has no matching translation in JSON file, it will appear literally is when view is compiled (aligning with GETTEXT standards).

Example:

This API splits the basics of its goals into three classes, each with its own responsibility:

  1. Settings: encapsulates settings required to read from / write to JSON files. These include information about locale, domain, folder, extension described above. If a default locale is set, it will be used whenever no JSON file exists for preferred locale.
  2. Reader: reads from relevant JSON translation file based on Settings above. Once a JSON file is read to retrieve a translation, its contents become instantly available to any other keyword for that locale and domain.
  3. Writer: writes translations to relevant JSON file based on user input and Settings above. Unlike GETTEXT, the moment a new translation is saved, it becomes instantly available to clients without web server restart.

All classes inside API belong to namespace Lucinda\Internationalization!

How can I use it?

To learn how to install and use this API, follow this step-by-step guide!


Share