定制 devture/symfony-translation-bundle 二次开发

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

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

devture/symfony-translation-bundle

Composer 安装命令:

composer require devture/symfony-translation-bundle

包简介

Symfony bundle that allows .json files to be translated between multiple languages.

README 文档

README

Web UI for managing Symfony bundles' translation files inside the actual web application.

This is a port of the similarly named Silex bundle: devture/silex-translation-bundle

Finds all source language files (example: messages.en.json, another-domain.en.json) in a given list of base directories and allows these files to be translated to all given languages.

A {translationDomain}.{targetLanguage}.json file is generated and saved next to {translationDomain}{sourceLanguage}.json for each locale, whenever the translations for it are saved.

A {translationDomain}{targetLanguage}.hash.json file is also saved in the same directory. It contains "hints" telling the translation system which source translation string a given translation is derived from. This is so that a translation can be considered outdated if the source translation string changes. At this moment, outdated translations are automatically marked as untranslated in the web UI (that is to say, they are not marked as "already translated, but outdated", but simply as "not translated").

Installation

Install through composer (composer require --dev devture/symfony-translation-bundle).

Add to config/bundles.php:

Devture\Bundle\TranslationBundle\DevtureTranslationBundle::class => ['dev' => true],

Permissions

Since the translation system needs to save translation files in the project, we need to grant file-writing privileges to the web server user.

Example:

$ find /srv/http/my-project/src -type d -name translations | xargs chown :http
$ find /srv/http/my-project/src -type d -name translations | xargs chmod g+w

Configuration

You most likely want this bundle active only for your development (dev) environment. Thus, you can drop the following routing config in config/packages/dev/devture_translation.yaml

devture_translation:
    source_language_locale_key: en
    paths_to_translate:
        - "%kernel.project_dir%/src"
        - "%kernel.project_dir%/translations"
    locales:
        - {"key": "en", "name": "English"}
        - {"key": "ja", "name": "Japanese"}
    twig_layout_path: "base.html.twig"

locales needs to contain all languages that the translation system should be active for (including the source language).

Multiple paths can be specified in paths_to_translate. Each is scanned for files matching the following pattern: */translations/<some translation domain>.<source_language_locale_key>.json.

twig_layout_path is the path to your layout file, which would contain the translation system. The only requirement is that it defines a content block. The translation system would render within it.

Example layout file:

<!doctype html>
<html>
	<body>
		<h1>Website</h1>
		{% block content %}{% endblock %}
	</body>
</html>

Routing example

You most likely want this bundle active only for your development (dev) environment. Thus, you can drop the following routing config in config/routes/dev/DevtureTranslationBundle.yaml:

DevtureTranslationBundleWebsite:
    prefix: /{_locale}/translation
    resource: "@DevtureTranslationBundle/Resources/config/routes/website.yaml"
    requirements:
        _locale: "en|ja"

The Web UI is available at the devture_translation.manage route.

Styling

This bundle relies on Bootstrap v4 for styling. Unless you install and include it (somewhere in your twig_layout_path template), things would look ugly.

Additionally, you can make the pages look prettier by including a flag icon for each language somewhere in your layout or CSS file.

<style>
	.devture-translation-flag {
		border: 1px solid #dbdbdb;
		width: 20px;
		height: 13px;
		display: inline-block;
		vertical-align: text-top;
	}
	.devture-translation-flag.en {
		background: url('/images/flag/en_US.png') no-repeat;
	}
	.devture-translation-flag..ja {
		background: url('/images/flag/ja_JP.png') no-repeat;
	}
</style>

devture/symfony-translation-bundle 适用场景与选型建议

devture/symfony-translation-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 818 次下载、GitHub Stars 达 3, 最近一次更新时间为 2018 年 01 月 26 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 devture/symfony-translation-bundle 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2018-01-26