benjamincurrie / CodeIgniter-Language-Switch

Codeigniter hook for multi language support, focuses on support for SEO friendly URLs in each language.
15 stars 11 forks source link

Help! #2

Closed martonboti closed 1 year ago

martonboti commented 12 years ago

Hi,

I'm trying to use your code, and it works fine but I don't know how to use it properly(newbie over here).

My url is like http://mysite/en/home/

This works fine if I load $autoload['language'] = array('english'); by default. If I change the url to http://mysite/hu/home/ it says Unable to load the requested language file: language/english/hungarian_lang.php it should be like language/hungarian/hungarian_lang.php

I have two separate folders in the language folder, english and hungarian.

How can I fix this?

benjamincurrie commented 12 years ago

Hey,

Have you set up everything correctly in the config/languages.php ?

The plugin was made specifically to work with SEO urls, eg. /home and /otthon, I put in some extra code to be able to use urls as you mention, but it's fairly untested. To do what you want to do you'll need to change the following settings:

$config['url_style'] = "Path"; $config['languages'] = array('english'=>'English', 'hungarian'=>'Hungarian' ); $config['language_codes'] = array( 'en' => 'english', 'hu' => 'hungarian' );

If that doesn't solve it let me know and I'll try help you out.

Be sure to use codeigniter's built in site_url to get language specific links, and there is a function language_menu to create a menu for switching languages.

Ben

On Fri, Sep 7, 2012 at 11:28 AM, Roscoe notifications@github.com wrote:

Hi,

I'm trying to use your code, and it works fine but I don't know how to use it properly(newbie over here).

My url is like http://mysite/en/home/

This works fine if I load $autoload['language'] = array('english'); by default. If I change the url to http://mysite/hu/home/ it says Unable to load the requested language file: language/english/hungarian_lang.php it should be like language/hungarian/hungarian_lang.php

I have two separate folders in the language folder, english and hungarian.

How can I fix this?

— Reply to this email directly or view it on GitHubhttps://github.com/benjamincurrie/CodeIgniter-Language-Switch/issues/2.

martonboti commented 12 years ago

Okay, I changed the $config['url_style'] = "Path"; but it didn't help.

Could you give me an example with site_url and language_menu functions? I tried like this: a class="register" href="<?php echo site_url(); ?>home/register"> but i get tons of errors. Thanks.

benjamincurrie commented 12 years ago

Would be more like:

Language menu can just be called as <?php echo language_menu('custom-class'); ?> and will return something like:

Make sure you've configured everything in the settings as I've mentioned above. Throw the errors you get in here and I'll try help you out.

Ben

On Fri, Sep 7, 2012 at 12:18 PM, Roscoe notifications@github.com wrote:

Okay, I changed the $config['url_style'] = "Path"; but it didn't help.

Could you give me an example with site_url and language_menu functions? I tried like this: a class="register" href="<?php echo site_url(); ?>home/register"> but i get tons of errors. Thanks.

— Reply to this email directly or view it on GitHubhttps://github.com/benjamincurrie/CodeIgniter-Language-Switch/issues/2#issuecomment-8370310.

martonboti commented 12 years ago

I've configured everything as far as I can tell. My links aren't working now, they look like this: http://localhost/allamvizsga/?0=en&1=home&2=register And the language menu only shows 1 language, hungarian. No errors now, just nothing works :| Should I load or set up something in the controller?

martonboti commented 12 years ago

This might help:

language.php

$config['url_style'] = "Path"; $config['languages'] = array('english'=>'English', 'hungarian'=>'Hungarian'); $config['browser_language_detection'] = TRUE; $config['language_codes'] = array( 'en' => 'english', 'hu' => 'hungarian' ); $config['segment_translations'] = array( 'hungarian' => array( 'index' => 'index', 'about' => 'rolunk', 'downloads' => 'letoltes', 'contact' => 'elerhetoseg', 'home' =>'otthon' ) );

autoload.php

$autoload['packages'] = array(); $autoload['libraries'] = array('database','form_validation','session','email'); $autoload['helper'] = array("html","url","form","language"); $autoload['config'] = array('language'); $autoload['language'] = array(); $autoload['model'] = array('user_model');

config.php

