简体   繁体   English

在PHP webapp中模板化不同的语言

[英]Templating different languages in a PHP webapp

I'm writing a webapp in german, so all buttons,text,tooltips etc. are in german for now. 我正在用德语写一个webapp,所以现在所有按钮,文本,工具提示等都是德语。 But I want to use some kind of template file for the webapp so I can quick change to another language if needed. 但我想为webapp使用某种模板文件,以便我可以根据需要快速更改为其他语言。 I thought about textfile that I explode with "\\n" and load into a sessionvariable to have always all text the user will need in his session. 我想到了我用"\\n" explode文本文件并加载到sessionvariable中,以便始终拥有用户在其会话中需要的所有文本。 An other approach would be to parse such a file ie a XML document like this: 另一种方法是解析这样的文件,即这样的XML文档:

<?xml version="1.0"?>
<phrase>
    <placeholder></placeholder>
    <value></value>
</phrase>

where every field has it's own name/value that represents a textsnippet or button or whatever on the website, and then cast it into an object an cache it for everyone. 其中每个字段都有自己的名称/值,表示文本片段或按钮或网站上的任何内容,然后将其转换为一个对象,为每个人缓存它。 I think the second approach is the best for working with multiple languages for a webapp. 我认为第二种方法是最适合使用多种语言的webapp。 Anybody perhaps some pointers what I could do even better, or just post how you did this kind of languagetemplating for mutlinational webpages/webapps in the past. 任何人都可能会指出我能做得更好的事情,或者只是发布你过去如何使用这种语言模仿mutlinational webpages / webapps。

Take a look at GNU Gettext , its very handy for multilanguage support. 看看GNU Gettext ,它非常方便多语言支持。

The main idea is that you just wrap your words or phrases into a function, like 主要的想法是你只需将你的单词或短语包装成一个函数,比如

echo _g('Hello');

so you do not have any engine changes. 所以你没有任何引擎变化。 You will have to add translation files for each language you are using. 您必须为您正在使用的每种语言添加翻译文件。

Since you are looking for a translation solution, I understand you don't use a framework to develop your site, since most of them provide you with solutions to handle translations. 由于您正在寻找翻译解决方案,我了解您不使用框架来开发您的网站,因为大多数网站都为您提供处理翻译的解决方案。

Most frameworks and apps I've seen in php use arrays, where the original sentence is the key and the translation is the value. 我在php中看到的大多数框架和应用都使用数组,其中原始句子是关键,翻译是价值。 So, to make easier to translate it to several languages, the key is in english. 因此,为了更容易将其翻译成多种语言,关键在于英语。

In case you use gettext as suggested, or another aproach, it'll be useful also to parse your code to catch all strings to be translated automatically, since it can be a mess doing it manually when the base code grows and you want to keep up to date your translations. 如果您按照建议使用gettext或其他方法,那么解析您的代码以捕获所有要自动翻译的字符串也会很有用,因为当基本代码增长并且您想要保留时,手动执行它会很麻烦最新的翻译。

You've come up with 2 solutions for storing the data, but I suggest you need to think further about the architecture and take a more complete view of the lifecycle of each request. 您已经提出了两种用于存储数据的解决方案,但我建议您需要进一步思考架构并更全面地了解每个请求的生命周期。

Regarding architecture: neither solution scales up to describe an extensive vocabulary very well - although for one or 2 pages it will suffice. 关于架构:两种解决方案都不能扩展到很好地描述广泛的词汇 - 尽管一两页就足够了。 The alternative approach, to manage a translation database (such as gettext) which might be overkill - and performs less optimally with small numbers of pages but importantly performance does not deteriorate significantly with large/multiple dictionaries. 替代方法是管理可能过度的翻译数据库(例如gettext),并且对于少量页面执行效果较差,但重要的是,大/多个词典的性能不会显着恶化。 A compromise solution might be to have a dataset for each URL/language (which might be extracted from a consolidated database). 折衷的解决方案可能是为每个URL /语言(可能从统一数据库中提取)提供数据集。

If it were me, I would not use either method you proposed for storing the data: parsing XML creates a sginficant overhead for each page request : using \\n as a delimiter precludes the use of \\n within a translation. 如果是我,我不会使用您建议用于存储数据的任何方法:解析XML会为每个页面请求创建一个明显的开销:使用\\ n作为分隔符会排除在翻译中使用\\ n。 Using a serialized PHP array seems to be the least expensive solution. 使用序列化的PHP阵列似乎是最便宜的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM