Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature namespaces #174

Closed
wants to merge 33 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
be19111
added .idea folder into .gitignore
Jul 19, 2024
57f6f6b
added .phpunit.cache folder into .gitignore
Jul 19, 2024
04024e7
phpunit.xml.dist schema updated
Jul 19, 2024
caf72c8
code style
Jul 19, 2024
188967c
PHPUnit replaced by Pest
Jul 19, 2024
6a62ee8
Tests refactoring
Jul 19, 2024
cfd30e9
PHP 8.0 compatibility
Jul 19, 2024
450db0d
github test runner fixed
Jul 19, 2024
9e4153b
PHP CS Fixer added and reconfigured
Jul 19, 2024
2e60a98
PHP CS Fixer configuration rules fix
Jul 19, 2024
83e3070
PHP CS Fixer configuration rules fixed
Jul 19, 2024
df9b024
Update composer.json
Jul 22, 2024
a3d8682
Update composer.json and run-tests.yml
Jul 22, 2024
394e2af
Revert "Update composer.json and run-tests.yml"
Jul 22, 2024
a5d5cc2
Update composer.json
Jul 22, 2024
ee48bd4
removed unused migration
Jul 22, 2024
5cce48e
removed constants and comments in test files
Jul 22, 2024
e03d396
removed DOCBlocks form Pest.php
Jul 22, 2024
9c5f657
removed declare(strict_types=1);
Jul 22, 2024
0fa8621
Fix styling
aon4o Jul 22, 2024
3b436f8
package ServiceProvider updated with spatie/laravel-package-tools
aon4o Jul 22, 2024
42f1b7f
updated README.md
aon4o Jul 22, 2024
76e2d5b
Fix Laravel 8 compatibility
aon4o Jul 22, 2024
b077803
grammar fix README.md
aon4o Jul 22, 2024
66f23fa
Namespaced translations feature implemented
aon4o Jul 23, 2024
c8d8544
updated CHANGELOG.md
aon4o Jul 23, 2024
8317a9c
tests fixed
aon4o Jul 23, 2024
6a52598
backwards compatibility for migrations fixed
aon4o Jul 23, 2024
e9fcb51
backwards compatibility for migrations fixed
aon4o Jul 23, 2024
263cd2e
LanguageLine caching updated
aon4o Jul 23, 2024
e0c07e5
Added tests for namespaced translations
aon4o Jul 23, 2024
9239886
Translation loading fix
aon4o Jul 25, 2024
dc62cbb
Fix styling
aon4o Jul 25, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/php-cs-fixer.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: Check & fix styling

on: [push]
on: [ push ]

jobs:
style:
Expand All @@ -13,7 +13,7 @@ jobs:
- name: Fix style
uses: docker://oskarstark/php-cs-fixer-ga
with:
args: --config=.php_cs --allow-risky=yes
args: --config=.php-cs-fixer.php --allow-risky=yes

- name: Extract branch name
shell: bash
Expand Down
98 changes: 49 additions & 49 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
@@ -1,54 +1,54 @@
name: run-tests

on:
- push
- pull_request
- push
- pull_request

jobs:
test:
runs-on: ${{ matrix.os }}

strategy:
fail-fast: false
matrix:
os: [ubuntu-latest]
php: [8.2, 8.1, 8.0]
laravel: ['8.*', '9.*', '10.*', '11.*']
dependency-version: [prefer-stable]
include:
- laravel: 10.*
testbench: 8.*
- laravel: 9.*
testbench: 7.*
- laravel: 8.*
testbench: 6.*
- laravel: 11.*
testbench: 9.*
exclude:
- laravel: 10.*
php: 8.0
- laravel: 11.*
php: 8.1
- laravel: 11.*
php: 8.0

name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }} - ${{ matrix.os }}

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick
coverage: none

- name: Install dependencies
run: |
composer require "laravel/framework:${{ matrix.laravel }}" "nesbot/carbon:^2.63" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update
composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction

- name: Execute tests
run: vendor/bin/phpunit
test:
runs-on: ${{ matrix.os }}

strategy:
fail-fast: false
matrix:
os: [ ubuntu-latest ]
php: [ 8.2, 8.1, 8.0 ]
laravel: [ '8.*', '9.*', '10.*', '11.*' ]
dependency-version: [ prefer-stable ]
include:
- laravel: 10.*
testbench: 8.*
- laravel: 9.*
testbench: 7.*
- laravel: 8.*
testbench: 6.*
- laravel: 11.*
testbench: 9.*
exclude:
- laravel: 10.*
php: 8.0
- laravel: 11.*
php: 8.1
- laravel: 11.*
php: 8.0

name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }} - ${{ matrix.os }}

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick
coverage: none

- name: Install dependencies
run: |
composer require "laravel/framework:${{ matrix.laravel }}" "nesbot/carbon:^2.63" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update
composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction

- name: Execute tests
run: composer test
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@ build
composer.lock
docs
vendor
.php_cs.cache
.php-cs-fixer.cache
.phpunit.cache
.phpunit.result.cache
.idea
30 changes: 23 additions & 7 deletions .php_cs → .php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,46 @@
->ignoreDotFiles(true)
->ignoreVCS(true);

return PhpCsFixer\Config::create()
return (new PhpCsFixer\Config())
->setRules([
'@PSR2' => true,
'array_syntax' => ['syntax' => 'short'],
'ordered_imports' => ['sortAlgorithm' => 'alpha'],
'array_syntax' => [
'syntax' => 'short',
],
'ordered_imports' => [
'sort_algorithm' => 'alpha',
],
'no_unused_imports' => true,
'not_operator_with_successor_space' => true,
'trailing_comma_in_multiline_array' => true,
'trailing_comma_in_multiline' => [
'elements' => [
'arrays',
],
],
'phpdoc_scalar' => true,
'unary_operator_spaces' => true,
'binary_operator_spaces' => true,
'blank_line_before_statement' => [
'statements' => ['break', 'continue', 'declare', 'return', 'throw', 'try'],
'statements' => [
'break',
'continue',
'declare',
'return',
'throw',
'try',
],
],
'phpdoc_single_line_var_spacing' => true,
'phpdoc_var_without_name' => true,
'class_attributes_separation' => [
'elements' => [
'method', 'property',
'method' => 'one',
'property' => 'one',
],
],
'method_argument_space' => [
'on_multiline' => 'ensure_fully_multiline',
'keep_multiple_spaces_after_comma' => true,
]
],
])
->setFinder($finder);
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

All notable changes to `laravel-translation-loader` will be documented in this file

