Jump to content

Adding more languages for PHP Localization for CMS


yubi

Recommended Posts

Hello there,

 

I have a CMS which I've trying to create localization for. The idea is in the admin area you check a radio button and it changes the language of the entire admin interface. There is a mysql database with a table called nb_settings that has a column called lang. Up to this point I've gotten it to work but am having problems adding additional languages. The localization files have names like lang.nl.php,lang.en.php and so on..

 

The original code that works is this in a file called settings.php:

...
<?php
if($row['lang'] == nl)
{
echo '<input name="lang" type="radio" value="nl" checked="checked" /> '.($_CONSTANTS['NL']).'
<input name="lang" type="radio" value="en" /> '.($_CONSTANTS['EN']).'
<input name="lang" type="radio" value="de" /> '.($_CONSTANTS['DE']).'';
}
elseif($row['lang'] == en)
{
echo '<input name="lang" type="radio" value="nl" /> '.($_CONSTANTS['NL']).'
<input name="lang" type="radio" value="en" checked="checked" /> '.($_CONSTANTS['EN']).'
<input name="lang" type="radio" value="de" /> '.($_CONSTANTS['DE']).'';
}
elseif($row['lang'] == de)
{
echo '<input name="lang" type="radio" value="nl" /> '.($_CONSTANTS['NL']).'
<input name="lang" type="radio" value="en" /> '.($_CONSTANTS['EN']).'
<input name="lang" type="radio" value="de" checked="checked" /> '.($_CONSTANTS['DE']).'';
}
?>
...

 

Another file called common.php is used as well and has the following:

 

<?php
session_start();
header('Cache-control: private'); // IE 6 FIX

$sql = "SELECT * FROM nb_settings";
$query = mysql_query($sql);
$row = mysql_fetch_assoc($query);
                  
                        if($row['lang'] == nl) 
                        { 
                            include('languages/lang.nl.php'); 
                        } 
                        elseif($row['lang'] == en) 
                        { 
                            include('languages/lang.en.php'); 
                        } 
                        elseif($row['lang'] == de) 
                        { 
                            include('languages/lang.de.php'); 
                        } 
                     


?>

 

But I am havng trouble adding more languages and expanding this. There is a folder called languages into which I put the individual localization files.

 

 

Just adding similar lines of code as shown below in both common and settings files doesn't do it: ie:

 

 

1)modifying settings.php to become:

 

<?php 
if($row['lang'] == nl) 
{ 
echo '<input name="lang" type="radio" value="nl" checked="checked" />  '.($_CONSTANTS['NL']).'
<input name="lang" type="radio" value="en" /> '.($_CONSTANTS['EN']).'
<input name="lang" type="radio" value="de" /> '.($_CONSTANTS['DE']).'
<input name="lang" type="radio" value="fra" /> '.($_CONSTANTS['FRA']).'';
} 
elseif($row['lang'] == en) 
{ 
echo '<input name="lang" type="radio" value="nl" /> '.($_CONSTANTS['NL']).'
    <input name="lang" type="radio" value="en" checked="checked" /> '.($_CONSTANTS['EN']).'
<input name="lang" type="radio" value="de" /> '.($_CONSTANTS['DE']).'
<input name="lang" type="radio" value="fra" /> '.($_CONSTANTS['FRA']).''; 
} 
elseif($row['lang'] == de) 
{ 
echo '<input name="lang" type="radio" value="nl" /> '.($_CONSTANTS['NL']).'
    <input name="lang" type="radio" value="en" /> '.($_CONSTANTS['EN']).'
<input name="lang" type="radio" value="de" checked="checked"  /> '.($_CONSTANTS['DE']).'
<input name="lang" type="radio" value="fra" /> '.($_CONSTANTS['FRA']).'';

} 

elseif($row['lang'] == fra) 
{ 
echo '<input name="lang" type="radio" value="nl" /> '.($_CONSTANTS['NL']).'
    <input name="lang" type="radio" value="en" /> '.($_CONSTANTS['EN']).'
<input name="lang" type="radio" value="de" /> '.($_CONSTANTS['DE']).'
<input name="lang" type="radio" value="fra" checked="checked" />  '.($_CONSTANTS['FRA']).'';

} 
?>

 

 

2)and adding in common.php:

 


elseif($row['lang'] == fra) 
                        { 
                            include('languages/lang.fra.php'); 
                        } 

 

