jbernavaprah/lighthouse-translation

Translation support for Laravel Lighthouse

v1.0.1 2022-06-18 14:29 UTC

This package is auto-updated.

Last update: 2024-12-18 19:50:00 UTC


README

Software License GitHub Tests Action Status Coverage Status PHPStan Latest Version on Packagist Total Downloads

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.