Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Gabotronix's avatar

Google Cloud Translate php API returning unicode characters instead of the characters themselves

I'm using Cloud Translate PHP sdk to translate strings from a JSON file, the problem is it returns the translated strings with unicode characters instead of the real characters, so I have to search and replace each one which is quite a hassle...

Example:

"Exp\u00e9riences"

Is there a way to return the characters instead of the weird unicode thingy?

My method:

public function translateJsonFile(Request $request)
    {
        $htmlString = '';
        $source = 'en';
        $target = 'it';

        $key = 'My key';

        $translate = new TranslateClient([
            'key' => $key
        ]);

        //$trans = new GoogleTranslate();

        $newArray = [];
        $json = File::get(base_path() . '/resources/lang/english.json');
        $array = json_decode($json, true);

        foreach ($array as $key => $value) 
        {
            //$result = $trans->translate($source, $target, $value);

            $text_translated = $translate->translate($value, [
                'source' => $source,
                'target' => $target
            ])['text'];

            $newArray[$key] = $text_translated;
            //sleep(1);
            usleep( 500000 );
        }

        $fileName = '/temp/translated-'.rand(1,1000).'.json';

        Storage::disk('public')->put($fileName, json_encode($newArray));

		return response()->json([
			'newArray' => $newArray,
        ]);
    }
0 likes
0 replies

Please or to participate in this conversation.