The Laravel Translationpackage interacts with the files in order to achieve the following:

List all languages

Add a language

List all translations

Add a translation

Update existing translations

The plan for the package is, much like many features of Laravel, to expose multiple drivers to power the translation management. The first driver will utilize Laravel’s existing file-based translations with plans to later add a database driver. With this in mind, we first define a contract to which driver will implement to ensure all the required methods are available to the package.

The file driver needs to interrogate the filesystem in order to return the data in the required format. This involves a lot of filtering, mapping and iterating, so we will lean quite heavily on Laravel’s collections.

Listing languages

To generate a collection of languages, we use the filesystem to get an array of directories from the configured language path, wrapping the result in a collection.

Adding/updating translations

Translations are added and updated in largely the same fashion. First, we get the contents of the file the translation should be added to in array format. Then, we check whether or not the key to be added already exists. If it does, we update the value and if not, we append the new key and value to the array. Finally, the whole array is written back to the file.

This driver lays the foundation from which we can build upon. Next time, we’ll build out the user interface which will ship with the package. It utilizes a combination of Tailwind CSS and Vue.js, two frameworks which have been widely adopted by the Laravel community.