Both don't work. The php is obviously not correctly parse and the syntax may have issues.

Can anyone help me out with how to add additional languages?

Link to comment
Share on other sites

Yes indeed. At the bottom of the localization files. See attached example and below for the englich localization file:

 

<?php
/* 
------------------
Language: English
------------------
*/

define('LANG_FORM_SUBMIT', 'Submit');
define('LANG_FORM_CLEAR', 'Clear Form');
define('LANG_FORM_UPLOAD', 'Upload');

// Dashboard
define('LANG_DASHBOARD', 'Dashboard');
define('LANG_DASHBOARD_INSTALL_ERROR', 'Please delete the install.php file.');
define('LANG_DASHBOARD_ADD_PAGE', 'Add a new page');
define('LANG_DASHBOARD_EDIT_PAGE', 'Edit / Delete pages');
define('LANG_DASHBOARD_EDIT_SETTINGS', 'Edit Settings');
define('LANG_DASHBOARD_TEMPLATES', 'Manage your templates');
define('LANG_DASHBOARD_FILEMANAGER', 'Upload, edit and delete files/folders');
define('LANG_DASHBOARD_DOCU', 'Check out the documentation.');

// Index
define('LANG_MANAGE_PAGES', 'Manage Pages');
define('LANG_ADD_PAGE', 'Add Page');
define('LANG_EDIT_PAGE', 'Edit / Delete Page');
define('LANG_SETTINGS', 'Settings');
define('LANG_FILE_MANAGER', 'File Manager');
define('LANG_DOCU', 'Documentation');
define('LANG_MANAGE_USERS', 'Manage Users');
define('LANG_ADD_USER', 'Add User');
define('LANG_EDIT_USER', 'Edit / Delete Users');
define('LANG_LOGOUT', 'Logout');
define('LANG_INDEX_TITLE', 'Admin Section');
define('LANG_VISIT_SITE', 'Visit Site');
define('LANG_LOGIN_AS', 'You are logged in as');

// New Page
define('LANG_NEW_PAGE_HEAD', 'Create New Page');
define('LANG_PAGE_TITLE', 'Page Title');
define('LANG_MENU_TITLE', 'Menu Title');
define('LANG_PAGE_KEYWORDS', 'Page Keywords');
define('LANG_PAGE_DESCRIPTION', 'Page Description');
define('LANG_PAGE_CONTENT', 'Page Content');
define('LANG_PAGE_SUCCES', 'Page successfully added!');
define('LANG_MENU_TITLE_TOOLTIP', 'The name you wish to appear in the menu - Leave blank to exclude this page from the menu');
define('LANG_KEYWORDS_TOOLTIP', 'These are keywords that best describe your page - Seperate by commas');
define('LANG_DESCRIPTION_TOOLTIP', 'This is a short description of your page . This is what google will display');

define('LANG_EDIT_PAGE_HEAD', 'Manage Pages');
define('LANG_PAGE_DELETE', 'Page successfully Deleted!');
define('LANG_PAGE_EDIT', 'Page successfully edited!');

// Edit Page
define('LANG_EDIT_PAGE_1_HEAD', 'Edit Page');
define('LANG_TABLE_ID', 'ID');
define('LANG_TABLE_NAME', 'Name');
define('LANG_TABLE_CREATED', 'Created');
define('LANG_TABLE_EDITED', 'Last Edited');
define('LANG_TABLE_MENU', 'Menu Order');
define('LANG_TABLE_ACTIONS', 'Actions');
define('LANG_EDIT_TOOLTIP', 'Edit this page');
define('LANG_DELETE_TOOLTIP', 'Delete this page');
define('LANG_DELETE_ERROR', 'Sorry, You can not delete the homepage');

// Settings
define('LANG_SETTINGS_HEAD', 'Edit Settings');
define('LANG_SETTINGS_SUCCES', 'Settings successfully edited!');
define('LANG_SITE_NAME', 'Site Name');
define('LANG_SITE_KEYWORDS', 'Site Keywords');
define('LANG_SITE_DESCRIPTION', 'Site Description');
define('LANG_SITE_URL', 'Site URL');
define('LANG_SEO_URLS', 'Enable SEO urls?');
define('LANG_ADMIN_LANGUAGE', 'Admin Language');
define('LANG_SITE_NAME_TOOLTIP', 'The name of you website');
define('LANG_SITE_KEYWORDS_TOOLTIP', 'Keywords that best describe your website');
define('LANG_SITE_DESCRIPTION_TOOLTIP', 'A short description of your website');
define('LANG_SITE_URL_TOOLTIP', 'Should be the location where CMS is installed (E.G www.mywebsite.com/)');
define('LANG_SEO_TOOLTIP', 'Makes URLs search engine friendly. Eg. www.yoursite.com/12/page-name.html. To enable this you must make sure the .htaccess file is in the root of your website.');

