技术支持转实施
Setting up a multilingual site may be a good way to attract new customers to your business or gain more participants in your project. Translating a simple site with a few static pages probably won’t probably be complicated, but more complex PHP web applications may require a lot of work when launching multiple language support. In this article I’ll present different types of content that need to be taken under consideration when internationalizing a site. Read on to get to know how to handle translating them into different languages.
设置多语言站点可能是吸引新客户加入您的业务或吸引更多项目参与者的好方法。 用几个静态页面转换一个简单的网站可能不会很复杂,但是更复杂PHP Web应用程序在启动多语言支持时可能需要进行大量工作。 在本文中,我将介绍在国际化站点时需要考虑的不同类型的内容。 请继续阅读以了解如何处理将它们翻译成不同的语言。
First of all, your site probably contains text content hard coded in your project files that needs to be translated into another languages. Text strings in template files or notification messages that are being handled in a PHP script are examples of such content. If you didn’t plan on going international when writing these parts of code, you will have to walk through each line and handle translating the text strings stored there.
首先,您的站点可能包含在项目文件中经过硬编码的文本内容,需要将其翻译成其他语言。 模板文件中的文本字符串或PHP脚本中正在处理的通知消息就是此类内容的示例。 如果您在编写这些代码部分时不打算走向国际,则必须遍历每一行并处理翻译存储在其中的文本字符串。
But how should the actual translations be done? The common way of handling multiple language content is to use Gettext – a software solution created to handle translations in applications written in different programming languages. It is also available in PHP as a separate extension. Using Gettext allows you to separate the app translations from the source code. In result, the person responsible for preparing the translations doesn’t have to dig into the code and can work independently from a web developer. When the translations are ready, they are being put in a separate file which is being read by the PHP script. The application matches the translations with source text strings stored in the code and finally the end user can see a site being displayed in the right language. What you need to do as a developer is to transform plain text strings in a PHP application into strings that can be handed by Gettext. You have to wrap the text in the gettext() function which is commonly accessed using the alias _() (the function name is an underscore). By doing this, you specify that a certain text string needs to be translated and can be handled by Gettext. If you want to get to know the details on how to manage Gettext translations in a PHP application, there is a complete tutorial explaining the subject in details, written by Abdullah Abouzekry: Localizing PHP Applications “The Right Way”. I encourage you to read it to see some practical examples of the logic I’ve described above. Also remember that if you’re using a framework as a base for your PHP application, it probably has a component responsible for managing the translations. Review your framework’s documentation to check if there are any tools that simplify handling the multiple language support.
但是实际的翻译应该如何完成? 处理多种语言内容的常用方法是使用Gettext ,这是一种软件解决方案,旨在处理以不同编程语言编写的应用程序中的翻译。 它也可以作为单独的扩展在PHP中使用。 使用Gettext可使您将应用程序翻译与源代码分开。 因此,负责准备翻译的人员不必深入研究代码,并且可以独立于Web开发人员工作。 翻译准备就绪后,会将它们放入一个单独的文件中,该文件由PHP脚本读取。 该应用程序将翻译与代码中存储的源文本字符串进行匹配,最终最终用户可以看到以正确语言显示的网站。 作为开发人员,您需要做的是将PHP应用程序中的纯文本字符串转换为Gettext可以处理的字符串。 您必须将文本包装在gettext()函数中 ,该函数通常使用别名_()访问(函数名是下划线)。 这样,您可以指定某个文本字符串需要翻译,并且可以由Gettext处理。 如果您想了解有关如何在PHP应用程序中管理Gettext转换的详细信息,请阅读Abdullah Abouzekry撰写的完整教程,其中详细介绍了该主题:“ 本地化PHP应用程序的正确方法” 。 我鼓励您阅读它,以查看上述逻辑的一些实际示例。 还要记住,如果您使用框架作为PHP应用程序的基础,则它可能具有负责管理翻译的组件。 查看框架的文档,以检查是否有任何工具可以简化对多语言支持的处理。
In more complex applications, a large part of the site’s content may be stored in a database. If the site supports only one language, you just need to save one version of a resource which is being fetched when needed. But implementing a multiple language site requires you to change the way of storing the data in a database. Language-specific resources now have to be identified by a language code and have to be fetched in the language version set by a visitor. What it means to you as a developer, is that you will probably have to change your database structure to be able to handle the translations.
在更复杂的应用程序中,网站内容的很大一部分可能存储在数据库中。 如果该站点仅支持一种语言,则只需保存一种版本的资源,该版本将在需要时提取。 但是实现多语言站点要求您更改将数据存储在数据库中的方式。 现在,必须通过语言代码来标识特定于语言的资源,并且必须以访问者设置的语言版本来获取特定于语言的资源。 对于开发人员来说,这意味着您可能必须更改数据库结构才能处理翻译。
To get to know what are the best practices on how to create a database structure for a multiple language site, you may just look at the solutions that are being used by different frameworks. Some frameworks offer behaviors (modules that provide an additional functionality to models) responsible for handling the translations of a specified model. After attaching a translation behavior to a model, the framework will write and read different language versions of data using a database structure that is capable of handling multiple language support.
要了解有关如何为多语言站点创建数据库结构的最佳实践,您可以查看不同框架使用的解决方案。 一些框架提供负责处理指定模型转换的behaviors (为模型提供附加功能的模块)。 在将翻译行为附加到模型后,框架将使用能够处理多种语言支持的数据库结构来写入和读取数据的不同语言版本。
For example, the CakePHP framework offers the Translate behavior that may be attached to your models. To do so, you need to specify which model fields are language-dependent. Let’s imagine you have the articles table which has two such fields: title and text:
例如, CakePHP框架提供了可能附加到模型的Translate行为 。 为此,您需要指定哪些模型字段与语言有关。 让我们想象一下,您的articles表具有两个这样的字段: title和text :
To store the translations, you need to move these language-dependent fields into a separate translations table. Each row in the translations table is identified by the language version, the model and the field name referring to the base table. So the articles table will now look as follows:
要存储翻译,您需要将这些与语言相关的字段移到单独的翻译表中。 翻译表中的每一行都由语言版本,模型和引用基本表的字段名称标识。 因此, articles表现在将如下所示:
The article title and text has been moved to a separate table that holds the translations. As you can see, a single translation of each of these fields is stored in a separate row:
文章标题和文本已移至保存翻译的单独表格中。 如您所见,每个这些字段的单个转换存储在单独的行中:
What is most important, the CakePHP framework handles the database reads and writes of the translated data automatically. After attaching the Translate behavior, you can call your model methods just as before and the framework links the main table with the translations table itself. You don’t have to worry about making joins in your select statements or inserting data into multiple tables when saving a new row. Just browse the documentation to see some code examples of handling translations this way.
最重要的是,CakePHP框架自动处理数据库对已翻译数据的读写。 附加了Translate行为后,您可以像以前一样调用模型方法,并且框架将主表与translations表本身链接起来。 保存新行时,您不必担心在select语句中进行联接或将数据插入到多个表中。 只需浏览文档即可查看以这种方式处理翻译的一些代码示例。
Other frameworks often offer similar solutions like the one described above. If you’re using the Symfony2 framework with the Doctrine ORM, just check the Doctrine translatable behavior to see an example. If your site doesn’t run on any framework, you can refer to the solutions described above when implementing your own way of handling the translations in a database.
其他框架通常提供类似上述解决方案的解决方案。 如果您将Symfony2框架与Doctrine ORM一起使用 ,只需检查Doctrine可翻译行为以查看示例。 如果您的站点未在任何框架上运行,则可以在实现自己的数据库处理翻译方式时参考上述解决方案。
If your site allows the visitors to write comments or reviews connected to a post or a product, you will have to handle the translations of such content as well.
如果您的网站允许访问者撰写与帖子或产品相关的评论或评论,则您也必须处理此类内容的翻译。
First of all, you need to save the language version of the content entered by the user when he/she submits a form. You will probably just have to assume that it equals to the language version of the site set by the user. You can also employ an external API (e.g. the Google Translate API) to detect the language of a specific text.
首先,您需要保存用户提交表单时输入的内容的语言版本。 您可能只需要假设它等于用户设置的网站的语言版本即可。 您还可以使用外部API(例如Google Translate API )来检测特定文本的语言。
Then, you need to translate the given text into all the language versions that are supported by your site. Instead of doing it manually, you can just use an external API that will provide you with machine translations of the content submitted by the visitors of your site. The ProgrammableWeb site lists over 60 translation APIs available over the Web so you will probably easily find a solution that suits your needs. If you’re looking for a specific tutorial on how to implement a translation API in an PHP application, I encourage you to read my articles on the Google Translate API: – Using Google Translate API with PHP which explains the basics on integrating a PHP script with the API, – Auto-translating User Submitted Content Using Google Translate API, which contains a complete code example on how to handle translating user submitted content on a PHP website.
然后,您需要将给定的文本翻译成站点支持的所有语言版本。 无需手动进行操作,您只需使用外部API,即可为您的网站访问者提交的内容提供机器翻译。 ProgrammableWeb站点列出了 Web上提供的60多种翻译API ,因此您可能会轻松地找到适合您需求的解决方案。 如果您正在寻找有关如何在PHP应用程序中实现翻译API的特定教程,建议您阅读有关Google Translate API的文章:– 将Google Translate API与PHP结合使用 ,其中介绍了集成PHP脚本的基础知识使用API,– 使用Google Translate API自动翻译用户提交的内容 ,其中包含有关如何处理PHP网站上的用户提交的内容翻译的完整代码示例。
Of course you may choose not to translate the user submitted content and display it in the original language versions. But in my opinion displaying machine translations is better than displaying content in a different language, even if the translations fetched from the APIs aren’t perfect.
当然,您可以选择不翻译用户提交的内容并以原始语言版本显示。 但我认为,即使从API提取的翻译并不完美,显示机器翻译也比以其他语言显示内容更好。
As your site probably consists not only of the text content, you will also have to handle the translations of various resources that are being shared in your webpages. Images, videos, attachments, PDF files – all of these types of content have to be handled as well. To simplify the way of displaying the proper version of a file at a webpage, you can store all the files in a directory structure that reflects the languages available on the site. The English version of a file will be stored under en directory, the French version under fr and so on. Then you can write a simple helper method that will fetch the proper file basing on the language version that is currently being set by a user. If there is a default version of a specific resource, you can extend the method and fetch the default file if the language-specific version hasn’t been found.
由于您的网站可能不仅包含文本内容,还必须处理网页中共享的各种资源的翻译。 图片,视频,附件,PDF文件–所有这些类型的内容也必须处理。 为了简化在网页上显示文件正确版本的方式,您可以将所有文件存储在反映网站上可用语言的目录结构中。 英文版本的文件将存储在en目录下,法语版本的存储在fr目录下,依此类推。 然后,您可以编写一个简单的帮助程序方法,该方法将根据用户当前正在设置的语言版本来获取适当的文件。 如果存在特定资源的默认版本,则可以扩展该方法并在未找到特定语言版本的情况下获取默认文件。
Handling the translations of the types of content described above is often not sufficient to end up with a site that supports different language versions completely. Remember that a PHP website often includes other sources of content. JavaScript code that modifies the DOM tree or displays notification messages often contains text strings that have to be translated. Also don’t forget setting the proper language version when using external APIs or widgets that are attached to your webpages (e.g. social plugins). Browsing through your site before implementing multiple language support may provide you with more types of content that have to be taken under consideration when preparing translations.
处理上述类型的内容的翻译通常不足以最终得到一个完全支持不同语言版本的站点。 请记住,PHP网站通常包含其他内容来源。 修改DOM树或显示通知消息JavaScript代码通常包含必须翻译的文本字符串。 另外,在使用附加到您网页上的外部API或小部件(例如社交插件)时,请不要忘记设置正确的语言版本。 在实施多语言支持之前浏览您的网站可能会为您提供准备翻译时必须考虑的更多类型的内容。
As you can see, launching a site that offers multiple language support requires you to handle translating different types of content in different ways. If you plan to translate your site, I encourage you to make a checklist of the types of the translations that need to be done. It may serve as a good starting point for assessing the amount of work that needs to be done and implementing the translations.
如您所见,要启动提供多种语言支持的网站,您需要以不同的方式来处理不同类型的内容。 如果您打算翻译您的网站,建议您对需要完成的翻译类型进行核对。 它可以作为评估需要完成的工作量和实施翻译的良好起点。
If you have any questions or comments regarding the article, feel free to post them below. You can also contact me through Google Plus.
如果您对本文有任何疑问或意见,请随时在下面发布。 您也可以通过Google Plus与我联系。
翻译自: https://www.sitepoint.com/implementing-multi-language-support/
技术支持转实施
相关资源:jdk-8u281-windows-x64.exe