Worked out a solution for;
> The following issues need to be resolved for my project;
> - The ability to fetch default data when the data is not available in
> the database in the requested language. For example a news article is
> not available in French so the English version would be fetched by the
> model given English is the default language of the site.
> - Some sort of indicator stating the requested language is not
> available so a version in the default language is given.
My news_articles controller looks like this at this point:
---
function beforeFilter(){
//set supported languages
$languages = array('en_US','fr_FR');
//set language preference if present in the url
(?language=xx_XX)
if(isset($this->params['url']['language'])){
if
(in_array($this->params['url']['language'],$languages)){
$this->NewsArticle->locale =
$this->params['url']['language'];
}
else{
$this->NewsArticle->locale = 'en_US';
}
}
else{
$this->NewsArticle->locale = 'en_US';
}
}
function index(){
//get articles array in the requested language
$trans_art = $this->NewsArticle->findAll();
//get articles array in the default language
$this->NewsArticle->locale = 'en_US';
$def_art = $this->NewsArticle->findAll();
//build array of translated articles - array key is the id of
the
article
foreach($trans_art as $artT){
$trans_art_list[$artT['NewsArticle']['id']] =
$artT['NewsArticle'];
}
//replace articles not found in the requested language by
articles
in the default language
//also set isLocale status - yes if the translation is there,
no if
not
foreach($def_art as $art){
if(array_key_exists($art['NewsArticle']['id'],$trans_art_list)){
$trans_art_list[$art['NewsArticle']['id']]['isLocale'] = 'yes';
$artList[]['NewsArticle'] =
$trans_art_list[$art['NewsArticle']
['id']];
}
else{
$art['NewsArticle']['isLocale'] = 'no';
$artList[]['NewsArticle'] = $art['NewsArticle'];
}
}
//set the combined array to the view
$this->set('news_articles', $artList);
$this->set('siteVars', $this->Conf->get('site.*'));
$this->set('title_for_layout', $this->Conf->get('site.name'));
}
-----
Short description:
- The beforeFilter get's the requested language or the default if not
given or invalid
- The index controller first get's every article in the default
language
- The index controller than get's every article in the requested
language (this array could be smaller)
- The index controller builds an array based in the default language.
- The index controller replaces records in the articles array for
every article where a translation is available
- Every article has a status variable indicating if the translation
was available or not (isLocale yes / no)
Next step would be to transfer this functionality to the model. In the
above example this functionality will be build into every controller
function. Something for the days to come...
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake
PHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---