// Filemanager
define('LANG_FILE_NAME', 'Name');
define('LANG_FILE_EDIT', 'Edit');
define('LANG_FILE_DELETE', 'Delete');
define('LANG_FILE_SIZE', 'Size');
define('LANG_UPLOAD_FILES', 'Upload Files');
define('LANG_CREATE_FOLDER', 'Create Folder');
define('LANG_UPLOAD_FILE_TITLE', 'Upload a file in current folder:');
define('LANG_EXIST_FILE', 'Overwrite existing file');
define('LANG_UPLOAD_BUTTON', 'Upload');
define('LANG_CREATE_FOLDER_TITLE', 'Create a new folder. Folder name:');
define('LANG_FILE_EDIT_TITLE', 'Edit File:');
define('LANG_DELETE_SURE', 'Are you sure?');

// Users
define('LANG_MANAGE_USERS_TITLE', 'Manage Users');
define('LANG_USERS_ID', 'ID	');
define('LANG_USERS_NAME', 'Username');
define('LANG_USERS_EMAIL', 'Email');
define('LANG_USERS_ACTIONS', 'Actions');
define('LANG_USERS_EDIT_TOOLTIP', 'Edit this user profile');
define('LANG_USERS_DELETE_TOOLTIP', 'Delete this user');
define('LANG_USERS_DELETE_ERROR', 'Sorry, You can not delete the site admin');
define('LANG_USERS_EDIT_SUCCES', 'User successfully edited!');
define('LANG_USERS_EDIT_TITLE', 'Edit User');
define('LANG_USERS_PASSWORD', 'Password');
define('LANG_USERS_ADD_TITLE', 'Create User');
define('LANG_USERS_ADD_SUCCES', 'User Successfully Added!');

// Templates
define('LANG_MANAGE_TEMPLATES', 'Manage Templates');
define('LANG_TEMPLATE_NAME', 'Name');
define('LANG_TEMPLATE_BROWSE', 'Browse Files');
define('LANG_TEMPLATE_PREVIEW', 'Preview');
define('LANG_TEMPLATE_ACTIVE', 'Active');
define('LANG_TEMPLATE_ACTIVATE', 'Activate');
define('LANG_TEMPLATE_UP_TITLE', 'Upload a Template file:');
define('LANG_TEMPLATE_UP_BUTTON', 'Upload a Template.');
define('LANG_TEMPLATE_BACK', 'Go Back');
define('LANG_TEMPLATE_EDIT', 'Edit');
define('LANG_TEMPLATE_EDIT_CODE', 'Edit this files code');
define('LANG_TEMPLATE_EDIT_HTML', 'Edit this file with a html editor (Not recommended for editing PHP files as PHP code may become obscured.)');
define('LANG_TEMPLATE_SAVE', 'Save File');
define('LANG_TEMPLATE_BLOCK', 'Insert Blocks');
define('LANG_TEMPLATE_BLOCK2', 'Block :');

$_CONSTANTS['TEMPLATES_ZIP_ERROR'] = 'Templates must be in .zip format.';
$_CONSTANTS['TEMPLATE_EXIST_ERROR'] = 'Template exists already - Select overwrite to overwrite the template';
$_CONSTANTS['TEMPLATE_SUCCES_UP'] = 'Template successfully uploaded!';
$_CONSTANTS['TEMPLATE_SUCCES_CHANGE'] = 'Successfully changed site template to';
$_CONSTANTS['TEMPLATE_ERROR_CHANGE'] = 'Template not changed. Error occured';
$_CONSTANTS['TEMPLATE_ERROR_CHANGE_2'] = 'There is no index.php file in that template folder.';

