概述 快速入门 教程 手册 最佳实践 组件 参考 贡献

发布于 2015-08-27 16:31:34 | 383 次阅读 | 评论: 0 | 来源: 网络整理

Internationalization and localization adapt the applications and their contents to the specific region or language of the users. In Symfony this is an opt-in feature that needs to be enabled before using it. To do this, uncomment the following translator configuration option and set your application locale:

# app/config/config.yml
framework:
    # ...
    translator: { fallbacks: ["%locale%"] }

# app/config/parameters.yml
parameters:
    # ...
    locale:     en

翻译项文件格式

The Symfony Translation component supports lots of different translation formats: PHP, Qt, .po, .mo, JSON, CSV, INI, etc.

最佳实践

Use the XLIFF format for your translation files.

Of all the available translation formats, only XLIFF and gettext have broad support in the tools used by professional translators. And since it’s based on XML, you can validate XLIFF file contents as you write them.

Symfony 2.6 added support for notes inside XLIFF files, making them more user-friendly for translators. At the end, good translations are all about context, and these XLIFF notes allow you to define that context.

小技巧

The Apache-licensed JMSTranslationBundle offers you a web interface for viewing and editing these translation files. It also has advanced extractors that can read your project and automatically update the XLIFF files.

翻译项文件的位置

最佳实践

Store the translation files in the app/Resources/translations/ directory.

Traditionally, Symfony developers have created these files in the Resources/translations/ directory of each bundle.

But since the app/Resources/ directory is considered the global location for the application’s resources, storing translations in app/Resources/translations/ centralizes them and gives them priority over any other translation file. This lets you override translations defined in third-party bundles.

翻译项的键名

最佳实践

Always use keys for translations instead of content strings.

Using keys simplifies the management of the translation files because you can change the original contents without having to update all of the translation files.

Keys should always describe their purpose and not their location. For example, if a form has a field with the label “Username”, then a nice key would be label.username, not edit_form.label.username.

翻译项文件的例子

Applying all the previous best practices, the sample translation file for English in the application would be:

<!-- app/Resources/translations/messages.en.xliff -->
<?xml version="1.0"?>
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
    <file source-language="en" target-language="en" datatype="plaintext" original="file.ext">
        <body>
            <trans-unit id="1">
                <source>title.post_list</source>
                <target>Post List</target>
            </trans-unit>
        </body>
    </file>
</xliff>
最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务