dejodev/laravel-multi-language-strings 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

dejodev/laravel-multi-language-strings

Composer 安装命令:

composer require dejodev/laravel-multi-language-strings

包简介

Laravel Package that adds a Multilanguage string class and an Attribute cast for adding language support to Models.

README 文档

README

The Laravel Multi Language Strings package allows you to easily store and manage multilanguage strings in a JSON field within a database table. It provides a MultiLanguageString class and an attribute cast for Eloquent models, which are aware of Laravel locale and fallback locale settings. This package simplifies the process of setting and retrieving multilanguage strings, ensuring seamless localization support in your Laravel applications.

Installation

Install the package via composer:

  composer require dejodev/laravel-multi-language-strings

Add a JSON field to your database migration to store the multilanguage strings.

    public function up(): void
    {
        Schema::create('products', function (Blueprint $table) {
            $table->id();
            $table->json('productName'); // Add this line
            $table->timestamps();
        });
    }

Add the MultiLanguageString class to your model's $casts property.

<?php

namespace App\Models;

use DeJoDev\MultiLanguageStrings\MultiLanguageString;
use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    protected $casts = [
        'productName' => MultiLanguageString::class,
    ];
}

Usage

You can now use the MultiLanguageString class to store multilanguage strings in your Eloquent models.

$product = new Product();

// Set the value for the current locale
$product->productName = 'Product name';

// Or use the set method
$product->productName->set('Product name');

// Set the value for a specific locale
$product->productName->set('Product name', 'nl'); 

// Set the value for all locales a once. (all existing locales will be cleared)
$product->productName->set([
    'en' => 'Product name',
    'nl' => 'Product naam',
]);

// Or set another MultiLanguageString as value
$product->productName = MultiLanguageString::create([
    'en' => 'Product name',
    'nl' => 'Product naam',
]);

// Unset the value for a specific locale
$product->productName->unset('nl');

To get a multilanguage string use the get method.

$product = Product::find(1);

// Get the value for the current locale, if the value is not set for the
// current locale the value for the fallback locale will be returned.
$product->productName->get();

// Get the value for a specific locale
$product->productName->get('nl');

// Multilanguage strings implements Stringable and can be cast to a string.
$s = (string) $product->productName; 
print($product->productName); 

// Alias for get();
$product->productName->toString();

Advanced usage

The MultiLanguageString class is a wrapper around an associative array that maps locale codes to string values. The class is aware of the current locale and fallback locale settings in Laravel, and it provides a convenient API for setting and retrieving multilanguage strings.

The MultiLanguageString class can be used independently of Eloquent models.

// Create a new `MultiLanguageString` instance.
$mls = MultiLanguageString::create([
    'en' => 'Hello World!',
    'nl' => 'Hallo Wereld!',
]);

// Create a new `MultiLanguageString` instance from a JSON string.
$mls = MultiLanguageString::fromJson('{"en":"Hello World!","nl":"Hallo Wereld!"}');

// Convert the `MultiLanguageString` instance to a JSON string.
$json = $mls->toJson();

Locale handling

The MultiLanguageString class is aware of the current locale and fallback locale settings in Laravel. By default, the class uses the current locale to get the value of a multilanguage string. If the value is not set for the current locale, the class will use the fallback locale to get the value. You can enable or disable the fallback locale for a specific instance of the MultiLanguageString class.

// Set fallback option explicit upon instance creation().
$mls = MultiLanguageString::create([
    'en' => 'Hello World!',
    'nl' => 'Hallo Wereld!',
], useFallbackLocale: false);

// Set fallback option on existing instance.
$mls->setUseFallbackLocale(false);

// Get fallback option on existing instance. 
$default = $mls->getUseFallbackLocale();

// Set the default value for the `useFallbackLocale` property.
// This will be used for all new instances.
MultiLanguageString::setUseFallBackLocaleDefault(false);

// Get the default value for the `useFallbackLocale` property.
$default = MultiLanguageString::getUseFallBackLocaleDefault();

// Gets the locale that is used to get the value, using the fallback locale when enabled.
$locale = $mls->getUsedLocale(): string;
$locale = $mls->getUsedLocale('nl'): string;

// Gets an array of the locales stored in the MultiLanguageString.
$locales = $mls->getLocales(); // e.g. ['en', 'nl']

(c) 2025 Wouter de Jong

dejodev/laravel-multi-language-strings 适用场景与选型建议

dejodev/laravel-multi-language-strings 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 11 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 02 月 17 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 dejodev/laravel-multi-language-strings 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-02-17