define('LANG_TEMPLATE_INSERT', 'Insert Template Variables');
define('LANG_TEMPLATE_INSERT2', 'Site Specific Variables');
define('LANG_TEMPLATE_INSERT3', 'Page Specific Variables');
define('LANG_TEMPLATE_INSERT4', 'Template Specific Variables');
define('LANG_TEMPLATE_PAGE_DATE', 'Page Date');
define('LANG_TEMPLATE_INSERT_MENU', 'Insert Menu');
define('LANG_TEMPLATE_PATH', 'Template Path');

// Blocks
define('LANG_MANAGE_BLOCKS', 'Manage Blocks');
define('LANG_ADD_BLOCKS', 'Add Block');
define('LANG_EDIT_BLOCKS', 'Edit / Delete Blocks');
define('LANG_BLOCK_ID', 'ID');
define('LANG_BLOCK_NAME', 'Block Name');
define('LANG_BLOCK_DESCRIPTION', 'Description');
define('LANG_BLOCK_ACTIONS', 'Actions');
define('LANG_BLOCK_EDIT_TOOLTIP', 'Edit this block');
define('LANG_BLOCK_REMOVE_TOOLTIP', 'Delete this block');

define('LANG_BLOCK_CREATED_SUCCES', 'Block Successfully Created!');
define('LANG_NEW_BLOCK_DESCRIPTION', 'Block Description (optional)');
define('LANG_NEW_BLOCK_DESCRIPTION_TOOLTIP', 'This is a short description what your block does. Leave black if you wish.');
define('LANG_BLOCK_CONTENT', 'Block Content');
define('LANG_BLOCK_CONTENT_TOOLTIP', 'This will be the content of your block. You can include almost anything in here (PHP,HTML,JAVASCRIPT,CSS)');
define('LANG_BLOCK_CONTENT2', 'Block content goes here.');
define('LANG_BLOCK_EDITOR_OFF', 'Please turn the editor off before submitting to ensure your block saves correctly.');

define('LANG_BLOCK_EDIT_SUCCES', 'Block Successfully Edited!');
define('LANG_EDIT_BLOCK_TITLE', 'Edit Block');

// Documentation
define('LANG_DOC_P1', 'Creating & editing templates is a simple process. The templating systems uses small inserts of PHP within a HTML file, this allows for easy template creation and customization.');
define('LANG_DOC_P2', 'Templates can be edited directly within the CMS backend using the <a href="?action=templates">template manager</a> or the <a href="?action=file_manager">file manager</a>.');
define('LANG_DOC_P3', 'All free css templates found on the internet can easily be used with Newbie.<br>Here is a great website with links to thousands of free web templates <a href="http://www.smashingmagazine.com/2007/01/12/free-css-layouts-and-templates/">http://www.smashingmagazine.com/../free-css-layouts-and-templates/</a>');

