File size: 5,539 Bytes
d2897cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php

namespace Mautic\CoreBundle\Model;

use Mautic\CoreBundle\Entity\TranslationEntityInterface;
use Mautic\LeadBundle\Entity\Lead;
use Symfony\Component\HttpFoundation\Request;

/**
 * Provides helper methods for determine the requested language from contact's profile and/or request.
 */
trait TranslationModelTrait
{
    /**
     * Get the entity based on requested translation.
     *
     * @param Lead|array|null $lead
     *
     * @return array[$parentEntity, TranslationEntityInterface $entity]
     */
    public function getTranslatedEntity(TranslationEntityInterface $entity, $lead = null, Request $request = null): array
    {
        [$translationParent, $translationChildren] = $entity->getTranslations();

        $leadPreference = $chosenLanguage = null;

        if (count($translationChildren)) {
            if ($translationParent) {
                $translationChildren = $translationParent->getTranslationChildren();
            } else {
                $translationParent = $entity;
            }

            // Generate a list of translations
            $translations = [$translationParent->getId() => $translationParent->getLanguage()];
            foreach ($translationChildren as $c) {
                $translations[$c->getId()] = $c->getLanguage();
            }

            // Generate a list of translations for this entity
            $translationList = [];
            foreach ($translations as $id => $language) {
                $core = $this->getTranslationLocaleCore($language);
                if (!isset($translationList[$core])) {
                    $translationList[$core] = [];
                }
                $translationList[$core][$language] = $id;
            }

            // Get the contact's preferred language if defined
            $languageList   = [];
            $leadPreference = null;
            if ($lead) {
                if ($lead instanceof Lead) {
                    $languageList[$leadPreference] = $lead->getPreferredLocale();
                } elseif (is_array($lead) && isset($lead['preferred_locale'])) {
                    $languageList[$leadPreference] = $lead['preferred_locale'];
                }
            }

            // Check request for language
            if (null !== $request) {
                $browserLanguages = $request->server->get('HTTP_ACCEPT_LANGUAGE');
                if (!empty($browserLanguages)) {
                    $browserLanguages = explode(',', $browserLanguages);
                    foreach ($browserLanguages as $language) {
                        if (($pos = strpos($language, ';q=')) !== false) {
                            // remove weights
                            $language = substr($language, 0, $pos + 1);
                        }
                        // change - to _
                        $language = str_replace('-', '_', $language);

                        if (!isset($languageList[$language])) {
                            $languageList[$language] = $language;
                        }
                    }
                }
            }

            $matchFound    = false;
            $preferredCore = false;
            foreach ($languageList as $language) {
                $core = $this->getTranslationLocaleCore($language);
                if (isset($translationList[$core])) {
                    // Does the dialect exist?
                    if (isset($translationList[$core][$language])) {
                        // There's a match
                        $matchFound     = $translationList[$core][$language];
                        $chosenLanguage = $language;
                        break;
                    } elseif (!$preferredCore) {
                        // This will be the fallback if no matches are found
                        $preferredCore = $core;
                    }
                }
            }

            if ($matchFound) {
                // A translation was found based on language preference
                $entity = ($matchFound == $translationParent->getId()) ? $translationParent : $translationChildren[$matchFound];
            } elseif ($preferredCore) {
                // Return the best matching language
                $bestMatch      = array_values($translationList[$preferredCore])[0];
                $entity         = ($bestMatch == $translationParent->getId()) ? $translationParent : $translationChildren[$bestMatch];
                $chosenLanguage = $preferredCore;
            }
        }

        // Save the preferred language to the lead's profile
        if (!$leadPreference && !empty($chosenLanguage) && $lead instanceof Lead) {
            $lead->addUpdatedField('preferred_locale', $chosenLanguage);
        }

        // Return the translation parent and translated entity
        return [$translationParent, $entity];
    }

    /**
     * Run post saving a translation aware entity.
     */
    public function postTranslationEntitySave(TranslationEntityInterface $entity): void
    {
        // If parent, add this entity as a child of the parent so that it populates the list in the tab (due to Doctrine hanging on to entities in memory)
        if ($translationParent = $entity->getTranslationParent()) {
            $translationParent->addTranslationChild($entity);
        }
    }

    /**
     * @return string
     */
    protected function getTranslationLocaleCore($locale)
    {
        if (str_contains($locale, '_')) {
            $locale = substr($locale, 0, 2);
        }

        return $locale;
    }
}