[//]: # (TODO: Update version when decided)

## 3.0.0 - 2024-07-23

- added support for namespaced database translations

## 2.7.0 - 2020-12-04

- add support for php 8.0
Expand Down Expand Up @@ -41,7 +47,9 @@ All notable changes to `laravel-translation-loader` will be documented in this f
- add support for Laravel 5.8

## 2.2.3 - 2019-02-01

``

- use Arr:: and Str:: functions

## 2.2.2 - 2018-10-25
Expand Down
67 changes: 45 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,35 @@
![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/spatie/laravel-translation-loader/run-tests.yml?branch=main&label=Tests&style=flat-square)
[![Total Downloads](https://img.shields.io/packagist/dt/spatie/laravel-translation-loader.svg?style=flat-square)](https://packagist.org/packages/spatie/laravel-translation-loader)

In a vanilla Laravel or Lumen installation you can use [language files](https://laravel.com/docs/localization) to localize your app. This package will enable the translations to be stored in the database. You can still use all the features of [the `__` helper function](https://laravel.com/docs/localization#retrieving-translation-strings) you know and love.
In a vanilla Laravel or Lumen installation, you can use [language files](https://laravel.com/docs/localization) to
localize your app. This package will enable the translations to be stored in the database. You can still use all the
features of [the `__` helper function](https://laravel.com/docs/localization#retrieving-translation-strings) you know
and love.

```php
__('messages.welcome', ['name' => 'dayle']);
```

You can even mix using language files and the database. If a translation is present in both a file and the database, the database version will be returned.
You can even mix using language files and the database. If a translation is present in both a file and the database, the
database version will be returned.

Want to use a different source for your translations? No problem! The package is [easily extendable](https://github.com/spatie/laravel-translation-loader#creating-your-own-translation-providers).
Want to use a different source for your translations? No problem! The package
is [easily extendable](https://github.com/spatie/laravel-translation-loader#creating-your-own-translation-providers).

Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects [on our website](https://spatie.be/opensource).
Spatie is a web design agency based in Antwerp, Belgium.
You'll find an overview of all our open source
projects [on our website](https://spatie.be/opensource).

## Support us

[<img src="https://github-ads.s3.eu-central-1.amazonaws.com/laravel-translation-loader.jpg?t=1" width="419px" />](https://spatie.be/github-ad-click/laravel-translation-loader)

We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us).
We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can
support us by [buying one of our paid products](https://spatie.be/open-source/support-us).

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards).
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using.
You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards
on [our virtual postcard wall](https://spatie.be/open-source/postcards).

## Installation

Expand All @@ -48,21 +58,26 @@ Spatie\TranslationLoader\TranslationServiceProvider::class,
You must publish and run the migrations to create the `language_lines` table:

```bash
php artisan vendor:publish --provider="Spatie\TranslationLoader\TranslationServiceProvider" --tag="migrations"
php artisan vendor:publish --provider="Spatie\TranslationLoader\TranslationServiceProvider" --tag="translation-loader-migrations"
php artisan migrate
```

Optionally you could publish the config file using this command.
Optionally, you could publish the config file using this command.

```bash
php artisan vendor:publish --provider="Spatie\TranslationLoader\TranslationServiceProvider" --tag="config"
php artisan vendor:publish --provider="Spatie\TranslationLoader\TranslationServiceProvider" --tag="translation-loader-config"
```

This is the contents of the published config file:

```php
return [

/*
* The name of the table in which the language lines are stored.
*/
'table_name' => 'language_lines',

/*
* Language lines will be fetched by these loaders. You can put any class here that implements
* the Spatie\TranslationLoader\TranslationLoaders\TranslationLoader-interface.
Expand All @@ -85,23 +100,26 @@ return [
];
```

> **Note:** publishing assets doesn't work out of the box in Lumen. Instead you have to copy the files from the repo.
> **Note:** publishing assets doesn't work out of the box in Lumen. Instead, you have to copy the files from the repo.

## Usage

You can create a translation in the database by creating and saving an instance of the `Spatie\TranslationLoader\LanguageLine`-model:
You can create a translation in the database by creating and saving an instance of
the `Spatie\TranslationLoader\LanguageLine`-model:

```php
use Spatie\TranslationLoader\LanguageLine;

LanguageLine::create([
'group' => 'validation',
'key' => 'required',
'text' => ['en' => 'This is a required field', 'nl' => 'Dit is een verplicht veld'],
'namespace' => '*',
'group' => 'validation',
'key' => 'required',
'text' => ['en' => 'This is a required field', 'nl' => 'Dit is een verplicht veld'],
]);
```

You can fetch the translation with [Laravel's default `__` function](https://laravel.com/docs/localization#retrieving-translation-strings):
You can fetch the translation
with [Laravel's default `__` function](https://laravel.com/docs/localization#retrieving-translation-strings):

```php
__('validation.required'); // returns 'This is a required field'
Expand All @@ -111,15 +129,19 @@ app()->setLocale('nl');
__('validation.required'); // returns 'Dit is een verplicht veld'
```

You can still keep using the default language files as well. If a requested translation is present in both the database and the language files, the database version will be returned.
You can still keep using the default language files as well. If a requested translation is present in both the database
and the language files, the database version will be returned.

If you need to store/override json translation lines, just create a normal LanguageLine with `group => '*'`.
If you need to store/override JSON translation lines, just create a normal LanguageLine with `group => '*'`.

## Creating your own translation providers

This package ships with a translation provider than can fetch translations from the database. If you're storing your translations in a yaml-file, a csv-file, or ... you can easily extend this package by creating your own translation provider.
This package ships with a translation provider that can fetch translations from the database.
If you're storing your translations in a yaml-file, a csv-file, etc.,
you can easily extend this package by creating your own translation provider.

A translation provider can be any class that implements the `Spatie\TranslationLoader\TranslationLoaders\TranslationLoader`-interface. It contains only one method:
A translation provider can be any class that implements
the `Spatie\TranslationLoader\TranslationLoaders\TranslationLoader`-interface. It contains only one method:

```php
namespace Spatie\TranslationLoader\TranslationLoaders;
Expand All @@ -129,15 +151,15 @@ interface TranslationLoader
/*
* Returns all translations for the given locale and group.
*/
public function loadTranslations(string $locale, string $group): array;
public function loadTranslations(string $locale, string $group, string|null $namespace = null): array;
}
```

Translation providers can be registered in the `translation_loaders` key of the config file.

## Changelog

Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.
Please see [CHANGELOG](CHANGELOG.md) for more information about what has changed recently.

## Testing

Expand All @@ -151,7 +173,8 @@ Please see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTI

## Security

If you've found a bug regarding security please mail [security@spatie.be](mailto:security@spatie.be) instead of using the issue tracker.
If you've found a bug regarding security, please mail [security@spatie.be](mailto:security@spatie.be) instead of using
the issue tracker.

## Credits

Expand Down
Loading