Boss needs a pretty simple script, looking for some help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
NewfieBilko
Forum Contributor
Posts: 189
Joined: Sun Jun 06, 2004 6:45 pm
Location: Newfoundland
Contact:

Boss needs a pretty simple script, looking for some help

Post by NewfieBilko »

Hey guys. I only have 2 days left till of employment here, and the boss has asked me to put togther a rather simple script, involving a directory of language files.

In the root there is :

/en_US/
/de/
/it/
/po/
and so on.

There are a couple dozen of these directorys each having rouglhly 15 files or so in each, each of these files is a php file like such:

/en_us/alangfile.php:

Code: Select all

$stringї'action'] = 'Action';
$stringї'activities'] = 'Activities';
$stringї'activity'] = 'Activity';
$string&#1111;'activityclipboard'] = 'Moving this activity: <b>$a</b>';
$string&#1111;'activityiscurrentlyhidden'] = 'Sorry, this activity is currently hidden';
$string&#1111;'activitymodule'] = 'Activity module';
$string&#1111;'activityreport'] = 'Activity report';
$string&#1111;'activityselect'] = 'Select this activity to be moved elsewhere';
$string&#1111;'activitysince'] = 'Activity since $a';
$string&#1111;'add'] = 'Add';
$string&#1111;'addadmin'] = 'Add admin';
$string&#1111;'addcreator'] = 'Add course creator';
$string&#1111;'added'] = 'Added $a';
$string&#1111;'addinganew'] = 'Adding a new $a';
$string&#1111;'addinganewto'] = 'Adding a new $a->what to $a->to';
$string&#1111;'addingdatatoexisting'] = 'Adding data to existing';
$string&#1111;'addnewcategory'] = 'Add new category';
$string&#1111;'addnewcourse'] = 'Add a new course';
$string&#1111;'addnewuser'] = 'Add a new user';
$string&#1111;'address'] = 'Address';
$string&#1111;'addstudent'] = 'Add student';
$string&#1111;'addteacher'] = 'Add teacher';
$string&#1111;'admin'] = 'Admin';
$string&#1111;'adminhelpaddnewuser'] = 'To manually create a new user account';
$string&#1111;'adminhelpassignadmins'] = 'Admins can do anything and go anywhere in the site';
$string&#1111;'adminhelpassigncreators'] = 'Creators can create new courses and teach in them';
$string&#1111;'adminhelpassignstudents'] = 'Go into a course and add students from the admin menu';
$string&#1111;'adminhelpassignteachers'] = 'Find a course then use the icon to add teachers';
$string&#1111;'adminhelpauthentication'] = 'You can use internal user accounts or external databases';
$string&#1111;'adminhelpconfiguration'] = 'Configure how the site looks and works';
$string&#1111;'adminhelpconfigvariables'] = 'Configure variables that affect general operation of the site';
$string&#1111;'adminhelpcourses'] = 'Define courses and categories and assign people to them';
$string&#1111;'adminhelpedituser'] = 'Browse the list of user accounts and edit any of them';
$string&#1111;'adminhelplanguage'] = 'For checking and editing the current language pack';
$string&#1111;'adminhelplogs'] = 'Browse logs of all activity on this site';
$string&#1111;'adminhelpmanagedatabase'] = 'Access the database directly (be careful!)';
$string&#1111;'adminhelpmanagemodules'] = 'Manage installed modules and their settings';
$string&#1111;'adminhelpsitefiles'] = 'For publishing general files or uploading external backups';
$string&#1111;'adminhelpsitesettings'] = 'Define how the front page of the site looks';
$string&#1111;'adminhelpthemes'] = 'Choose how the site looks (colors, fonts etc)';
$string&#1111;'adminhelpusers'] = 'Define your users and set up authentication';
$string&#1111;'administration'] = 'Administration';
$string&#1111;'administrator'] = 'Administrator';
Every language directory has the same php files, each of these are in all directories. The files have all the same variables, but the values are in the specific language.

