承接 aozisik/translatable 相关项目开发

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

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

aozisik/translatable

Composer 安装命令:

composer require aozisik/translatable

包简介

Translatable is a package that helps you have translatable fields on models. These fields will store your translations in JSON format and will be automatically fetched to your current app locale when you try to reach them. You can also get a specific language of your choice by using certain methods.

README 文档

README

#Laravel Translatable# Build Status Latest Unstable Version License

This package is designed to work with Laravel 4 and Eloquent ORM.

It requires no additional migrations, no changes to your current data models or no significant code revisions. The implementation is very simple and straight forward.

I devised this package as a simple means to enable fields on Eloquent models to be translatable. You just add the TranslatableTrait and you're set.

Go to your composer.json and add the following line

	"aozisik/translatable": "dev-master"

An exemplary implementation:

<?php
use Aozisik\Translatable\TranslatableTrait;

class Books extends \Eloquent {

	use TranslatableTrait;

	protected $guarded = ['id'];
	protected $localizedFields = ['name', 'description'];
	
}

Name and description fields in your Book model are now translatable.

##How does it Work?##

Upon adding the "TranslatableTrait" trait, your model will begin to translate localizedFields on the fly. However, if you have data in the table previously saved without the trait, it might yield unexpected results.

If you are starting from scratch, Translatable works like this:

  • When you access an attribute, say $book->name, translatable will automatically translate it into the activated language*

  • When you set an attribute and the set value is a string, it will be assumed that this string is in the activated language*

  • To store multilingual translations at once, you must assign an array to the attribute. The keys in the array should correspond to language codes.

      $book->name = array('en' => 'Book', 'fr' => 'Le Livre', 'de' => 'Das Buch');
      $book->save;
    

In the database, the name field for this record looks like this: {"en":"Book","fr":"Le Livre","de":"Das Buch"}

Pretty simple! However, please take into account that the designated input length for the field may not be enough to store lengthy strings of JSON.

So rather than VARCHAR(255), you are usually better off using TEXT for localized fields.

How to Get Translations in a Specific Language

You may need to get translations in a specific language other than the activated language at that time.

This can be done like that:

$book->getTranslation('name', 'de); // Das Buch
$book->getTranslation('name', 'en); // Book
$book->getTranslation('name', 'fr); // Le Livre

$book->name; // Book, if App::getLocale() returns "en"    

*Activated language is determined by App::getLocale()

Pros and Cons##

Let's start with pros:

  • No additional tables to store translations,
  • No significant change in code required,
  • No migrations, no configuration, no nothing

Just add the trait to your model and it works out of the box.

However there are some drawbacks

  • Sorting can be a problem as localized fields will basically store json serializations.
  • If you have existing data, you should store them in the same format as this trait does.
  • You may need to turn some VARCHAR's into TEXT's depending on your current database design.

Overall, I believe this to be a very practical approach and it is quite simple to implement.

I am open to suggestions. Pull requests and bug reports are welcomed.

aozisik/translatable 适用场景与选型建议

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-11-19