folded/translation

Translate terms for your web app.

v0.1.0 2020-10-16 18:35 UTC

This package is auto-updated.

Last update: 2024-12-17 04:04:10 UTC


README

Translate terms for your web app.

Packagist License Packagist PHP Version Support Packagist Version Build Status Maintainability TODOs

Summary

About

I created this package to have a standalone, easy way to use translation in my web app. This library is based on Laravel's translation engine.

Folded is a constellation of packages to help you setting up a web app easily, using ready to plug in packages.

Features

  • Can translate terms in the desired language
  • Can set a default language to fallback on
  • Can use json and key based translations in the same time
  • Can use pluralized translations

Requirements

  • PHP version >= 7.4.0
  • Composer installed

Installation

1. Install the package

In your root folder, run this command:

composer required folded/translation

2. Prepare the folders

To work, you need to have a folder that contains your translation. This is the recommended organization:

.
└── lang/
    ├── en
    └── fr

Inside the lang folders, you can use any organization that fits your needs, from key based translations to json based translation.

When using key based translation, you will likely put any files containing the key and translated terms in their according folder depending the language. Here is an example.

.
└── lang/
    ├── en/
    │   └── messages.php
    └── fr/
        └── messages.php

For example, the messages.php file can contain this for the en folder:

return [
  "home" => [
    "title" => "Welcome in the home page",
  ],
];

And this for the fr folder:

return [
  "home" => [
    "title" => "Bienvenue sur la page d'accueil",
  ],
];

Howether, if you want, you can use json based translation. A good use case with JSON based translation files is when you need to use the translated term as the key. JSON based translations files must leave in the immediate folder lang, unless the previous key based method. Here is an example:

.
└── lang/
    ├── en/
    │   └── messages.php
    ├── fr/
    │   └── messages.php
    ├── en.json
    └── fr.json

And here is the content of en.json for example:

{
  "Contact us for a custom tailored quotation": "Contact us for a custom tailored quotation"
}

And here is the content of fr.json:

{
  "Contact us for a custom tailored quotation": "Contactez nous pour un devis sur-mesure"
}

3. Add the bootstrap code

As early as possible, configure the library:

use function Folded\setDefaultTranslationLang;
use function Folded\setTranslationFolderPath;

setDefaultTranslationLang("en");
setTranslationFolderPath("path/to/folder");

Examples

Keep in mind that, at any moment, you can refer to the official Laravel translation documentation if you have some doubt.

1. Get a translated term by its key

In this example, we will get a translated term from a key based translation.

use function Folded\getTranslation;

echo getTranslation("messages.home.title");

This implies you have this folder structure:

.
└── lang/
    ├── en/
    │   └── messages.php
    └── fr/
        └── messages.php

You can also get a translated term from the original term text itself. For this, we recommend using JSON based translation.

use function Folded\getTranslation;

echo getTranslation("Contact us for a custom tailored quotation");

This implies you have this folder structure:

.
└── lang/
    ├── en.json
    └── fr.json

2. Use placeholders in translated term

In this example, we will put values in a translation that contains placeholders.

use function Folded\getTranslation;

echo getTranslation("messages.home.welcome", ["name" => "John"]);

This implies you have a translation like following:

// lang/en/messages.php
return [
  "home" => [
    "welcome" => "Welcome, :name",
  ],
];

3. Get a pluralized translated term

In this example, will get a pluralizable term translation.

use function Folded\getVariableTranslation;

$numberOfPageViewed = 5000;

echo getVariableTranslation("messages.home.page-viewed", $numberOfPageViewed);

This implies you have the following folder structure:

.
└── lang/
    ├── en/
    │   └── messages.php
    └── fr/
        └── messages.php

And your messages.php file in the en folder contains:

return [
  "home" => [
    "page-viewed" => "{0} No page viewed|[1] One page viewed|[2,*] :count page viewed",
  ]
];

For more information, browse the Laravel pluralized translation documentation.

4. Change the lang before getting a translated term

In this example, we will change the lang right before getting the translated term.

use function Folded\setTranslationLang;
use function Folded\getTranslation;

setTranslationLang("fr");

echo getTranslation("messages.home.title");

Version support