composer require jBernavaPrah/lighthouse-translation
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"]
}
}
type Item {
name: Translate!
# ...
}
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
}
}
}
}
}
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.