define('LANG_DOC_TITLE', 'Creating a Template');
define('LANG_DOC_P4', 'Lets say we are creating a template called "<strong>mysite_template</strong>".');
define('LANG_DOC_P5', 'First we would create a folder called "<strong>mysite_template</strong>" in your "templates" folder.<br>
    You would then create a file called "index.php" and place it in that folder.<br>This file will contain all the design/html of your website.');
define('LANG_DOC_P6', 'You can edit the templates manually or use the <a href="?action=templates">Template Editor</a>.');
define('LANG_DOC_P7', 'Templates can also be zipped and upload directly within the template manager.');

define('LANG_DOC_P8', 'Below are the PHP inserts which output your content :');
define('LANG_DOC_P9', 'This will output the name of the page you are on.');
define('LANG_DOC_P10', 'This will output the page keywords for inclusion in your keywords meta tag.');
define('LANG_DOC_P11', 'This will output the page description for inclusion in the description meta tag.');
define('LANG_DOC_P12', 'This will output the content(html) of your page.');
define('LANG_DOC_P13', 'This will output the date which the page was created.');
define('LANG_DOC_P14', 'To out put the menu you use this :');
define('LANG_DOC_P15', 'There are two options for outputting the menu.');
define('LANG_DOC_P16', 'The first is :');
define('LANG_DOC_P17', 'This outputs the menu in an un-ordered list format like :');
define('LANG_DOC_P18', 'The second is :');
define('LANG_DOC_P19', 'This will output the menu as a link list like :');
define('LANG_DOC_P20', 'There are also site wide variables which can be used in templates.');
define('LANG_DOC_P21', 'This is the location where Newbie is installed.');
define('LANG_DOC_P22', 'This is the name of your website.');
define('LANG_DOC_P23', 'These are the default keywords and description for the whole website.');
define('LANG_DOC_P24', 'This is the path to your template. This is needed if you are including an external CSS file in a template.<br>
Like below.');

// Other
$_CONSTANTS['NL'] = 'Dutch';
$_CONSTANTS['EN'] = 'English';
$_CONSTANTS['FRA'] = 'French';
$_CONSTANTS['DE'] = 'German';
$_CONSTANTS['ON'] = 'On';
$_CONSTANTS['OFF'] = 'Off';
$_CONSTANTS['UP'] = 'Up';
$_CONSTANTS['DOWN'] = 'Down';
$_CONSTANTS['FILE_WARNING'] = 'Sorry, cannot write to file';
$_CONSTANTS['FILE_SAVE'] = 'File successfully saved!';
$_CONSTANTS['FILE_INVALID'] = 'Invalid file!';
$_CONSTANTS['CLICK_CLOSE'] = 'Click to close';
$_CONSTANTS['FOLDER_SUCCES'] = 'Folder successfully created!';
$_CONSTANTS['FOLDER_ERROR'] = 'Sorry, Something went wrong!';
$_CONSTANTS['FILE_ERROR1'] = 'The uploaded file exceeds the upload_max_filesize!';
$_CONSTANTS['FILE_ERROR2'] = 'The uploaded file exceeds the MAX_FILE_SIZE!';
$_CONSTANTS['FILE_ERROR3'] = 'The uploaded file was only partially uploaded!';
$_CONSTANTS['FILE_ERROR4'] = 'No image was uploaded!';
$_CONSTANTS['FILE_ERROR4_2'] = 'No file was uploaded!';
$_CONSTANTS['FILE_ERROR6'] = 'Missing a temporary folder!';
$_CONSTANTS['FILE_ERROR7'] = 'Failed to write file to disk!';
$_CONSTANTS['FILE_ERROR8'] = 'File upload stopped by extension!';
$_CONSTANTS['FILE_ERROR9'] = 'File exists already - Select overwrite to overwrite the file';
$_CONSTANTS['FILE_ERROR10'] = 'Failed to upload file!';
$_CONSTANTS['FILE_SUCCES_UP'] = 'File successfully uploaded!';
$_CONSTANTS['FILE_SUCCES_DEL'] = 'Deleted file :';
?>

 

[attachment deleted by admin]

Link to comment
Share on other sites

Regarding the constant : if($row['lang'] == nl)  .

 

There is  a table called nb_settings and that table has a column called lang.

 

This can be nl or some other language. In the install.php file, the table is created with the lang column.

sql[3] = "CREATE TABLE IF NOT EXISTS `nb_settings` (
  `name` varchar(255) NOT NULL,
  `keywords` varchar(255) NOT NULL,
  `description` varchar(500) NOT NULL,
  `seo_url` int(3) NOT NULL,
  `lang` varchar(3) NOT NULL,
  `template` varchar(50) NOT NULL,
  `site_url` varchar(255) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
";

On install of the CMS, line 229 of the attached code adds the nl language and it works.

 

...
...
$sql[5] = "INSERT INTO `nb_settings` (`name`, `keywords`, `description`, `seo_url`, `lang`, `template`, `site_url`) VALUES
('test', 'test', 'test', 1, 'nl', 'templates/default', '');
";

Link to comment
Share on other sites

Your comparing the database record to what appears to be an undefined constant nl. You need to turn error reporting to E_ALL and set display errors to on when developing. If nl is meant to be a string it needs to be surrounded by quotes.

 

if($row['lang'] == 'nl')

 

It might not be causing your issue but it's a pretty good sign of poor code. Is this code your or some third party?

Link to comment
Share on other sites

Yes, it's pitting up the errors and I'll get back in a minute or two:

Notice: Use of undefined constant nl - assumed 'nl' in...

Notice: Use of undefined constant en - assumed 'en'...

Notice: Use of undefined constant de - assumed 'de'...

 

Theyr'e all undefined but how come it worked until now? Get back to you in a jiffy...

Link to comment
Share on other sites

The errors where always there, you just can't see them when you have error reporting set lower. It doesn't mean there not effecting performance though, errors are errors and php has to go out of its way to work around them.

 

It's a sign of poor code is all. Code should be developed with error reporting set as strictly as possible, that way the dev will see and fix the errors straight away.

 

These errors don't likely have anything to do with your actual problem, it's just something else that should be fixed.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.