$config['base_url'] = '''; $config['index_page'] = ''; $config['uri_protocol'] = 'AUTO'; $config['url_suffix'] = ''; $config['language'] = ''; $config['charset'] = 'UTF-8'; $config['enable_hooks'] = TRUE; $config['subclassprefix'] = 'MY'; $config['permitted_urichars'] = 'a-z 0-9~%.:-'; $config['allow_get_array'] = TRUE; $config['enable_query_strings'] = TRUE; $config['controller_trigger'] = 'c'; $config['function_trigger'] = 'm'; $config['directory_trigger'] = 'd'; $config['log_threshold'] = 0; $config['log_path'] = ''; $config['log_date_format'] = 'Y-m-d H:i:s'; $config['cache_path'] = ''; $config['encryption_key'] = 'whatever'; $config['sess_cookie_name'] = 'ci_session'; $config['sess_expiration'] = 7200; $config['sess_expire_on_close'] = FALSE; $config['sess_encrypt_cookie'] = FALSE; $config['sess_use_database'] = FALSE; $config['sess_table_name'] = 'ci_sessions'; $config['sess_match_ip'] = FALSE; $config['sess_match_useragent'] = TRUE; $config['sess_time_to_update'] = 300; $config['cookie_prefix'] = ""; $config['cookie_domain'] = ""; $config['cookie_path'] = "/"; $config['cookie_secure'] = FALSE; $config['global_xss_filtering'] = FALSE; $config['csrf_protection'] = FALSE; $config['csrf_token_name'] = 'csrf_test_name'; $config['csrf_cookie_name'] = 'csrf_cookie_name'; $config['csrf_expire'] = 7200; $config['compress_output'] = FALSE; $config['time_reference'] = 'local'; $config['rewrite_short_tags'] = FALSE; $config['proxy_ips'] = '';

benjamincurrie commented 12 years ago

Set 'enable_query_strings' in your config.php to FALSE,

Also, you don't need to configure the 'segment_translations' unless you want SEO friendly URLs, in which case change url_style to SEO.

Let me know if that fixes it, Ben

On Fri, Sep 7, 2012 at 1:03 PM, Roscoe notifications@github.com wrote:

This might help:

language.php

$config['url_style'] = "Path"; $config['languages'] = array('english'=>'English', 'hungarian'=>'Hungarian'); $config['browser_language_detection'] = TRUE;

$config['language_codes'] = array( 'en' => 'english', 'hu' => 'hungarian' ); $config['segment_translations'] = array( 'hungarian' => array( 'index' => 'index', 'about' => 'rolunk', 'downloads' => 'letoltes', 'contact' => 'elerhetoseg', 'home' =>'otthon' ) );

autoload.php

$autoload['packages'] = array(); $autoload['libraries'] = array('database','form_validation','session','email'); $autoload['helper'] = array("html","url","form","language"); $autoload['config'] = array('language'); $autoload['language'] = array(); $autoload['model'] = array('user_model');

config.php

$config['base_url'] = '''; $config['index_page'] = ''; $config['uri_protocol'] = 'AUTO'; $config['url_suffix'] = ''; $config['language'] = ''; $config['charset'] = 'UTF-8'; $config['enable_hooks'] = TRUE; $config['subclassprefix'] = 'MY'; $config['permitted_urichars'] = 'a-z 0-9~%.:-'; $config['allow_get_array'] = TRUE; $config['enable_query_strings'] = TRUE; $config['controller_trigger'] = 'c'; $config['function_trigger'] = 'm'; $config['directory_trigger'] = 'd'; $config['log_threshold'] = 0; $config['log_path'] = ''; $config['log_date_format'] = 'Y-m-d H:i:s'; $config['cache_path'] = ''; $config['encryption_key'] = 'whatever'; $config['sess_cookie_name'] = 'ci_session'; $config['sess_expiration'] = 7200; $config['sess_expire_on_close'] = FALSE; $config['sess_encrypt_cookie'] = FALSE; $config['sess_use_database'] = FALSE; $config['sess_table_name'] = 'ci_sessions'; $config['sess_match_ip'] = FALSE; $config['sess_match_useragent'] = TRUE; $config['sess_time_to_update'] = 300; $config['cookie_prefix'] = ""; $config['cookie_domain'] = ""; $config['cookie_path'] = "/"; $config['cookie_secure'] = FALSE; $config['global_xss_filtering'] = FALSE; $config['csrf_protection'] = FALSE; $config['csrf_token_name'] = 'csrf_test_name'; $config['csrf_cookie_name'] = 'csrf_cookie_name'; $config['csrf_expire'] = 7200; $config['compress_output'] = FALSE; $config['time_reference'] = 'local'; $config['rewrite_short_tags'] = FALSE; $config['proxy_ips'] = '';

