CodeIgniter: Templates e Internacionalização

Encontrei uma maneira rápida e fácil de usar várias línguas com templates. Acho que não é um sistema de templates de verdade, mas por enquanto, está dando conta do recado. Depois da configuração inicial do meu CodeIgniter, decidi implementar um sistema de várias linguas. Seguindo esse tutorial, notei que cada página (view) traduzida é na verdade um arquivo independente, Não achei isso legal, pois se meu designer precisa mudar uma tag ou uma propriedade, ele teria que mudar em todas as views.

O truque é simples, e certamente deve haver outra forma mais elegante de fazer isso. O artigo acima diz que se eu tenho uma visão “myview”, eu não preciso adicionar um sufixo caso eu esteja na minha língua padrão, mas em outras línguas, eu preciso do sufixo do código da língua, por exemplo _pt, _en, etc. Mas no meu caso, todas as línguas deverão ter seu próprio sufixo, até mesmo a língua padrão, e a minha view sem sufixo é na verdade o template da página.

O código abaixo mostra o meu controlador chamando meu template:

[cc lang=”php”]
load_view(‘myview’); // Aqui eu carrego o meu template que será interceptado
}
}

/* End of file welcome.php */
/* Location: ./system/application/controllers/welcome.php */
[/cc]

Minha pequena modificação começa aqui. Eu não preciso chamar myview_pt pelo controlador (nesse caso eu teria que fazer uma checagem de qual lingua o usuario selecionou), porque o controlador modificado do artigo citado faz isso automaticamente. Então eu apenas crio myview_en.php e myview_pt.php (assumingo que tenho apenas essas duas línguas), e o conteudo desses arquivos seria algo mais ou menos assim:

[cc lang=”php”]
lang->load(‘myview’, ‘english’); ?>

[/cc]

Na primeira linha, “myview” é um arquivo localizado em system/language/english/myview_lang.php

Dessa forma eu coloco todo o meu html e css no myview.php e trabalho simulando um template. Espero que alguem venha comentar esse post dizendo “Olha, é assim que se faz corretamente”, mas até lá eu vou trabalando dessa forma pois assim pelo menos está resolvendo meu problema.

Até a próxima!

Did you like this? Share it:

30 Comments

  • Robson |

    A versão 1.6 já possui suporte nativo à internacionalização. Vc continua precisando apenas de uma view também e faz chamadas das variáveis da página via $this->lang->line(‘form_save’) , por exemplo.

  • akira_lee |

    Olá,
    Tenho o codeigniter (estou a começar a explorar) mas ainda não percebi muito bem como posso usar as línguas. Tenho a pasta language/en, language/pt e language/es coloco um ficheiro com as variáveis (iguais em todos) e respectivas traduções, mas como faço para alterar o idioma? Há alguma maneira de o fazer sem ter que fazer refresh à pagina?

    Obrigada

  • admin |

    Olá, infelizmente só com refresh mesmo, pois o CI depende da url para saber qual linguagem usar

  • nada |

    nada de mais, só um teste, favor apagar, desculpa ai amigo ter postado um teste em seu blog

  • Diana |

    Greetings from Los angeles! I’m bored at work
    so I decided to browse your site on my iphone during lunch break.

    I really like the information you provide here and can’t wait to take a
    look when I get home. I’m amazed at how quick your blog loaded
    on my cell phone .. I’m not even using WIFI, just 3G ..
    Anyhow, very good site!

  • Carrie |

    The post. The times. Manmoth. CBS. CNN. USA Today. The economists (far right leaning).
    Yougov. 538. The American prospect (hard right). Real clear politics and the national review.
    Polls showing impeachment favorability growing rapidly

  • hi |

    Good web site you’ve got here.. It’s hard to find good
    quality writing like yours nowadays. I truly
    appreciate people like you! Take care!!

  • tinyurl.com |

    I have read so many articles or reviews concerning
    the blogger lovers but this post is actually a good piece of writing, keep it up.

  • goyard |

    Spot on with this write-up, I really think this web site needs rather more consideration. I抣l most likely be again to read way more, thanks for that info.

  • http://tinyurl.com/ |

    I am really inspired along with your writing skills as neatly as with the layout in your weblog.
    Is that this a paid topic or did you modify it your self?
    Either way keep up the nice quality writing, it’s uncommon to look a nice weblog like this one
    today..

  • coconut oil there |

    Very nice post. I simply stumbled upon your weblog and wished to mention that I’ve really enjoyed browsing your blog posts.
    After all I will be subscribing on your feed and I am hoping you write
    again very soon!

  • ps4 games |

    This piece of writing will help the internet people for setting up new web site or
    even a blog from start to end.

  • ps4 games |

    Unquestionably believe that which you said. Your favorite justification seemed to be on the net the simplest thing
    to be aware of. I say to you, I definitely
    get irked while people consider worries that they just do not
    know about. You managed to hit the nail upon the top and defined out the whole thing without having side effect , people could take a signal.
    Will likely be back to get more. Thanks

  • quest bars cheap |

    Greate post. Keep writing such kind of info on your blog.
    Im really impressed by it.
    Hey there, You have done a fantastic job. I’ll certainly digg it and personally recommend to
    my friends. I am sure they’ll be benefited from this site.

  • quest bars cheap |

    Excellent post. I was checking continuously this blog and
    I am impressed! Very helpful info particularly the last part 🙂 I care for such information a lot.
    I was looking for this particular information for a very long
    time. Thank you and best of luck.

  • quest bars cheap |

    I’ll immediately grab your rss feed as I can not in finding
    your email subscription link or e-newsletter service.
    Do you’ve any? Kindly permit me recognise in order that I could subscribe.
    Thanks.

  • quest bars cheap coupon twitter |

    I truly love your site.. Excellent colors & theme.
    Did you develop this amazing site yourself?
    Please reply back as I’m wanting to create my own personal site and want to
    know where you got this from or just what the theme is named.
    Thank you!

  • quest bars cheap |

    I loved as much as you’ll receive carried out right here.

    The sketch is tasteful, your authored subject matter stylish.
    nonetheless, you command get bought an edginess over that you wish be delivering
    the following. unwell unquestionably come further formerly again since exactly the same nearly a lot often inside
    case you shield this increase.

  • ps4 games |

    I do agree with all the ideas you’ve presented on your post.
    They are really convincing and will definitely work. Nonetheless, the posts are very brief for beginners.
    Could you please extend them a bit from subsequent time?
    Thank you for the post.

  • ps4 games |

    Its like you read my mind! You seem to know so much about
    this, like you wrote the book in it or something.

    I think that you could do with some pics to drive the message home a little bit, but other than that, this is excellent blog.
    A fantastic read. I will definitely be back.

  • ps4 games |

    Spot on with this write-up, I truly feel this
    site needs much more attention. I’ll probably be back
    again to read through more, thanks for the info!

  • quest bars cheap |

    Nice post. I used to be checking constantly this weblog and
    I am impressed! Extremely helpful information specifically the ultimate
    section 🙂 I maintain such information much.

    I used to be seeking this particular info for a long
    time. Thank you and best of luck.

So, what do you think ?