定制 sirgrimorum/transarticles 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

sirgrimorum/transarticles

Composer 安装命令:

composer require sirgrimorum/transarticles

包简介

Localization using db table as source for translated text

README 文档

README

Latest Version on Packagist PHP Version Total Downloads License

Database-backed multilingual content for Laravel. Store rich-text articles in the database instead of flat files, retrieve them by locale, expose them to JavaScript, and manage them via the CrudGenerator admin — all with a single helper call.

Features

  • Database-backed translations — articles stored per locale in the articles table
  • Automatic locale fallback — returns any available language when the current locale is missing
  • Dot-notation keysscope.nickname addressing (e.g. help.contact_us)
  • JavaScript exposure — publish a whole scope of articles to a JS global for frontend use
  • Blade directives — render or expose articles without leaving the template
  • trans_article() helper — global function usable anywhere in PHP
  • Seeder generation — dump the current articles table to a seed file with one command
  • CrudGenerator integration — articles can be managed via the CrudGenerator admin when both packages are installed

Requirements

  • PHP >= 8.2
  • Laravel >= 9.0

Installation

composer require sirgrimorum/transarticles

Run migrations

php artisan migrate

This creates the articles table with columns: id, nickname, scope, lang, content, activated, user_id.

Publish configuration (optional)

php artisan vendor:publish --provider="Sirgrimorum\TransArticles\TransArticlesServiceProvider" --tag=config

Publishes config/sirgrimorum/transarticles.php.

Configuration

config/sirgrimorum/transarticles.php

return [
    // Eloquent model used to store articles
    'default_articles_model' => \Sirgrimorum\TransArticles\Models\Article::class,

    // Database column that stores the locale code
    'default_lang_column' => 'lang',

    // Model scope used to find a single article
    'default_findarticle_function_name' => 'findArticle',

    // Model scope used to find a collection of articles
    'default_findarticles_function_name' => 'findArticles',

    // JavaScript global variable for exposed articles
    'default_base_var' => 'translations',
];

Creating Articles

Insert records directly or use the CrudGenerator admin (if installed):

\Sirgrimorum\TransArticles\Models\Article::create([
    'scope'     => 'homepage',
    'nickname'  => 'hero_title',
    'lang'      => 'en',
    'content'   => '<h1>Welcome to our site</h1>',
    'activated' => true,
]);

Retrieving Articles

Facade

use Sirgrimorum\TransArticles\Facades\TransArticles;

// Returns content for the current locale (or fallback)
$html = TransArticles::get('homepage.hero_title');

// Expose a scope as a <script> tag
$script = TransArticles::getjs('homepage');

Helper function

echo trans_article('homepage.hero_title');

Blade directives

{{-- Render an article inline --}}
@transarticles('homepage.hero_title')

{{-- Expose an entire scope to JavaScript --}}
@transarticles_tojs('homepage')

{{-- Custom JS variable name --}}
@transarticles_tojs('homepage', 'pageContent')

JavaScript usage (after @transarticles_tojs)

// window.translations.homepage.hero_title => "<h1>Welcome...</h1>"
document.getElementById('hero').innerHTML = translations.homepage.hero_title;

Locale Fallback Behaviour

  1. Look for an article with lang = App::getLocale() and activated = true
  2. If not found, return any activated article for that scope.nickname
  3. If still not found, return the nickname itself (useful for spotting missing articles)

A warning label is shown when content is returned in a fallback language.

Artisan Commands

transarticles:createseed

Generates a database seed file from the current articles table:

# Seed only the articles table
php artisan transarticles:createseed

# Seed all tables (excluding migrations)
php artisan transarticles:createseed --all

# Force overwrite
php artisan transarticles:createseed --force

The generated seed file is placed in database/seeds/ (or database/seeders/ for Laravel 8+).

API Reference

TransArticles::get()

TransArticles::get(string $nickname): string

Returns the HTML content of the article identified by scope.nickname for the current locale.

TransArticles::getjs()

TransArticles::getjs(
    string $scope,      // Scope to expose (or 'scope.nickname' for a single article)
    string $basevar = '' // JS global variable (defaults to config 'default_base_var')
): string

Returns a <script> tag that assigns all articles in the scope to window.{basevar}.{scope}.

trans_article()

trans_article(string $nickname): string

Global helper — equivalent to TransArticles::get($nickname).

Blade directive — @transarticles

@transarticles(string $nickname)

Blade directive — @transarticles_tojs

@transarticles_tojs(string $scope, string $basevar = '')

License

The MIT License (MIT). See LICENSE.md.

sirgrimorum/transarticles 适用场景与选型建议

sirgrimorum/transarticles 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 275 次下载、GitHub Stars 达 0, 最近一次更新时间为 2018 年 02 月 21 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 sirgrimorum/transarticles 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

与 sirgrimorum/transarticles 相关的其它包

同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-02-21