— Reply to this email directly or view it on GitHubhttps://github.com/benjamincurrie/CodeIgniter-Language-Switch/issues/2#issuecomment-8371624.

benjamincurrie commented 12 years ago

I'll try find time to fix it so that it also works with query strings a bit later, so if you need them you'll have to hold on for a bit.

Ben

On Fri, Sep 7, 2012 at 1:09 PM, Benjamin Currie ben@benjamincurrie.comwrote:

Set 'enable_query_strings' in your config.php to FALSE,

Also, you don't need to configure the 'segment_translations' unless you want SEO friendly URLs, in which case change url_style to SEO.

Let me know if that fixes it, Ben

On Fri, Sep 7, 2012 at 1:03 PM, Roscoe notifications@github.com wrote:

This might help:

language.php

$config['url_style'] = "Path"; $config['languages'] = array('english'=>'English', 'hungarian'=>'Hungarian'); $config['browser_language_detection'] = TRUE;

$config['language_codes'] = array( 'en' => 'english', 'hu' => 'hungarian' ); $config['segment_translations'] = array( 'hungarian' => array( 'index' => 'index', 'about' => 'rolunk', 'downloads' => 'letoltes', 'contact' => 'elerhetoseg', 'home' =>'otthon' ) );

autoload.php

$autoload['packages'] = array(); $autoload['libraries'] = array('database','form_validation','session','email'); $autoload['helper'] = array("html","url","form","language"); $autoload['config'] = array('language'); $autoload['language'] = array(); $autoload['model'] = array('user_model');

config.php

