<?php

use Illuminate\Database\Seeder;

class DetectLanguage extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $listFiles = glob(base_path('resources/js/lang') . '/*.js');
        foreach ($listFiles as $file) {
            if (basename($file) == "index.js") continue;
            $langContent = file_get_contents($file);
            $langContent = str_replace("/*{importModuleLang}*/", "", $langContent);
            $langContent = str_replace("export default", "", $langContent);
            $langContent = str_replace(";", "", $langContent);
            $langContent = trim($langContent);
            $langContent = $this->json_decode_nice($langContent);
            print $langContent;

            break;
        }
    }

    function json_decode_nice($json, $assoc = FALSE)
    {
        $json = str_replace(array("\n", "\r"), "", $json);
        $json = preg_replace('/([{,]+)(\s*)([^"]+?)\s*:/', '$1"$3":', $json);
        return json_decode($json, $assoc);
    }
}
