Add URI Segment for Language in Codeigniter - php

I have a site; I need to develop to support multiple languages, for example en, tr, ar, sp, etc.
But the problem is , what is the best way to do it with Codeigniter?
I have a controller ( SITE ). I have a lot of functions LIKE ( hotel , city , page , blog )
I want to before this method add a segment ( language ). This is old links
http://example.com/hotel/mariot
http://example.com/hotel/hilton
http://example.com/city/london
http://example.com/city/paris
...
..
and I want the links to be like this
http://example.com/en/hotel/mariot
http://example.com/ar/hotel/mariot
http://example.com/sp/hotel/mariot
http://example.com/tr/hotel/mariot
http://example.com/en/city/london
http://example.com/ar/city/paris
....
..
sometimes thinking every language have a controller but it is possible...
What is the best way to change all links to support the languages?
I'm sorry If my question is not clear, this is my English.

The right way, is using routes.php look for it inside (application/config/routes.php)
You can use regex expressions for your specific problem.
here is the manual: http://ellislab.com/codeigniter/user-guide/general/routing.html

if you want your links to be like this
http://example.com/en/something
then you might need to have subdomain or folder in your project called en. but yet this is not the perfect solution.
what you have to do is to make your text output assigned into variables which takes their values from an xml file. like if you have some text called "your room number is: 0" and this value is hard coded HTML then you should put it in en.xml file under some tag and get it from the xml file which is requested by the user according to the required lang.
you might can do the previous solution in all languages but you have to consider that Arabic language is RTL language so just consider that in your css. But generally you can use xml files to control the languages.
hope that is what you needed.

Related

Web translating - single or separate files for different languages?

SHORT DESCRIPTION
It will be Lithuanian - English website (only 2 languages).
This will be jobs listing website, so content shouldn't be translated. If employer provided job in English so this should be in English even if Lithuanian language is selected. There should be translated only field names, buttons & error messages.
QUESTION IN SHORT
What is better way to have single file index.php for both languages or create separate files for LT and EN languages?
1st WAY
So I can make It in few ways, for example:
www.mysite.com/jobs -- default Lithuanian language
www.mysite.com/en/jobs -- English language
So in this case I need to have 2 duplicate websites (just translated) Lithuanian stored in root/ folder and English stored in root/en/ folder? So If I change something in Lithuanian website I need to make exact changes in English website?
It not looks like good practice to write code twice.
2nd WAY
Create variable something like $lang = "en";, store all field names in database in way like this:
Id FieldName_lt FieldName_en
1 Vardas First Name
2 Pavardė Last Name
3 El. paštas E-mail
And select from database via PHP like SELECT FieldName_ . $lang FROM table..., but It could be SQL Injected If I'll use variable in SQL query.
3rd WAY
Maybe It's better way to store field names in arrays (there will be maybe 150+ fields)?
If I'll go for 2nd or 3rd way, should I save language choice in cookies? So in this way website url always will be like below?
www.mysite.com/jobs.php?lang=en
www.mysite.com/jobs.php?lang=lt
Maybe there is another way to make It, without showing language choice in address bar at all? Or It's bad practice to hide language choice form address bar?
ADDITIONAL
In HTML form I'm using data validation in following:
<input type="text" id="first-name" placeholder="" required
data-validation="length alphanumeric"
data-validation-length="3-12"
data-validation-error-msg="User name has to be an alphanumeric value (3-12 chars)"/>
So how about error message translation?
The same as database approach you can use static file for each language and translation.
en.php
return [
"somekey" => "English Translation"
];
lt.php
return [
"somekey" => "Lithunian Translation"
];
You can then mod rewrite to get language from url if you want some directory structure, or simple query parameter or cookies (as specified by others). If you are using any any RESTfull service it is also possible to set it in HTTP header. Many frameworks also there to help you parse data from url out of the box.
$langCode is language code fetched from Query PAram, url path, header or cookie
you can also use http://php.net/file_exists to check if translation file is available or not before you use require_once to pull the translation resource.
Once you get the language code you can just use
$stringResource = require_once "lang/{$langCode}.php";
Then you can fetch all the resource by its key from $stringResource.
<?php $stringResource = require_once "lang/{$langCode}.php"; ;?>
<input type="text" id="first-name" placeholder="" required
data-validation="length alphanumeric"
data-validation-length="3-12"
data-validation-error-msg="<?php echo $stringResource['somekey'] ;?>"/>
You can just edit the translation in editor. wont need to connect to database and as it is just assoc array. it would be way faster.
Query:
www.mysite.com/jobs.php?lang=en
as already mentioned it will be ignored in term of SEO. Query parameters are ignored by crawlers.
URL Path
www.mysite.com/en/jobs.php
here you need to do mod rewrite http://httpd.apache.org/docs/2.0/misc/rewriteguide.html which is basically just catch url and fetch out the en part and rewrite to something like www.mysite.com/jobs.php?lang=en
Both data can be get from $_GET['lang']. but url path will have benefit on SEO.
Cookies
Will not be shown in address bar. but that also means if the link is shared to another user they will not see the language of origin they will see default language.
https://support.google.com/webmasters/answer/182192?hl=en#1 as per google doc. i believe it would be nice to do it using url path.
I think the approach with the Database is fine. Whenever you want to change the translation for something you will just have to edit the table entry. You can write a class which doeas the translation for you, so you only have to pass the language-id and the language.
How to save the language/ how to display it: It depends on how it is meant to be used. If it is likely people often share a link to your site, you could inclue the language in the url, e.g. as a GET paramater. If it should "just" stay the same for the user who visited the site, cookies are a nice approach.
Using a cookie to store the language preference is an option but might cause some issues for SEO and this will be relevant for a job site.
Without using cookies you can either put the language in the directory path as you suggested. You don't need to have 2 separate websites then, you can use url rewriting to change a part of the path into a query parameter (for apache use mod_rewrite).
Using just a query param, just as your second suggestion will work too but looks less nice.
Make sure you offer your users a option to switch language, for example using flag icons or just text links.