$config['base_url'] = '''; $config['index_page'] = ''; $config['uri_protocol'] = 'AUTO'; $config['url_suffix'] = ''; $config['language'] = ''; $config['charset'] = 'UTF-8'; $config['enable_hooks'] = TRUE; $config['subclassprefix'] = 'MY'; $config['permitted_urichars'] = 'a-z 0-9~%.:-'; $config['allow_get_array'] = TRUE; $config['enable_query_strings'] = TRUE; $config['controller_trigger'] = 'c'; $config['function_trigger'] = 'm'; $config['directory_trigger'] = 'd'; $config['log_threshold'] = 0; $config['log_path'] = ''; $config['log_date_format'] = 'Y-m-d H:i:s'; $config['cache_path'] = ''; $config['encryption_key'] = 'whatever'; $config['sess_cookie_name'] = 'ci_session'; $config['sess_expiration'] = 7200; $config['sess_expire_on_close'] = FALSE; $config['sess_encrypt_cookie'] = FALSE; $config['sess_use_database'] = FALSE; $config['sess_table_name'] = 'ci_sessions'; $config['sess_match_ip'] = FALSE; $config['sess_match_useragent'] = TRUE; $config['sess_time_to_update'] = 300; $config['cookie_prefix'] = ""; $config['cookie_domain'] = ""; $config['cookie_path'] = "/"; $config['cookie_secure'] = FALSE; $config['global_xss_filtering'] = FALSE; $config['csrf_protection'] = FALSE; $config['csrf_token_name'] = 'csrf_test_name'; $config['csrf_cookie_name'] = 'csrf_cookie_name'; $config['csrf_expire'] = 7200; $config['compress_output'] = FALSE; $config['time_reference'] = 'local'; $config['rewrite_short_tags'] = FALSE; $config['proxy_ips'] = '';

— Reply to this email directly or view it on GitHubhttps://github.com/benjamincurrie/CodeIgniter-Language-Switch/issues/2#issuecomment-8371624.

martonboti commented 12 years ago

Looking good, links are working, language menu changes the url. But the text doesn't appear. It's like the languages aren't loaded. If I load english with autoload for example, the text appears in english, I click the hungarian link and Unable to load the requested language file: language/hungarian/english_lang.php

Thanks for helping, I must be a pain in the ass.

benjamincurrie commented 12 years ago

No problems,

Looks like the last one is something to do with codeigniter, or your codeigniter setup. Usually you have a language file like main_lang.php, and that will exist in both folders.

Ben

On Fri, Sep 7, 2012 at 1:27 PM, Roscoe notifications@github.com wrote:

Looking good, links are working, language menu changes the url. But the text doesn't appear. It's like the languages aren't loaded. If I load english with autoload for example, the text appears in english, I click the hungarian link and Unable to load the requested language file: language/hungarian/english_lang.php

Thanks for helping, I must be a pain in the ass.

— Reply to this email directly or view it on GitHubhttps://github.com/benjamincurrie/CodeIgniter-Language-Switch/issues/2#issuecomment-8372324.

martonboti commented 12 years ago

Thanks Ben, it's working now.

benjamincurrie commented 12 years ago

Good stuff,

Feel free to improve on it if you find any bugs, I don't have much time these days.

On Mon, Sep 10, 2012 at 10:59 AM, Roscoe notifications@github.com wrote:

Thanks Ben, it's working now.

— Reply to this email directly or view it on GitHubhttps://github.com/benjamincurrie/CodeIgniter-Language-Switch/issues/2#issuecomment-8425430.

franzrecinos commented 12 years ago

Hi, I have the same issue. the language selector shows and I can switch from default, English to French. But when I want to go back to english, the text stays the same. My language files are in my system folder and not the application folder. Can that be the issue? Thanks

benjamincurrie commented 12 years ago

Doubt the language files in the system folder would make a difference, CodeIgniter checks both, and if it's loading french then it's finding the correct folder. Can't really help much without knowing more, take a look at the url's being created that could give you a hint as to what is going on.

On Mon, Sep 17, 2012 at 3:35 PM, franzverip notifications@github.comwrote:

Hi, I have the same issue. the language selector shows and I can switch from default, English to French. But when I want to go back to english, the text stays the same. My language files are in my system folder and not the application folder. Can that be the issue? Thanks

— Reply to this email directly or view it on GitHubhttps://github.com/benjamincurrie/CodeIgniter-Language-Switch/issues/2#issuecomment-8627946.

franzrecinos commented 12 years ago

Thank you for your answer. the problem is I had enabled compression and PHP was giving an error before compressing, hence nothing was being displayed and script stopped. French being the default language, on the english change I get error:

A PHP Error was encountered

Severity: Warning

Message: Constants may only evaluate to scalar values

Filename: hooks/Language.php

Line Number: 45

Which is this code:

if(file_exists(APPPATH.'controllers/'.$segments[0].'.php') OR is_dir(APPPATH.'controllers/'.$segments[0])) { // If so let's just go with the default language line 45 --> define('LANGUAGE', $config['language']); }

Now since mu controller name is login and the translated french is connexion, which is not a file or dir so just by putting a 2 before define I should be ok, but I wanted to know if the error I am getting is normal.

Thanks

benjamincurrie commented 12 years ago

Hm,

Not sure about that error, seems like $config['language'] is null or an array, not sure why or how, try dumping it before the define.

I would suggest whatever your default language is, you keep the controllers in that language. I'm not sure if it would make a difference but I never thought of or tested that.

On Tue, Sep 18, 2012 at 8:44 AM, franzverip notifications@github.comwrote:

Thank you for your answer. the problem is I had enabled compression and PHP was giving an error before compressing, hence nothing was being displayed and script stopped. French being the default language, on the english change I get error:

A PHP Error was encountered

Severity: Warning

Message: Constants may only evaluate to scalar values

Filename: hooks/Language.php

Line Number: 45

Which is this code:

if(file_exists(APPPATH.'controllers/'.$segments[0].'.php') OR is_dir(APPPATH.'controllers/'.$segments[0])) { // If so let's just go with the default language define('LANGUAGE', $config['language']); }

Now since mu controller name is login and the translated french is connexion, which is not a file or dir so just by putting a 2 before define I should be ok, but I wanted to know if the error I am getting is normal.

Thanks

— Reply to this email directly or view it on GitHubhttps://github.com/benjamincurrie/CodeIgniter-Language-Switch/issues/2#issuecomment-8652906.

franzrecinos commented 12 years ago

You where right, my language variable stayed an array from another lib I tried before. Thanks for this lib and your help.