构建网站翻译文件 [英] Structuring Website Translation files

查看:74
本文介绍了构建网站翻译文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

建立网站时,我几次遇到这个问题. 我将以使用PHP和Laravel为例进行说明,但是这个问题在多个平台之间是很常见的. 这已经在一些问题中得到解决( post2 post3 Laravel 4 Laravel 5 具有相似的本地化功能,至少对于本主题的目的而言足够相似)

本地化结构构成了跨语言文件(en,es,de,fr ...)的内容,其中可以有多个.php文件,这些文件包含一个返回多级字典结构的return语句.

/lang
    /en
        messages.php
    /es
        messages.php

并且文件包含以下内容:

<?php    
return [

    'example1' => 'example message for value exaple-key',
    'example2' => [
        'sub-example' => 'example message for example1.sub.example',
    ],    
];

调用此操作是通过执行以下操作来完成的:

//Laravel 5    
trans('messages.example1'); //outputs 'example message for value exaple-key'
trans('messages.example2.sub-example'); //outputs 'example message for example1.sub.example'

//Laravel 4   
Lang::get('messages.example1'); //outputs 'example message for value exaple-key'
Lang::get('messages.example2.sub-example'); //outputs 'example message for example1.sub.example'

想到一些分组方法:

  1. 通过网站内容

    示例:homepage.php, page1.php, page2.php...

  2. 通过逻辑域:

    示例:auth.php, validation.php, pagination.php...

  3. 通过html:

    示例:buttons.php, popup_messages.php, form_data.php...

  4. 通过笔直翻译:

    示例:simple_words.php, phrases.php...,然后包含诸如'password-to-short' => 'your password is to long'

  5. 之类的内容
  6. 前面提到的一些混合/组合

所有这些都有一些明显的优点和缺点,我不会尝试说明这一点,但是 5th 选项最有可能是最好的解决方案,但是仍然存在划界的问题尽可能减少短语和内容的重复.

另一个问题是如何解决在某些情况下首字母大写和在其他情况下小写以及末尾使用标点符号的问题.

我确实对此问题进行了重新研究,但没有明确的指南和/或可借鉴的好例子.

欢迎所有意见.

解决方案

我倾向于将Laravel应用中的功能分组为独立的组件".例如,我最近一直在为应用程序开发电子邮件活动功能,因此将服务提供商类,模型和服务类放在 app/Email 文件夹中.

牢记这一点,我以类似的方式组织我的翻译.因此,即使在这个项目中我们不翻译字符串,但如果我这样做,我将创建一个 resources/assets/lang/zh-cn/email.php 文件,并在其中放置电子邮件组件的翻译后的字符串

因此在另一个项目中,我的目录结构可能如下所示:

  • /资源
    • /lang
      • /en
        • auth.php
        • email.php
        • events.php
        • news.php
        • pagination.php
        • passwords.php
        • validation.php

希望这会有所帮助.

I faced this problem several times while building websites. I will explain the using PHP and Laravel as an example but this problem is a common amoung multiple platforms. This was already addressed in a few questions (post1, post2,post3, post4 and some others) but the posts didn't really get a good answer.

The question is: What is the best way of structuring translated content inside of language files?

I'm currently using Laravel (I'm not mentioning the version because both Laravel 4 and Laravel 5 have similar localisation functionalities, at least similar enough for the purpouses of this topic).

The localisation structures the content accross language files (en, es,de, fr...) inside which there can be multiple .php files that contain a return statement that returns a multi-level dictionary structure.

/lang
    /en
        messages.php
    /es
        messages.php

and the files contain something like this:

<?php    
return [

    'example1' => 'example message for value exaple-key',
    'example2' => [
        'sub-example' => 'example message for example1.sub.example',
    ],    
];

and calling of this is done by doing something like this:

//Laravel 5    
trans('messages.example1'); //outputs 'example message for value exaple-key'
trans('messages.example2.sub-example'); //outputs 'example message for example1.sub.example'

//Laravel 4   
Lang::get('messages.example1'); //outputs 'example message for value exaple-key'
Lang::get('messages.example2.sub-example'); //outputs 'example message for example1.sub.example'

A few methods of grouping come to mind:

  1. by website content

    example: homepage.php, page1.php, page2.php...

  2. by logical domain:

    example: auth.php, validation.php, pagination.php...

  3. by html:

    example: buttons.php, popup_messages.php, form_data.php...

  4. by straight traslation:

    example: simple_words.php, phrases.php... and than contain content like 'password-to-short' => 'your password is to long'

  5. Some hybrid/combination of the ones mentioned before

All of these have some obvious benefits and drawbacks and I won't try to go int that but the 5th option is most likely the best solution but there's still the problem of where to draw the line to get minimal duplication of phrases and content.

Annother problem is how to solve the problem of uppercase first characters in some cases and lowercase in other cases as well as punctuation characters at the ends.

I did reaserch regarding this problem but there are no definitive guidelines and/or good examples available to learn from.

All opinions are welcome.

解决方案

I tend to group functionality in my Laravel apps into self-contained ‘components’. For example, I’ve been working on email campaign functionality for an application recently so put the service provider class, models, service classes in a folder at app/Email.

Bearing this in mind, I organise my translations in a similar fashion. So even though on this project we’re not translating strings, if we were I would create a resources/assets/lang/en/email.php file, and put translated strings for the email component in there.

So in another project, my directory structure might look like this:

  • /resources
    • /lang
      • /en
        • auth.php
        • email.php
        • events.php
        • news.php
        • pagination.php
        • passwords.php
        • validation.php

Hope this helps.

这篇关于构建网站翻译文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