What i need, is a script that will have a Language selection DropDown, and the page will _GET lang= whateverlang, then set a variable to whichever language it is, so that the page will then load all the variables (aka administror) from the desired directory.

Its quite easy Im sure, but Im not good with coming up with scripts from nothing. Im sure it'll go something like:

Code: Select all

If isset(GET'lang') 
&#123;
$lang= (GET&#1111;lang])
pre_load = $lang/langfile.php
pre_load = $lang/langfile2.php

prep_replace all variables with /lang/ variables

echo $administrator

//Should come out in whatever lang u tell it.

lol
as you cans ee i need some help with this. I hope from my explanation you can understand what i need done.
User avatar
NewfieBilko
Forum Contributor
Posts: 189
Joined: Sun Jun 06, 2004 6:45 pm
Location: Newfoundland
Contact:

Post by NewfieBilko »

I am goping to try and impleiment this code snippet into my code for getting the string var outta the file:?

Code: Select all

function get_string($identifier) &#123;
/// Return the translated string specified by $identifier as
/// for $module.  Uses the same format files as STphp.
/// $a is an object, string or number that can be used
/// within translation strings
///
/// eg "hello \$a->firstname \$a->lastname"
/// or "hello \$a"

    global $course;     /// Not a nice hack, but quick
    if (empty($CFG->courselang)) &#123;
        if (!empty($course->lang)) &#123;
            $CFG->courselang = $course->lang;
        &#125;
    &#125;

    $lang = current_language();

    if ($module == "") &#123;
        $module = "moodle";
    &#125;

    $langpath = "$CFG->dirroot/lang";
    $langfile = "$langpath/$lang/$module.php";

    // Look for the string - if found then return it

    if (file_exists($langfile)) &#123;
        if ($result = get_string_from_file($identifier, $langfile, "\$resultstring")) &#123;
            eval($result);
            return $resultstring;
        &#125;
    &#125;
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Here's more or less how I think it should work

Code: Select all

$desired_language = $_GET['lang'];
include("/language_files/".${desired_language."_file_1"});
include("/language_files/".${desired_language."_file_2"});
include("/language_files/".${desired_language."_file_3"});
//and so on
Then, in your file, just keep referring to the $string array, as it will contain the variables from whatever files you included.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I may go with a more "automatic" type system:

untested example only

Code: Select all

<?php

// population information for the drop down
function getLanguages($langdir)
{
  // make sure $langdir is a string and a directory
  if(!is_string($langdir) || !is_dir($langdir))
    return false;

  // discover all the directories inside the language folder
  $dirlist = glob($langdir . DIRECTORY_SEPERATOR . '*', GLOB_ONLYDIR);

  $outlist = array();

  // run through each folder found, to get their language definition
  foreach($dirlist as $dir)
    // check for readability (and existance) of the language definition
    if(is_readable($dir . DIRECTORY_SEPERATOR . 'language.def'))
      // load the language definition, ex. English (US)
      $outlist[preg_replace('#^' . preg_quote($langdir . DIRECTORY_SEPERATOR,'#') . '#', '', $dir)] = trim(file_get_contents($dir . DIRECTORY_SEPERATOR . 'language.def')));

  return $outlist;
}

// include all php files related to a language
function loadLanguage($langdir, $lang)
{
  // make sure $langdir is a string and directory, and ensure $lang is a string and is a directory when combined with $langdir
  if(!is_string($langdir) || !is_dir($langdir) || !is_string($lang) || !is_dir($langdir . DIRECTORY_SEPERATOR . $lang))
    return false;

  // grab the file list from the requested language directory
  $files = glob($langdir . DIRECTORY_SEPERATOR . $lang . DIRECTORY_SEPERATOR . '*.php');

  // counter
  $x = 0;

  // iterate through the returned array
  foreach($files as $file)
    // check if file is readable
    if(is_readable($file))
    {
      // increment the counter;
      ++$x;
      // include it..
      include_once($file);
    }

  return $x; // return the number of files loaded..
}
User avatar
NewfieBilko
Forum Contributor
Posts: 189
Joined: Sun Jun 06, 2004 6:45 pm
Location: Newfoundland
Contact:

