Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 51
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
I18nController
0.00% covered (danger)
0.00%
0 / 51
0.00% covered (danger)
0.00%
0 / 3
156
0.00% covered (danger)
0.00%
0 / 1
 index
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 getTranslations
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 1
30
 postTranslations
0.00% covered (danger)
0.00%
0 / 28
0.00% covered (danger)
0.00%
0 / 1
42
1<?php
2
3namespace App\Http\Controllers;
4
5use Illuminate\Http\Request;
6use Illuminate\Http\JsonResponse;
7
8class I18nController extends Controller
9{
10    public function index(Request $request): JsonResponse
11    {
12        $user = $request->user();
13        return response()->json([
14            'result'  => compact('user')
15        ]);
16    }
17
18    /**
19     * @param Request $request
20     * @return JsonResponse
21     */
22    public function getTranslations(Request $request): JsonResponse
23    {
24        // Get all language files in the resources/lang directory
25        $langFiles = glob(resource_path('lang') . '/*/*.php');
26
27        // Specify which files are allowed to be returned
28        $allowedFiles = [
29            'welcome.php',
30        ];
31
32        // Initialize an array to store translations for each language
33        $languages = [];
34
35        // Iterate over each language file
36        foreach ($langFiles as $filePath)
37        {
38            $fileName = basename($filePath);
39
40            // Check if the file is allowed to be returned
41            if (in_array($fileName, $allowedFiles))
42            {
43                // Extract the language from the file path
44                $lang = basename(dirname($filePath));
45
46                // Include the file and get its content as an array
47                $translationArray = include $filePath;
48
49                // Check if the content is an array
50                if (is_array($translationArray))
51                {
52                    // Add the keys and values to the languages array
53                    $fileNameWithoutExtension = pathinfo($fileName, PATHINFO_FILENAME);
54                    foreach ($translationArray as $key => $value)
55                    {
56                        $languages[$fileNameWithoutExtension][$key][$lang] = $value;
57                    }
58                }
59            }
60        }
61
62        // Get the authenticated user
63        $user = $request->user();
64
65        // Return a JSON response with the user information and the translation data
66        return response()->json([
67            'result' => compact('user'),
68            'data' => $languages
69        ]);
70    }
71
72
73    /**
74     * @param Request $request
75     * @return JsonResponse
76     */
77    public function postTranslations(Request $request): JsonResponse
78    {
79        try {
80
81            // Getting the request data
82            $data = $request->input('data');
83
84            // Checking if the data was received correctly
85            if ($data === null)
86            {
87                // Return a JSON with an error code and a message
88                return response()->json([
89                    'error' => true,
90                    'message' => 'Translation data was not provided.'
91                ], 400); // Using status code 400 to indicate a bad request
92            }
93
94            // Decoding special characters in the string
95            $data = urldecode($data);
96
97            // Separating key-value pairs and converting them into an associative array
98            parse_str($data, $dataArray);
99
100            // Initializing an array to store all content by language
101            $languageContents = [];
102
103            // Iterating through all translations
104            foreach ($dataArray as $key => $value)
105            {
106                // Splitting the key into parts using '###-###' as delimiter
107                $parts = explode('###-###', $key);
108
109                // Getting the file name, language, and token
110                $fileName = $parts[0];
111                $language = $parts[1];
112                $token    = $parts[2];
113
114                // Updating the entry for this language in the content array
115                $languageContents[$language][$fileName][$token] = $value;
116            }
117
118            // Now that the content for each language is prepared, it needs to be written to the files
119            foreach ($languageContents as $language => $files)
120            {
121                // Each translation file for the same language
122                foreach ($files as $fileName => $content)
123                {
124                    // Creating the full path for the file
125                    $filePath = resource_path('lang') . '/' . $language . '/' . $fileName . '.php';
126
127                    // Converting the array back to PHP string format
128                    $phpContent = "<?php\n\nreturn " . var_export($content, true) . ";\n";
129
130                    // Writing the content to the file
131                    file_put_contents($filePath, $phpContent);
132                }
133            }
134
135            // If we reached here, everything went well!
136            return response()->json([
137                'result' => 'Translation update successfully done!'
138            ]);
139        } catch (\Exception $ex) {
140            // If we reached here, something went wrong and a JSON with an error code and a message is sent back to the client
141            return response()->json([
142                'error' => true,
143                'message' => $ex->getMessage()
144            ], 500); // Using status code 500 to indicate an error
145        }
146    }
147}