summaryrefslogtreecommitdiff
path: root/src/Helpers/Translation/TranslationServiceProvider.php
blob: 5534a1c3b53e99601eb45e9b75fe3137121a24fa (plain)
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
<?php

namespace Engelsystem\Helpers\Translation;

use Engelsystem\Config\Config;
use Engelsystem\Container\ServiceProvider;
use Gettext\Translations;
use Illuminate\Support\Str;
use Symfony\Component\HttpFoundation\Session\Session;

class TranslationServiceProvider extends ServiceProvider
{
    /** @var GettextTranslator */
    protected $translators = [];

    public function register(): void
    {
        /** @var Config $config */
        $config = $this->app->get('config');
        /** @var Session $session */
        $session = $this->app->get('session');

        $locales = $config->get('locales');
        $locale = $config->get('default_locale');
        $fallbackLocale = $config->get('fallback_locale', 'en_US');

        $sessionLocale = $session->get('locale', $locale);
        if (isset($locales[$sessionLocale])) {
            $locale = $sessionLocale;
        }

        $session->set('locale', $locale);

        $translator = $this->app->make(
            Translator::class,
            [
                'locale'                => $locale,
                'locales'               => $locales,
                'fallbackLocale'        => $fallbackLocale,
                'getTranslatorCallback' => [$this, 'getTranslator'],
                'localeChangeCallback'  => [$this, 'setLocale'],
            ]
        );
        $this->app->singleton(Translator::class, function () use ($translator) {
            return $translator;
        });
        $this->app->alias(Translator::class, 'translator');
    }

    /**
     * @param string $locale
     * @codeCoverageIgnore
     */
    public function setLocale(string $locale): void
    {
        $locale .= '.UTF-8';
        // Set the users locale
        putenv('LC_ALL=' . $locale);
        setlocale(LC_ALL, $locale);

        // Reset numeric formatting to allow output of floats
        putenv('LC_NUMERIC=C');
        setlocale(LC_NUMERIC, 'C');
    }

    /**
     * @param string $locale
     * @return GettextTranslator
     */
    public function getTranslator(string $locale): GettextTranslator
    {
        if (!isset($this->translators[$locale])) {
            $names = ['default', 'additional'];

            /** @var GettextTranslator $translator */
            $translator = $this->app->make(GettextTranslator::class);

            /** @var Translations $translations */
            $translations = $this->app->make(Translations::class);
            foreach ($names as $name) {
                $file = $this->getFile($locale, $name);
                if (Str::endsWith($file, '.mo')) {
                    $translations->addFromMoFile($file);
                } else {
                    $translations->addFromPoFile($file);
                }
            }

            $translator->loadTranslations($translations);

            $this->translators[$locale] = $translator;
        }

        return $this->translators[$locale];
    }

    /**
     * @param string $locale
     * @param string $name
     * @return string
     */
    protected function getFile(string $locale, string $name = 'default'): string
    {
        $filepath = $file = $this->app->get('path.lang') . '/' . $locale . '/' . $name;
        $file = $filepath . '.mo';

        if (!file_exists($file)) {
            $file = $filepath . '.po';
        }

        return $file;
    }
}