Welcome Guest, Not a member yet? Register   Sign In
localization issue
#1

(This post was last modified: 04-29-2021, 04:38 AM by eleumas.)

i believe to have an issue like this: https://forum.codeigniter.com/thread-75292.html

I have a site in 3 language: IT EN ES

I use {locale} and all work good but only if the pages are identical.

For example: in IT and ES i display the "regular home page" but in EN i would like display a maintenance page. Something force the view and too in EN i can see the home in english.

My link:
mydomain.com/it
mydomain.com/es
mydomain.com/en

EN controller (i tried too with redirect but doesn't work):
PHP Code:
    public function index()
    {
    
  $data['title'] = 'My site';
    
  $data['metaDescription'] = '';

      return 
redirect()->to(base_url('en/offline'));
    
  //echo view('offline', $data);
    


IT controller:
PHP Code:
public function index()
    {
    
  $data['title'] = 'Il mio sito';
    
  $data['metaDescription'] = '';

    
  echo view('home'$data);
    } 

Any idea to solve? Thanks for help me.
Reply
#2

What about a basic if control ? I use it to check if language segment of my url is empty or not. I have changed my codes for you

PHP Code:
public function index(){

        $lang $this->request->uri->getSegment(1);

        if($lang == 'en'){
            return redirect()->to(site_url('/en/offline'));
        

I tried checking the language with segment feature but you can check it via localization as well

PHP Code:
$locale $this->request->getLocale();
if(
$locale == 'en'){
//goes here..

Reply
#3

What do you mean by IT controller and EN controller? When using localization you don't need separate controller for each languages. The text is pulled from the language files but the logic in the controller is the same. Is you want different logic for a specific language you do as demyr said. Use getLocale() to make an if statement.
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
Reply
#4

(This post was last modified: 04-30-2021, 02:49 AM by eleumas.)

(04-29-2021, 05:16 PM)includebeer Wrote: What do you mean by IT controller and EN controller? When using localization you don't need separate controller for each languages. The text is pulled from the language files but the logic in the controller is the same. Is you want different logic for a specific language you do as demyr said. Use getLocale() to make an if statement.

Sorry all logic in one controller? Something like this:

PHP Code:
class Helloworld extends BaseController
{
// for english
public function about_us()
{
return 
view('about_us');
}

// for italian
public function chi_siamo() 
{
return 
view('chi-siamo');
}


and after what is the route for have a link like: www.mydomain.com/en/about-us or www.mydomain.com/it/chi-siamo ?

PHP Code:
$routes->get('{locale}/about-us''Helloworld::about_us'); 


INSTEAD

If i have 2 separate controller can i use localization and placeholder {locale}?
Is there a method more correct for this case?

A little example: https://drive.google.com/drive/folders/1...sp=sharing

Thanks for help me.
Reply
#5

(This post was last modified: 04-30-2021, 06:48 AM by demyr.)

You should have a language column on mysql for any table (pages, products, blog etc).
On your controller you can retrieve your datas by using: slug + language, or id + language

For example (Controller):

Code:
public function about_us()
{

$lang = $this->request->uri->getSegment(1);
$page_slug = $this->request->uri->getSegment(2);

$data['page'] = $this->PageModel->fetch_page($lang, $page_slug);

echo view ('site/assets/header',$data);
echo view ('site/page');

}


Model:

PHP Code:
public function fetch_page($lang$page_slug){
        $db      = \Config\Database::connect();
        $builder $db->table('pages');
        $query $builder->where('page_slug'$page_slug)
                    ->where('page_lang'$lang)
                ->get();

            return $query->getRow();
    

+ you can name your routes in any language, and get the source from the same Controller and Method.

Code:
$routes->get('{locale}/about-us', 'Helloworld::about_us');
$routes->get('{locale}/chi-siamo', 'Helloworld::about_us');
$routes->get('{locale}/wer-wir-sind', 'Helloworld::about_us');


Everything is clear.
Reply
#6

Sorry, but Codeigniter have a specific folder called language for translate multilangual sites. Why i should use a mySQL table for to do this? Smile
Reply
#7

It depends on what you are trying to accomplish. For my blog, I have a column in the article table for the language it is written in. I have only one controller that is used by all languages but it fetch only the articles in the language matching the locale defined in the url. I also have a column for the slug because I didn’t want to define a route for each article in every language. In this particular case, I don’t need a different controller for EN and for FR.

For the list of blog posts, the urls will look like this:
Code:
/en/blog
/fr/blog

And for the blog posts, the urls will look like this:
Code:
/en/blog/some-article-in-english
/fr/blog/un-article-en-francais
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
Reply
#8

(This post was last modified: 04-30-2021, 10:22 AM by demyr.)

(04-30-2021, 07:01 AM)eleumas Wrote: Sorry, but Codeigniter have a specific folder called language for translate multilangual sites. Why i should use a mySQL table for to do this? Smile


Well, If you think like that, it looks you have a long way to produce a larger scale project.

Language folder is for texts, not for your algorithms. In order to set the correct relational structure and algorithm you will need a very basic and solid branch to grasp, to connect/join everything in a clear way. So, a language column on your table will save your life and will make everything easier.

+

On your that ,mutual, product page, let's imagine a title of a part with if:

PHP Code:
<?php if($lang == 'en'){
  echo 'Categories';
}else if (
$lang == 'it'){
 echo 
'Categorie';
}else if (
$lang == 'de'){
  echo 'Kategorien';
//etc..
?>

but with language file you can only write :

PHP Code:
<?php echo lang('MyLangFile.catPartTitle');?>

that's all and clear again.
cordiali saluti Smile
Reply
#9

I have a simple site without blog or anything.
I would like only translate the text and i would like have an example how to use localization in Codeigniter 4 because i have some issue how i have specificed in my first post.

I know if the app is more complex i have to use a database and not a language file, but for example for the navbar items or others parts i can use a language file, no needed a database.

Thanks for help me.
Reply
#10

Read this may help you to understand what were talking about.

Multilanguage Database Design in MySQL
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB