The issue is that the contents of the .js file are not valid JSON. The code is trying to parse the contents of the .js file as JSON, but it is not valid JSON. Instead, the contents of the .js file are in the format of a JavaScript object. To fix this, you can use a regular expression to extract the contents of the object and then use the json_encode function to convert it to JSON.
Here's an updated version of the code:
// Read the contents of the .js file
$contents = File::get(base_path() . '/resources/templang/es.js');
// Extract the contents of the object
preg_match('/{.*}/s', $contents, $matches);
$contents = $matches[0];
// Parse the contents as JSON
$data = json_decode($contents, true);
// Write the data to the .json file
$fileName = '/temp/transformed-'.rand(1,1000).'.json';
Storage::disk('public')->put($fileName, json_encode($data, JSON_PRETTY_PRINT));
The regular expression /{.*}/s matches everything between the first { and the last } in the file. The s modifier at the end of the regular expression allows the . character to match newlines.