您将如何在多语言Laravel应用程序中组织邮件 [英] How would you organize mails in a multilingual Laravel application

查看:56
本文介绍了您将如何在多语言Laravel应用程序中组织邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很好奇如何在多语言应用程序中有效生成电子邮件.

I'm curious about how to effectively generate emails in a multilingual application.

为了使所有答案保持一致:假设您有一本典型的商业新闻,其中包含大量图像,标记,当然还有很多文本段落.假定所有文本都不来自数据库,但应进行硬编码.此外,这些段落中的某些单词可以加粗,强调,...新闻稿将在订阅者的首选语言环境中发送.

For the sake of getting all answers aligned: let's say you have a typical commercial newsletter with lots of images, markup, and of course many textual paragraphs. Assume that all text does NOT come from a database but should be hard-coded. Moreover, some words in these paragraphs can be bolded, emphasized,... The newsletter will be sent in the subscriber's preferred locale.

如何构建一个系统来处理此问题?

How can I build a system to handle this?

  • 您会使用 trans()引用每个段落并在Laravel的 lang 文件夹中定义所有翻译吗?那么单个单词标记部分呢?在语言文件中加入HTML标签对我来说有点不对劲.
  • 还是您会为电子邮件创建单独的语言文件夹(例如,views/emails/en),并让Laravel获取正确的语言文件夹?但是随后可能会存在大量重复的邮件布局定义...
  • 或者也许是完全其他的东西?
  • Would you reference each paragraph with trans() and define all translations in Laravel's lang folder? How about the individual words markup part then? Incorporating HTML tags in language files feels somehow wrong to me.
  • Or would you create separate language folders for emails (e.g. views/emails/en) and make Laravel fetch the right one? But then lots of duplicate mail layout definition will probably exist...
  • Or perhaps something completely else?

推荐答案

具有静态内容的新闻通讯:

For newsletters with static content:

  1. 避免翻译文件
  2. 将模板组合物用于可重复使用的结构元素

Laravel的本地化系统适用于应用程序中的字符串,但是,对于电子邮件中文本繁多的内容,这些定义将难以维护,尤其是对于包含行内标记或定期更改的内容.具有许多 trans()调用的模板的渲染速度也较慢,尤其是在成千上万的电子邮件批量处理时.

Laravel's localization system works well for strings in an application, but, for the text-heavy content in an email, these definitions will be cumbersome to maintain, especially for content that contains inline markup or that changes regularly. Templates with many trans() calls also render more slowly, especially when batching thousands of emails.

如果其他人编写了内容,那么在需要的地方添加一些Blade指令要比将每段文本提取到本地化条目中容易得多.请记住,我们需要为每一个新闻通讯重复这项工作.

If somebody else writes the content, it's much easier to add a few Blade directives where needed than to extract every chunk of text into a localization entry. Remember, we'll need to repeat this work for every newsletter.

相反,只需将内容直接写到模板中,并为翻译成不同语言的消息使用命名层次结构.将结构和显示元素提取到可重用的组件中,以最大程度地减少与内容混合的标记量.

Instead, just write the content directly into the template, and use a naming hierarchy for messages translated to different languages. Extract structural and display elements into reusable components to minimize the amount of markup mixed in with the content.

我们的模板文件层次结构可能如下所示:

Our template file hierarchy might look like this:

resources/views/newsletters/
├── components/
│   └── article_summary.blade.php
├── en/
│   └── 2018-01-01.blade.php
├── es/
│   └── 2018-01-01.blade.php
├── fr/
│   └── 2018-01-01.blade.php
└── newsletter.blade.php

newsletter.blade.php 主模板包含在每个新闻通讯中显示的基本元素,例如页眉,徽标和页脚.它将根据用户的语言环境和发布日期 @include()相应的新闻正文:

The newsletter.blade.php master template contains basic elements displayed in every newsletter, like a header, logo, and footer. It will @include() the appropriate newsletter body based on the user's locale and the date of publication:

{{-- ... common header markup ... --}}

@include("newsletters.$locale.$date")

{{-- ... common footer markup ... --}}

本地化文件夹中的每个模板均包含该语言的消息文本,以及一些内联HTML或标记,用于根据需要设置格式.这是一个示例:

Each template in the localization folders contains the text for the message in that language, with a bit of inline HTML or markdown for formatting as needed. Here's an example:

# Newsletter Title

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc 
finibus sapien nec sapien dictum, vitae dictum dolor mattis. 
Nunc bibendum id augue...

@component('newsletters.components.article_summary', [
    'title' => 'Article Title 1', 
    'fullArticleUrl' => 'http://...',
])
Ut aliquet lobortis leo, cursus volutpat mi convallis ut. 
Vivamus id orci ut quam rhoncus eleifend. Nulla condimentum...
@endcomponent

@component('newsletters.components.article_summary', [
    'title' => 'Article Title 2', 
    'fullArticleUrl' => 'http://...',
])
Donec non finibus tellus, efficitur tincidunt leo. Nam nibh 
augue, consectetur tincidunt dui et, fermentum dictum eros...
@endcomponent

我们可以看到,没有什么可以分散消息内容的注意力. article_summary 组件从消息正文中隐藏标记:

As we can see, there's very little to distract from the content of the message. The article_summary component hides the markup from the message body:

<div class="summary">
    <a href="{{ $fullArticleUrl }}">
        <h2>{{ $title }}</h2>
    </a>

    <p>{{ $slot }}</p>
</div>

通过使用组件,我们可以自由更改每个新闻稿的版式,并且避免重写我们经常使用的标记.Laravel包含用于降价消息的一些有用的内置组件.

By using components, we're free to change the layout in each newsletter, and we avoid rewriting markup that we use often. Laravel includes some useful built-in components for markdown messages.

要发送消息,我们只传递主模板将使用可用语言解析的语言环境和新闻稿日期:

To send the message, we just pass in the locale and the newsletter date that our master template will resolve from the available languages:

public function build() 
{
    return $this->->markdown('newsletters.newsletter')->with([ 
        'locale' => $this->user->locale,
        'date' => '2018-01-01',
    ]);
}

最后一点,我们不需要完全跳过使用 trans().在某些情况下,例如在共享组件中,这可能是有道理的.对于大多数内容,我们可以通过直接在模板中编写内容来获得可读性和可维护性.

As a final note, we don't need to skip using trans() entirely. It may make sense in some cases, such as in shared components. For the majority of the content, we gain readability and maintainability by writing it directly in the template.

这篇关于您将如何在多语言Laravel应用程序中组织邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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