Best way to organize your localized translation file

As I've started building a project, there will be quite a few entries in the .po translation file. I use Poedit to build these.
My question is, what is the best practice for entries within this file? I was thinking, instead of referencing entries such as:
echo _('This is an entry.');
I was thinking of organizing them like:
echo _('error_pwd');
echo _('error_user_taken');
Which, once ran through the translation file, would output something like:
Password incorrect. Please try again.
Username is already taken. Please try another.
So, all my translations can be organized by type, such as error_, msg_, status_, tip_, etc.
Has anyone seen it done this way, or have any suggestions on a more organized method?
In fact it doesn't matter!
It's just up to you.
However, I advise you do not split translations in sections.
No there's any benefit in doing so. Actually, the most projects use the one file approach for all msgid entries.
Like django, see.
Of course, If you still want split translation by sections, might you want take a look on Domains:
From PHP doc:
This function (textdomain()) sets the domain to search within when calls are made to
gettext(), usually the named after an application.
Also, as earlier said, the advantage when using msgid as a real phrase ou word (instead underline or dotted notation key) is that it stays as default message if no there's a translation for entry.
And here goes some helpful links:
Django Porject - i18n, Definition
PHP textdomain function
What is bindtextdomain, textdomain in gettext?
How to determine which catalog to be used
This is a standard approach for other framework, e.g. Symfony/Laravel:
trans('error.validation');
But it has a downfall, if you forget to translate one phrase on your site it will appear like the keyword 'error.validation'

localization in codeigniter based on cookie value

I'm trying to rewrite asp.net mvc application to codeigniter.
Basically codeigniter follow mvc pattern so it's pretty much ok. Now I'm stuck at localization.
I do not want to change url, in order that /Company/About remains the same in english and german. Inside view I had in asp.net Views/Index.cshtml as default for german and Views/Index.en.US.cshtml for english localized page.
I will describe scenario which works perfect on my asp website.
User clicks on country flag
Based on 1. step value cookie is populated with country value
Helper load desired thred into current thread
Views are localized
How can I apply this approach to codeigniter, or similar at least?
Thanks
I think it will be not a good idea to have separate files for each language. You can do it easily using some other techniques, which are easy to implement.
While working on one of my CI project, i need to make it to support multiple languages. As i worked in other multilingual systems like Prestashop, so i borrowed ideas from there and implemented it in my CI project.
I have implemented it as followed:
1) I am storing words in language files. Each language has its single file named as language ISO code, like for english its name is en.php In this language file, words are stored as file_name_md5(of the word) in array like below for Hello World in view file hello.php .
$_lang = array(
'hello_b10a8db164e0754105b7a99be72e3fe5' => 'hallo Welt',
...
...
...
)
The key of the $_lang associative array is the world appended with file name, to be translated, and the value is the translation.
Storing words and fetching the words into / from these language files, are handled by the Translation library I created.
2) All my static text in views files are written in english. I created helper function for it called "l" , small L . Lets say i want Hello world to in my view (say hello.php) and should be translated into multiple languages. So in my view i write it like
<?=$this->l('Hello World...', 'hello')?>
3) Now the l helper function perform small operation on the arguments passed to it. It takes md5 of the world and append it with the file name as you can see above. Then it calls a member function of my Translation library, which looks into the $_lang array to find that match. If it finds the match, it returns the translation. If no translation is found for that word, then the l helper function returns the original text back.
4) I have created my own controller library from which all my controllers are extended. To preserve CI features, my parent controllers are extended from default CI controller. In my parent controller, i load the language files according to the user language. This way the $_lang array is available for the Translation library to look into for the words.
5) In my admin side, i have created a translation system, which reads all my View files for a specific pattern like the below one
<?=$this->l('Hello World...', 'hello')?>
The code generate a form in which a text field is created for each word. The text field name is the same as the $_lang array keys, like filename_md5_of_word . The text field label is the original word in this case "Hello World...". And the translation has to be written to the text field.
On saving, the translations are stored in to specific language file for that particular language selected for translation.
Using this method, you will be able to add as many languages as you like in future, without creating view files for each language, so it is flexible.
I hope i have explained enough so you can take the idea of how easily you can implement translation system, and avoid separate view files for each language.
If you have any questions, feel free to contact me here.
Thank you

