Hi,
since I "WANT" my system to offer my users a multi-language system but NOT posting ads in a language different than my standard language as configurated in Admin, I have made changes that enable me to have my wish without any related issues so far...
To achieve this I used three 'tweaks' created by teseo and two core changes by myself:
Pre-requisites:1) Make sure you have more than one language installed and set active for the website!
2) Set the default site language once, do not change it afterwards because this IS the language used from now.
3) Make sure your existing ads, if any, are ALL using the default/standard locale:
- You need to update existing item-description-locale for this using phpMyAdmin!!
- You need to delete all item descriptions except those with the default language locale as mentioned in 2.
Steps:1) Let your system only publish ads in default language no matter the current user locale:
http://forums.osclass.org/general-help/how-to-remove-multiple-languages-in-post-page-only-not-whole-website/In the above link/thread the sample code from teseo uses the "osc_current_user_locale()", to make the system use ONE default language independent from user chosen language I use the
default Osclass language.
To achieve this make sure you change: <?php $current_locale = osc_current_user_locale(); ?> into <?php $current_locale = osc_get_preference('language'); ?>
This is a theme 'fix', by using the default locale in both item-post.php and item-edit.php all ads will be posted in your default language as setup in Admin.
2) Always show the search result with keyword search no matter what current user locale was used when saving the ad:
http://forums.osclass.org/3-5-x/search-problem-oc-3-5-3/msg122409/#msg122409By simply adding this code in your themes folder's functions.php the search result will always be shown no matter what current locale is:
function AddSearchConditions() {
if (isset($params['sPattern'])) {
$mSearch = Search::newInstance();
// To enable locale independent search we Add a wildcar for locales with Pattern search
$mSearch->addLocale('%');
}
}
//Add our new search condition to the search conditions used by Osclass
osc_add_hook('search_conditions', 'AddSearchConditions');
3) Change core file "user.php" in folder: /oc-includes/osclass/controller/ to enable Alerts no matter what current locale is used.
Add/insert the following code that makes locale = all locales after line 89:
$search->addLocale('%');
4) Make sure categories are translated correctly when switching language on website while being on search result page. Thanks to teseo's help the following functions fix an issue with translation related to url change. Place the following two functions in your theme's "functions.php" :
// Two custom functions used to make search-category-result language switch work as long as core is not fixed!
// Credits go to teseo
// Theme needs following change:
// You need to replace in your theme "osc_change_language_url" with the other new function, "cust_change_language_url".
function cust_change_language_url($locale) {
$url = osc_change_language_url($locale);
if( osc_rewrite_enabled() && osc_is_search_page() ) {
$cats = osc_search_category();
if(isset($cats[0])) {
return $url . '/?category=' . $cats[0];
}
}
return $url;
}
function cust_on_change_language_translate_categories() {
$params = Params::getParamsAsArray();
if(isset($params['page']) && isset($params['category'])) {
if ($params['page'] == 'language' && $params['category']) {
$translateCat = new DAO();
$translateCat->dao->select('s_slug');
$translateCat->dao->from(DB_TABLE_PREFIX.'t_category_description d');
$translateCat->dao->join(sprintf("(SELECT fk_i_category_id
FROM %st_category_description
WHERE s_slug = '%s') id", DB_TABLE_PREFIX, $params['category']), "d.fk_i_category_id = id.fk_i_category_id", "INNER");
$translateCat->dao->where(sprintf("d.fk_c_locale_code = '%s'", $params['locale']));
$result = $translateCat->dao->get();
$result = $result->row();
if ($result) $_SERVER['HTTP_REFERER'] = str_replace($params['category'], $result['s_slug'], $_SERVER['HTTP_REFERER']);
}
}
}
osc_add_hook('init', 'cust_on_change_language_translate_categories');
Make sure you also check your theme and change the "osc_change_language_url" into the new function name!
5) Update another function in /oc-includes/osclass/model/Item.php called "extendDataSingle($item)" which is used to get item descriptions
based on current user locale. Without this change you risk getting -empty- item descriptions when code is used to get data through "Item class" directly.
Change the line: $prefLocale = osc_current_user_locale(); into $prefLocale = osc_get_preference('language');
That's it, new ads will only get published in
default locale.
Searches are always done using *all* locales so they will show up in search results no matter what locale is "current"
When on Search result page you are able to change site language without issue!
Item descriptions will always be fetched based on
default locale
Alerts will always show the items no matter what locale is "current"
NB1. This is a HACK so no guarantees and IF you want to use it you do so at own risk.
NB2. Be sure to make backup before making any changes!I am using it on my own site and so far everything seems to work 100%.
Regards,
Eric