1<?php 2// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project 3// 4// All Rights Reserved. See copyright.txt for details and a complete list of authors. 5// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details. 6// $Id$ 7 8 9class TranslationReader 10{ 11 /** 12 * Creates a TranslationReader with the file to be translated. this 13 * class can recognize JSON and CSV files. 14 * @param [type] $filename [description] 15 */ 16 public function __construct($filename) 17 { 18 $this->filename = $filename; 19 } 20 21 /** 22 * Return an associative array with translations. The array key is an 23 * english word and the value is the word translation. 24 * 25 * @return array translations 26 */ 27 public function getArray() 28 { 29 $ext = null; 30 $valid = is_string($this->filename) 31 && file_exists($this->filename) 32 && preg_match('/\.([a-z]{3,})$/', $this->filename, $ext) 33 && ! empty($ext[1]) 34 && method_exists($this, $ext[1] . "Read"); 35 36 if (! $valid) { 37 return null; 38 } 39 40 $method = $ext[1] . "Read"; 41 return call_user_func([$this, $method]); 42 } 43 44 private function csvRead() 45 { 46 $handle = fopen($this->filename, 'r'); 47 $header = fgetcsv($handle); 48 $translations = []; 49 50 $source_index = array_search('en', $header) ?: 0; 51 $target_index = $source_index > 0 ? 0 : 1; 52 53 while (($row = fgetcsv($handle))) { 54 $source = $row[ $source_index ]; 55 $target = $row[ $target_index ]; 56 $translations[ $source ] = $target; 57 } 58 59 return $translations; 60 } 61 62 private function jsonRead() 63 { 64 $content = file_get_contents('temp/' . $_FILES['language_file']['name']); 65 return json_decode($content, true); 66 } 67} 68