Skip to content

Commit

Permalink
docs: add installation instructions and examples
Browse files Browse the repository at this point in the history
  • Loading branch information
danjohnson95 committed Aug 5, 2024
1 parent ca23091 commit c1e45f6
Showing 1 changed file with 83 additions and 1 deletion.
84 changes: 83 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,85 @@
# Laravel i18n Compatibility

This package makes Laravel's translation work in tandem with frontend localisation frameworks, such as i18next.
![Tests](https://github.com/danjohnson95/laravel-i18n-compatibility/actions/workflows/test.yml/badge.svg)

This package makes Laravel's translation work in tandem with frontend localisation frameworks, such as i18next. So you can share the same translation strings between your Laravel backend, and your React or Vue frontend.

- [Laravel i18n Compatibility](#laravel-i18n-compatibility)
- [Example](#example)
- [Installation](#installation)
- [Usage](#usage)

## Example

Normally, your Laravel translation strings that interpolate variables would look like this:

```json
// resources/lang/en.json
{
"greeting": "Welcome, :name!"
}
```

But with this package, you can also define your translation strings like this:

```json
// resources/lang/en.json
{
"greeting": "Welcome, {{name}}!"
}
```

Meaning you can share that same translation string with your frontend localisation framework.

It works for pluralisation too. Laravel usually expects pluralisation strings to be delimited by a pipe character, like this:

```json
// resources/lang/en.json
{
"apples": "There is one apple|There are many apples"
}
```

But with this package, you can also define your pluralisation strings like this:

```json
// resources/lang/en.json
{
"apples_one": "There is one apple",
"apples_other": "There are many apples"
}
```

It's completely backwards compatible with Laravel's syntax too, so your Laravel application will consume either syntax.

## Installation

You can install the package via composer:

```bash
composer require danjohnson95/laravel-i18n-compatibility
```

## Usage

In Laravel, you can use your localisation strings exactly as you would normally.

```json
// resources/lang/en.json
{
"greeting": "Welcome, {{name}}!",
"apples_one": "There is one apple",
"apples_other": "There are many apples"
}
```

```php
// app/Http/Controllers/ExampleController.php
public function index()
{
return response()->json([
'greeting' => __('greeting', ['name' => 'Dan']),
'apples' => trans_choice('apples', ['count' => 1]),
]);
}
```

0 comments on commit c1e45f6

Please sign in to comment.