jbernavaprah / lighthouse-translation
Translation support for Laravel Lighthouse
v1.0.1
2022-06-18 14:29 UTC
Requires
- php: ^8.1
- laravel/framework: ^9.0
- nuwave/lighthouse: ^5.5
- spatie/data-transfer-object: ^3.7
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.0
- mockery/mockery: ^1.5
- nunomaduro/larastan: ^2.0
- orchestra/testbench: ^7.0
- orchestra/testbench-core: ^7.0
- phpoption/phpoption: ^1.8
- phpstan/phpstan-mockery: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- phpunit/phpunit: ^9.5
- thecodingmachine/safe: ^2.0
README
Is not PRODUCTION READY.
Requirements
Installation
1. Install using composer:
composer require jBernavaPrah/lighthouse-translation
2. UseTranslation
Apply the JBernavaPrah\Translation\UseTranslation
trait to your models.
use JBernavaPrah\UseTranslation ; use Illuminate\Database\Eloquent\Model; /** * @property \JBernavaPrah\LighthouseTranslation\Translate $name * */ class Item extends Model { use UseTranslation; public function translationColumns(): array{ return ["name"] } }
3. Declare field type as Translate
on GraphQL schema
type Item { name: Translate! # ... }
Usage
Enable Translation
The directive localize
accepts as a parameter the lang to use to return the
localized data.
If is not founded on the column, null is returned.
query { item(id: 1) @localize(lang: "en") { name { ...on Localized{ lang text } } } }
In case the directive is not used on that query/mutation, the RawTranslation type is returned.
query { item(id: 1) { name { ... on RawTranslation { data { lang text } } } } }
Credits:
A lot of ideas came thanks to daniel-de-wit/lighthouse-sanctum. Big thanks to him and his beautiful code!
Also the authors of nuwave/lighthouse did a great job on the documentation and code.