This is my very first post and I am quite new to programming (PHP, MYSQL, Javascript, CSS), but I am a fast learner and have been searching for a solution in this forum, but I am not sure about what to do with this problem I need a solution to.
The problem: I am experiencing a lot of trouble with a script I am using (a friend of mine made this and is not available for half a year) that is translating my cloud service.
The cloud service has 4 languages for the users to switch between, which means that I need a solution to translate these with. Everytime I want to use the script to translate something, I get a lot of duplicate entrys. For every user that is using the cloud service and is navigating to different pages, another entry with duplicates of everything - that I have set the script to translate - that those pages hold will be made. As of right now, the table where all the entrys are inserted into, consists of 26459 rows. If there was no duplicates, this table would have about 2000 rows.
The idea is that, if there are any new functions that are showing text of some sort in the webpage, I simply put
<?php echo $translator->{'I can translate this text'} ?>
When the script is active (set to true) and I visit the page that the function is located at, the text data will be inserted into a table in my database that consist of three columns:
value, lang, group
The type of the following columns are (I dunno if you need to know about this): value (type: text ) lang (type: varchar(4) ) group (type: int(9) )
The table is easy to edit with a very simple UI that has been set up in my cloud service. The UI looks like this: http://i.imgur.com/zLgHKV7.png
The translating script looks like this:
<?php
class Translator {
private $lang_file_path = 'application/lang/';
private $lang;
private $data;
public function __construct($lang) {
$this->lang = $lang;
if (file_exists(ROOT.$this->lang_file_path.$this->lang.'.json')) {
$this->data = json_decode(file_get_contents(ROOT.$this->lang_file_path.$this->lang.'.json'));
}
}
public function __get($val) {
// If translate == true, strings that can be translated will be saved into the database.
global $translate;
if( $translate == true)
{
global $globalDB;
$this->db = $globalDB;
$stmt = $this->db->prepare("SELECT `group` FROM language_string ORDER BY `group` DESC LIMIT 0,1");
$stmt->execute(array());
$res = $stmt->fetch(PDO::FETCH_ASSOC);
$nextGroup = $res['group'] + 1;
$stmt = $this->db->prepare("SELECT * FROM language_string WHERE value = ?");
$stmt->execute(array($val));
$res = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$res) {
$stmt = $this->db->prepare("INSERT INTO language_string (`value`, `lang`, `group`) VALUES (?, ?, ?)");
$stmt->execute(array($val, 'en', $nextGroup));
}
}
if (isset($this->data->{$val})) {
return $this->data->{$val};
} else {
return $val;
}
}
}
?>
This means that for every single text string that the script takes care of will be inserted as 5 rows with the only thing that separates them is the following:
eng <<--- This is the original text and thus cannot be edited in the cloud service UI
en <<--- English 2 in the picture
de <<--- Deutch
es <<--- Spanish
sv <<--- Swedish
Now, what I have been trying to figure out is how to stop the script from inserting a lot of duplicates (http://i.imgur.com/kKXlxDa.png) into the table, without making the column "group" unique (since there will always be 5 entrys for every translation and all 5 of those need to belong to the same group so that they can be translated properly in the translation UI).
Im sorry if this post was a bit long, but I think it's best if I dont leave out any details.