The following warnings occurred:
Warning [2] Undefined variable $unreadreports - Line: 26 - File: global.php(961) : eval()'d code PHP 8.2.25 (Linux)
File Line Function
/global.php(961) : eval()'d code 26 errorHandler->error
/global.php 961 eval
/showthread.php 28 require_once





× This forum is read only. As of July 23, 2019, the UserSpice forums have been closed. To receive support, please join our Discord by clicking here. Thank you!

  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Moving Languages to Database
#1
Hey there!

Although when I'm directly editing files I "can" edit the language file to make new stuff, but I am interested in the possibility of moving the languages into the database. Do you think this would be an easy process? What would I do to language.php to get it to pull the information still? Do you think this would be a good idea?

Thanks!
  Reply
#2
I don't see why that would be a problem.

I guess I have 2 thoughts...
1. Are you planning on offering non-English languages? If not, I don't see a major benefit.

2. You could also just do the db language stuff for your own pages and leave userspice stuff alone.

To answer your question, if I were doing the language in the db, I would...
a. Make a db table per page.
b. Give each new language its own row in the database.

Then you could have something where the user stores their preferred language in the user table and then for each page search something like...

Code:
$langQ = $db->query("SELECT * FROM pagename WHERE lang = ?",array($user->data()->lang));
Code:
$lang = $langQ->first();

Then every time you want to insert text, just use the column name

Code:
<?=$lang->colname;?>
  Reply
#3
Mudmin,

One language only. My thought was just that I wouldn't have to hard edit the language file everytime. How can I make certain pages use the DB languages instead of the current languages? Like right now the system knows to grab a language from the language file and it is X line. How do I do this for certain pages to grab from the db?
  Reply
#4
Is the issue that you want to be able to add phrases etc through a web interface instead of editing & uploading the file every time?

If so, either way you're going to have to add some sort of form for adding new phrases, it might just be easier to keep the language.php file and implement a form on an admin page that just writes to the php file on post. There are plenty of pre-existing free PHP scripts that allow you to do this (most do it for config files etc during install, but can be repurposed for this).

There are also scripts that let you edit the files in a WYSIWYG editor, also an option.
  Reply
#5
winterswolf,

My reasoning was wanting to add via a web interface. I like your idea, of writing to the PHP file. Is this a secure way to do it? Is there anything I should be concerned about? Do you know of any scripts that you have on hand?

Thanks so much.
  Reply
#6
This is how I would do it...

For each page, let's say index.php, I would make an index_lang.php

I would select * for the index table in the db (with all the language for that page).

Then I would generate the entire form and all form processing as a foreach loop. Make the column name your label and then echo in the existing text from the db.

It would probably be 15 lines of code to create and update the form for each of the pages and even then, the only thing you'd need to change on each of your lang pages is the table in the select * and update queries.
  Reply
#7
Hmmm, what about making a query that selects the current page name (e.g. index.php) from the db then I never have to change the query.......hmmmmm....?
  Reply
#8
So, this would be if each line of the db is an entire language... To generate the form... Note that you can pass in an array of columns you don't want to show (such as id)

Code:
function generateForm($table,$id, $skip=[]){
Code:
$db = DB::getInstance();
Code:
$fields = [];
Code:
$q=$db->query("SELECT * FROM {$table} WHERE id = ?",array($id));
Code:
$r=$q->first();

Code:
foreach($r as $field => $value) {
Code:
if(!in_array($field, $skip)){
Code:
echo '<div class="form-group">';
Code:
echo '<label for="'.$field.'">'.ucfirst($field).'</label>';
Code:
echo '<input type="text" class="form-control" name="'.$field.'" id="'.$field.'" value="'.$value.'">';
Code:
echo '</div>';
Code:
}
Code:
}
Code:
return true;
Code:
}
Here's the usage...

$t = 'language';
$skip = ['id'];
generateForm($t,$id,$skip);


And here's how you update...
Code:
function updateFields2($post, $skip=[]){
Code:
$fields = [];
Code:
foreach($post as $field => $value) {
Code:
if(!in_array($field, $skip)){
Code:
$fields[$field] = sanitize($post[$field]);
Code:
}
Code:
}
Code:
return $fields;
Code:
}

Here's the usage...
Code:
$skipFields = [];
Code:
$updateFields = updateFields2($_POST, $skipFields);
Code:
$db->update('language',1,$updateFields);
  Reply
#9
I was thinking the same thing about the while making the query based on page name.
  Reply
#10
Now where is this form control coming from? Am I missing something? Sorry I'm just boggled lol.
  Reply


Forum Jump:


Users browsing this thread: 14 Guest(s)