This is the community forum. For a developer response use the Client Area.
Follow us on Facebook, Twitter and YouTube!

Frontend : Forcing a language by code
#1

Hi,

Because I'm running a multilingual website, configuring Commentics to use one pre-configured language was not an option. Installing Commentics multiple times neither.
So I introduced a cmtx configuration variable that can "force" a language depending on the language selected on my own page.
Code on my page (the $lang variable being the language code in ISO 639-1 ):
PHP Code:
<?php 
switch($lang) {
case
"en":
$cmtx_set_language = "english";
break;
case
"de":
$cmtx_set_language = "german";
break;
case
"fr":
$cmtx_set_language = "french";
break;
}

Added code in includes/variables/set_variables.php :
PHP Code:
<?php 
if (isset($cmtx_set_language)) {
$cmtx_temp = $cmtx_set_language;
unset(
$cmtx_set_language);
global
$cmtx_set_language;
$cmtx_set_language = $cmtx_temp;
}

Changed Code in includes/comments.php (could also be done in another location to keep comments.php tidy) :
PHP Code:
<?php 
/*************************************************************** LANGUAGE ***************************************************************/
$frontendLanguage = isset($cmtx_set_language)?$cmtx_set_language:$cmtx_settings->language_frontend;
require_once
$cmtx_path . "includes/language/" . $frontendLanguage . "/page.php"; //load language file for page
require_once $cmtx_path . "includes/language/" . $frontendLanguage . "/comments.php"; //load language file for comments
require_once $cmtx_path . "includes/language/" . $frontendLanguage . "/processor.php"; //load language file for processor
require_once $cmtx_path . "includes/language/" . $frontendLanguage . "/form.php"; //load language file for form
require_once $cmtx_path . "includes/language/" . $frontendLanguage . "/countries.php"; //load language file for countries
require_once $cmtx_path . "includes/language/" . $frontendLanguage . "/ratings.php"; //load language file for ratings
/****************************************************************************************************************************************/

I also concatenated my language code with the page-id to separate comments by language but this is not necessary if you want to mix up comments in different languages :
PHP Code:
<?php 
$cmtx_page_id
= $pageId."-".$lang;


Could perhaps also be interesting for other websites ...

Cheers,
pier
Reply
#2

Good job Pier.. Thanks v.m.

I am in the same situation, and by chance, also unse the same variable name $lang storing the ISO code.

I was willing to modify directly the /includes/classes/settings.php to replace directly the $cmtx_settings->language_frontend value returned, when I stopped into your solution.

One issue I continue having is, how to deal with different languages, when use the questions feature, instead of the recapcha ( because its is more user friendly ).

I am searching where are stored the questions, in order to find the way to solve this.
Reply
#3

OK. I did it..

I added a new indexed column "lang" to the table {cmtx_prefix_}questions.
Then exported the english questions, and edited the file, with the new spanish questions. ( Must to remove the mention of the id, coz it is auto-incremented ), and executed the insert of the new rows.
Finally added the following code to the script {commentics_path}/includes/template/form.php to filter the query of questions ($cmtx_question_query) by language.

------------------------------
<?php if(isSet($_GET['lang'])){
$lang = $_GET['lang'];
}else if(isSet($_SESSION['lang'])){
$lang = $_SESSION['lang'];
}else if(isSet($_COOKIE['lang'])){
$lang = $_COOKIE['lang']; }
else{ $lang = 'en'; } // Default language as English or the one u prefer
?>
<?php $cmtx_question_query = mysql_query("SELECT * FROM `".$cmtx_mysql_table_prefix."questions` WHERE `lang`='".$lang."' ORDER BY Rand() LIMIT 1"); ?>
------------------------------

(13-Sep-2012, 09:39 AM)hugo_bcn Wrote:  Good job Pier.. Thanks v.m.

I am in the same situation, and by chance, also use the same variable name $lang storing the ISO code.

I was willing to modify directly the /includes/classes/settings.php to replace directly the $cmtx_settings->language_frontend value returned, when I stopped into your solution.

One issue I continue having is, how to deal with different languages, when use the questions feature, instead of the recatpcha ( because it is less user friendly ).

I am searching where are stored the questions, in order to find the way to solve this.
Reply
#4

Hi,

I was about to suggest the same solution.

However I would sanitize the variable $lang before using it in the MySQL query.

<?php if(isSet($_GET['lang'])){
$lang = $_GET['lang'];
}else if(isSet($_SESSION['lang'])){
$lang = $_SESSION['lang'];
}else if(isSet($_COOKIE['lang'])){
$lang = $_COOKIE['lang']; }
else{ $lang = 'en'; } // Default language as English or the one u prefer
$lang = cmtx_sanitize($lang, true, true, true);
?>

Have you completed the interview?
Reply
#5

Yes. Of course Steve. I showed that code, only as an example, coz depends of how is treated the variable in each application.

Thanks for your feedback.
Reply
#6

Steven,
I've tried to understand what pier did and how he did it.
However the explanation is to short for me to get it to work.

If this solution is actually working, would it be much work for you
to setup a small guide based on this, with some guidelines and
for instance publish it in the faq?

Compliments for this great application and the work and time you seem to invest in it!
(do you also have a life? ;-)

Cheers,
John
Reply


Possibly Related Threads…
Thread / Author Replies Views Last Post
Last Post by DeltaOS
27-Sep-2010, 04:55 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)