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

Defining language in AJAX calls
#1

I have been using Commentics for a few months, and it is a great product.

I have customized Commentics for the Finnish and English languages. I load the Commentics area to webpages by an AJAX call. The call will be handled with the following code.

Code:
if (!isset($_POST["article"]) || !isset($_POST["language"])) return "Comment area parameters missing";
$cmtx_identifier = $_POST["article"];
$cmtx_reference  =  [code_that_return_pagetitle];
$cmtx_url = [code_that_returns_page_url];
$cmtx_folder    = '/comments/';
if ([code_that_return_website_language] == "fi") $cmtx_language = "finnish";
else $cmtx_language = "english";
require($_SERVER['DOCUMENT_ROOT'] . $cmtx_folder . 'frontend/index.php');

This code works well. Commentics appears in Finnish on Finnish pages and in English on other pages. However, when a user submits a new comment (=makes AJAX call), the replies are always in English. It seems that the language information is not passed. What should I modify so that the language information would be passed in AJAX calls so the replies would be in the same language as the form.
Reply
#2

I think this is the best solution. Let me know how it goes.

Open /comments/frontend/index.php

Replace this:
PHP Code:
<?php 
if (isset($cmtx_language)) {
    define('CMTX_LANGUAGE', $cmtx_language);
}

With this:
PHP Code:
<?php 
if (isset($cmtx_language)) {
    define('CMTX_LANGUAGE', $cmtx_language);
    $_SESSION['cmtx_language'] = $cmtx_language;
} else if (isset(
$_SESSION['cmtx_language'])) {
    define('CMTX_LANGUAGE', $_SESSION['cmtx_language']);
}

Also it would be great if you could provide your Finnish translation so that others can use it as well.
https://commentics.com/faq/language/contribute

Have you completed the interview?
Reply
#3

Thank you. Sounds like a good solution.

Unfortunately, it is still not working perfectly. Yes, the AJAX reply language changed to Finnish but now the replies are in Finnish on English pages too. Feels like a cache problem but as far as I understand, there is not any cache involved. I have to debug more.

Niko

PS. I can provide my Finnish translations.
Reply
#4

I found the issue. $_SESSION["cmtx_language"] is always empty.

Even though it is set during the normal page load, the value is not available for the ajax call processor. The normal page load is done through my CMS but ajax calls are sent directly to Commentics. My CMS overrides the PHP session handling so my CMS and Commentics have different session variable namespaces. I need to change my Commenctics integration or would there be another way to pass the language information?

Niko
Reply
#5

How about a hidden language field to the comment form and it would be sent together with the form data? If the field does not exists then the default language would be used. No need to use session variables then.
Reply
#6

Okay I'll have a go at this in the coming days. I assume you're using the latest version.

Have you completed the interview?
Reply
#7

Thanks. Yes, the latest version.
Reply
#8

Open /comments/frontend/index.php

Replace this:
PHP Code:
<?php 
if (isset($cmtx_language)) {
    define('CMTX_LANGUAGE', $cmtx_language);
}

With this:
PHP Code:
<?php 
if (isset($_POST['cmtx_language'])) {
    define('CMTX_LANGUAGE', $_POST['cmtx_language']);
} else if (isset(
$cmtx_language)) {
    define('CMTX_LANGUAGE', $cmtx_language);
}

Open /comments/frontend/controller/main/page.php

After this:
PHP Code:
<?php 
'commentics_url' => $this->url->getCommenticsUrl(),

Add this:
PHP Code:
<?php 
'language' => $this->setting->get('language'),

Then I've attached the JavaScript files to overwrite in the folder:
/comments/frontend/view/default/javascript/

.zip javascript.zip Size: 141.71 KB  Downloads: 5

Have you completed the interview?
Reply
#9

Thank you.

I completed the steps and works perfectly now.

I can provide my Finnish translation. There are still a few untranslated entries but after I have finished, them I will zip the language folder and send it.

Niko
Reply
#10

Thanks for confirming it works. I'll include it in the next version.

Have you completed the interview?
Reply


Possibly Related Threads…
Thread / Author Replies Views Last Post
Last Post by pizzaboi
17-Nov-2016, 10:38 PM
Last Post by pizzaboi
17-Nov-2016, 08:12 PM
Last Post by ApexGene
27-Feb-2015, 10:22 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)