承接 dennis-koster/lighthouse-translatable 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

dennis-koster/lighthouse-translatable

Composer 安装命令:

composer require dennis-koster/lighthouse-translatable

包简介

Adds translatable directive for automatic schema generation for translatable fields

README 文档

README

This package will make generation of GraphQL types for translatable type definitions a breeze. It ships the @translatable directive, which can be applied to any GraphQL type definition.

The directive will look for any attributes of the TranslatableString scalar type, also included in this package, and include it in the types it generates. It will respect nullable states of attributes when generating the type definitions.

The directive can generate two types of GraphQL types, based off the main type the directive gets applied to:

  • A translation type: an individual type definition containing all the attributes that are translatable, along with its locale
  • An input type: an input definition, containing all the attributes that are translatable, along with its locale

Installation

The package can be installed through composer.

composer require dennis-koster/lighthouse-translatable

Package compatibility

The package is completely agnostic of any translations package you might use for saving and retrieving translations to and from the database. This package focuses solely on generating schema definitions.

Its only dependency is on nuwave/lighthouse version 6 or higher.

Arguments

The directive comes with a couple of arguments, all of them optional.

Argument Description Default
generateTranslationType Whether or not to generate a type for the translation definition. Boolean: True
translationTypeName The name of the type to be generated for the translation definition. String: <BaseType>Translation
translationsFieldName The name of the field that holds the array of translations. String: translations
translationsInputName The name of the input that holds the array of translations inputs. String: translations
generateInputType Whether or not to generate a type for the translation input definition. Boolean: True
inputTypeName The name of the type to be generated for the translation input definition. String: <BaseType>TranslationInput
appendInput The inputs the translation model input should be appended to. Array: []

The default values for the arguments may be altered through the publishable config file.

Basic usage

In its most primal form, the directive uses some sensible defaults. It will always generate a translation definition and a translation input definition for the type the directive is applied to.

It will take the name of the base type, in the example below NewsItem and append Translation (so NewsItemTranslation) for the translation definition, and TranslationInput for the translation input definition (NewsItemTranslationInput).

An attribute will be added to the base type definition, holding the translation definitions. By default, the attribute will be called translations but this is customizable through the translationsAttribute argument.

type NewsItem @translatable
{
    id: ID!
    title: TranslatableString!
    introduction: TranslatableString
}

# Will result in the following schema definition
type NewsItem
{
    id: ID!
    title: TranslatableString!
    introduction: TranslatableString
    translations: [NewsItemTranslation!]! # Added through the directive
}

type NewsItemTranslation {
    locale: String!
    title: String!
    introduction: String
}

input NewsItemTranslationInput {
    locale: String!
    title: String!
    introduction: String
}

Append input type to existing inputs

It's possible to provide the directive an array of existing inputs you want to append a translations argument to, through the appendInput argument.

The attribute will be called translations by default, and can be changed through the translationsAttribute argument.

type NewsItem @translatable(appendInput: ["CreateNewsItemInput"])
{
    id: ID!
    slug: String!
    title: TranslatableString!
    introduction: TranslatableString
}

input CreateNewsItemInput {
    slug: String!
}

# Will append the translations attribute to the CreateNewsItemInput
input CreateNewsItemInput {
    slug: String!
    translations: [NewsItemTranslationInput!]!
}

Customize type names and attribute name

It's possible to customize the names of the type definitions that are generated, as well as the name of the attribute it appends to existing type definitions.

type NewsItem @translatable(
    translationTypeName: "FooBarTranslation"
    inputTypeName: "FooBarTranslationInput"
    translationsAttribute: "localizations"
    appendInput: ["CreateNewsItemInput"]
) {
    id: ID!
    title: TranslatableString!
    introduction: TranslatableString
}

input CreateNewsItemInput {
    slug: String!
}

# Will result in the following schema definition
type NewsItem
{
    id: ID!
    title: TranslatableString!
    introduction: TranslatableString
    localizations: [FooBarTranslation!]!
}

input CreateNewsItemInput {
    slug: String!
    localizations: [FooBarTranslationInput!]!
}

type FooBarTranslation {
    locale: String!
    title: String!
    introduction: String
}

input FooBarTranslationInput {
    locale: String!
    title: String!
    introduction: String
}

Configuration

The directive comes with a configuration file that can be published through:

php artisan vendor:publish --provider=DennisKoster\\LighthouseTranslatable\\Providers\\LighthouseTranslatableProvider

Through the configuration file, all the default values for the directive may be adjusted to your liking.

View stubs

The generated types are created using blade views. These blade views can also be published and adjusted to your needs.

Blade view overview

type NewsItem @translatable(appendInput: ["CreateNewsItemInput"])
{
    id: ID!
    title: TranslatableString!
    introduction: TranslatableString
}

input CreateNewsItemInput {
    slug: String!
}

# Will result in the following schema definition
type NewsItem
{
    id: ID!
    title: TranslatableString!
    introduction: TranslatableString
    translations: [NewsItemTranslation!]! # translations-field.blade.php
}

input CreateNewsItemInput {
    slug: String!
    translations: [NewsItemTranslationInput!]! # translations-input.blade.php
}

type NewsItemTranslation {
    locale: String! # translatable-attribute-field.blade.php
    title: String! # translatable-attribute-field.blade.php
    introduction: String # translatable-attribute-field.blade.php
}

input NewsItemTranslationInput {
    locale: String! # translatable-attribute-input.blade.php
    title: String! # translatable-attribute-input.blade.php
    introduction: String # translatable-attribute-input.blade.php
}

dennis-koster/lighthouse-translatable 适用场景与选型建议

dennis-koster/lighthouse-translatable 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.79k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2024 年 09 月 27 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「translations」 「translatable」 「type」 「laravel」 「graphql」 「lighthouse」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 dennis-koster/lighthouse-translatable 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 dennis-koster/lighthouse-translatable 我们能提供哪些服务?
定制开发 / 二次开发

基于 dennis-koster/lighthouse-translatable 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 2.79k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 16
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-09-27