Php multilingual website

I am working on a website and the requirement is to make it in two languages i.e. icelandic and english.
just like facebook and other google, if a user selects a language, then the site is translated in that language.
I am not allowed to use google translator.
Any other way to do this in Php
Thanks in advance
Well, I never did it, but i did think about it :), for me i have to do something like this from scratch,
First, do not echo your String that will be displayed to your clients hardcoded, create a dictionary, this dictionary can be in any format, be it php file, xml file, json. You can also extend the functionality by adding Database in it. The main idea is to create a dictionary having all your messages that will be displayed to the user in all the languages you want to display it
consider if you do it using normal PHP FIle, use OOP built class say known as Message, then as attribute to the class add the several languages that you have to use and also some setters and getters
e.g.
Message
{
english;
french;
.....
}
then in PHP, when you echo your messages, try to get the language you want to use, and then
do something like this
echo message.getEnglishMessage();
Look, I've been very generic, now decide on the type of file that you'll use and build the dictionary
Hopes it helps :-)
I use an es.php (spanish not sure what icelandic is) and build all of the mod_rewrite off that. You treat it exactly same as you would if it were the index.php for english. For inputing data into the database have a column for language. All of your queries that call data will then have the language as a condition.
The "gettext" is the way you can go with but if you and your client are in nice understanding ask him to provide the data in language other than english as well and then in DB table there will be a column 'language' in which 'ic' or 'en' flag will be the data, and during fetching the data anywhere, according to language your sql query will contain the language as a where condition with desired flag as its value.

Best way to build a multilingual site with codeigniter?

My current approach has been to use to _remap function provided by codeigniter to get the URI segment in order to check if the language is "en" or "np"
Here is a sample:
function _remap($url_title){
$this->_identify_language($this->uri->segment(1));
$data ['sub_categories'] = $this->category_model->get_category_list_by_url($url_title)->result_array();
$data ['news'] = $this->news_model->get_news_list_by_url($url_title)->result_array();
$data ['url_title'] = $url_title;
$this->_render_front_view('main',$data);
}
I am using this technique on every controller. Which is well not very efficient.
I wanted to ask if using sessions to store language codes would be better or is my current technique good enough?
Are there any other ways i can do this multi-lingual thing?
Of course my database is currently shaped for 2 lanaguages and i have seperated the fields. e.g:- title_en, title_np. these are echoed according to the language field used.
Lots of parts to this.
Your URL's do not really need to be /en/ and /fr/ unless you want it to be used for Google Analytics. Spidering doesn't make a lot of difference. Accept-Language headers can be just as reliable.
Globally parse this URL segment. You can use this method or the Accept-Language, but either way you need a hook, a MY_Controller or extend the Lang class.
Think about if you want the different languages to be totally seperate. For example, if I have an English page not translated to French, and the French page does not exist, should it show the English page or 404? You can either store the lang = fr in the database and take the value from a constant set in the hook/MY_Controller/etc.
WHERE lang = CURRENT_LANGUAGE
Structure your DB. title_en title_fr is one method, but it soon because unmanagable with lots of languages. Have a "pages" and "page_content" table, so that all generic information is in one table then all language specific (title, content, meta, etc) is in the page_conten table, which has a lang field.
There are a million ways to do all of this, but there is lots more to think about than just the URL. My favourite
I have been using this internationalization library for codeigniter and I find it suits my needs pretty well.
It extends the Lang class, and then in the constructor it parses the URI to figure out which language to use. So it is just loaded before you use any language files. You don't need to add any code to your controllers. It simply changes the setting in the language object. So you can retrieve the current language the same as you normally would:
$this->lang->lang();
If you have 500 news and 2 languages, changing url prefix in root will give you 1000 links, lets say "/en/hello-world" and "/np/hello-world" will have identical content and possibly the same title, which can be bad from SEO aspect. I would use session or cookies to store preferences, to preserve link juice.

Categories