Post by NewfieBilko »

ALl in all that looks fine.. And thanks for the fucntions. But I get a call to Undefined Function from the GLOB()

??
pS: using php5
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

glob's a function in 5... maybe it's disabled? if so, you'll need a opendir/readdir/loop combo function to emulate it..
User avatar
NewfieBilko
Forum Contributor
Posts: 189
Joined: Sun Jun 06, 2004 6:45 pm
Location: Newfoundland
Contact:

Post by NewfieBilko »

yea i need something that will do this:

Code: Select all

// grab the file list from the requested language directory 
  $files = ($langdir.$lang.'/'.'*.php'); 

  foreach($files as $file) &#123;
    // check if file is readable 
    if(is_readable($file)) 
    &#123; 
      // increment the counter; 
      ++$x; 
      // include it.. 
      include_once($file); 
    &#125; 
  &#125;
i was thinking(im a pro at pseudo code)

Code: Select all

<?php

$lang = GET[lang]

LOOP
$FILE =FunctionToGetListOfFileInADirectory(needed)
x=+1
/LOOP

FOREACH $X
include (/$langdir/File . X+1)
ENDEACH



?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

fyi: DIRECTORY_SEPERATOR is a core constant that contains the server's OS's standard directory seperator character(s)..

there are several threads around here with opendir/readdir/loop functions, or code snippets..
User avatar
NewfieBilko
Forum Contributor
Posts: 189
Joined: Sun Jun 06, 2004 6:45 pm
Location: Newfoundland
Contact:

Post by NewfieBilko »

Ok, i can figure that out on my own thanks so much i really appericate everything you've done for me over this summer.

Anyway, one last thing.. The function

Code: Select all

// population information for the drop down 
function getLanguages($langdir) 
{ 
  // make sure $langdir is a string and a directory 
  if(!is_string($langdir) || !is_dir($langdir)) 
    return false; 

  // discover all the directories inside the language folder 
  $dirlist = glob($langdir . DIRECTORY_SEPERATOR . '*', GLOB_ONLYDIR); 

  $outlist = array(); 

  // run through each folder found, to get their language definition 
  foreach($dirlist as $dir) 
    // check for readability (and existance) of the language definition 
    if(is_readable($dir . DIRECTORY_SEPERATOR . 'language.def')) 
      // load the language definition, ex. English (US) 
      $outlist[preg_replace('#^' . preg_quote($langdir . DIRECTORY_SEPERATOR,'#') . '#', '', $dir)] = trim(file_get_contents($dir . DIRECTORY_SEPERATOR . 'language.def')); 

  return $outlist; 
}


How would i go about calling that from a simple html form?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

use the returned array to populate the drop down options.. i.e.

Code: Select all

foreach(getLanguages('some_directory') as $lang => $simpleName)
{
//....
}
User avatar
NewfieBilko
Forum Contributor
Posts: 189
Joined: Sun Jun 06, 2004 6:45 pm
Location: Newfoundland
Contact:

Post by NewfieBilko »

im not sure if i understand. I would put a SELECT option inside that foreach?


ummm and
some_directory, , in the prevous code you have $langdir, should this point to the directory where all my folders are? what does AS $lang=>$simplename do?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

NewfieBilko wrote:im not sure if i understand. I would put a SELECT option inside that foreach?
the drop down options would be written through the foreach, yes.
ummm and
some_directory, , in the prevous code you have $langdir, should this point to the directory where all my folders are? what does AS $lang=>$simplename do?
you pass the path to your languages folder to the function getLanguages()..

$lang => $simpleName ::

$lang will be each key in the array returned from getLanguages(). $simpleName is the value for each key..
Post Reply