Build website in 2 language where in user enters his data also - php

I have got idea of how to build a website in 2 languages, i have a switch and each language text in file and user it. But my website is a website where all freelancers register and post their projects and so. How do i control in what language users enter data. If user entered data in English, how do i show data in other language ?
Example: take 2 language English and Arabic
Case 1 :
1. User enters data only in english.
2. How do i show this data in Arabic??
Case 2:
1. How to make user enter data only in either Arabic or English??

Depending on how much content there is you may need to employ a dedicated translator.
I wouldn't suggest automatic translation or at least be very, very careful with it, as users don't like that at all (e.g. I definitely prefer reading text in English than an automated translation in my native language that some website oh so helpfully decided to show me based on my IP).

It depends on the site settings, you will have to use a language translator and set it up on your website accordingly.

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.

How to dynamically change website's language using PHP-MySQL?

I have PHP-MySQL Website with admin panel. I can enter data from admin panel and they display on user side web pages. Now, I want that I should be able to insert text data in English language from admin panel and they should display in 3 different languages at user side.
At user side, I want to give options for visitors to select language preference and user sides pages should display in that selected language. Right now I am completely unaware for what programming related changes I will need to make on user sides pages to make this possible.
I have searched and read some articles on internet but frankly could not find solution that will work for my case. So please help me.
Following things are used / set in my working environment.
Windows Server 2003
PHP 5.2.17
MySQL 5.0.51a (UTF-8 Unicode (utf8))
XHTML 1.0 Transitional
meta charset=iso-8859-1
Thank you in advance,
KRA
Assuming you have all your language variables and/or html views separated out into some sort of structure, you could always just use a cookie.
Have your site check for the presence of said cookie and if it doesn't exist, then have the site revert to the default language, otherwise adjust the language accordingly.
Ex:
Have a form with dropdown values for each language and when it is submitted, set the language cookie variable:
setcookie('userLanguage',$language_value,strtotime("+1 year"));
The first parameter is the name of the cookie variable, the second is the value (in this case, the user's language selection, and the third is the expiry date of the cookie (I made it one year, but you can set it to whatever you like).
The next step really depends on how your site is setup, but I'll assume that you have your language text in a db or handled in some intelligent way. Either way, you need to retrieve the cookie:
if ($_COOKIE['userLanguage'] <> '')
// Do some language stuff here based on the value of $_COOKIE['userLanguage']
else
// Do some stuff here for default language
That's about it. There are a lot of different ways to handle the actual language conversion, but the act of getting the user's preference and remembering it is pretty much as basic as that.
Assuming that you want the translations performed on the server side (PHP) you can use file_get_contents to fetch data from Google Translate API. Then you need to parse the response and get translated text. You need to get API KEY to access the Translate service.
<?php
$string = 'Hello World';
$source_lang = 'en';
$target_lang = 'zh-CN'
header ( "Content-Type: text/html;charset=utf-8" );
$data = file_get_contents ( 'https://www.googleapis.com/language/translate/v2?key=INSERT-YOUR-KEY&q='.urlencode($string).'&source='.$source_lang.'&target='.$target_lang );
$data = json_decode ( $data );
$translated = $data->data->translations->[0]->translatedText;
echo $translated;
?>
Server responses are JSON objects with that structure:
{
"data": {
"translations": [
{
"translatedText": "Hallo Welt",
"detectedSourceLanguage": "en"
}
]
}
}
More info about basic concept is avaliable on:
http://baris.aydinoglu.info/coding/google-translate-api-in-php.
Documentation of Google Translate API queries:
http://code.google.com/apis/language/translate/v2/using_rest.html
You may use this and save the relevant data to your database. You may have separate columns for each language in your database table. Once the text / data is entered by the admin, you have to convert them using the solution and save them to relevant columns on the SAME row. The user may be given data from the relevant column according to their language selection.
Well this depends alot on how you store your textual data. If you have it in a db (mysql) then i suggest that you have a collumn that stands for the language. Then you could have either a session variable or a specific url or something else that gives you some indication of which language the user is requesting.
What you need is some way to figure out what kind of language your user wants, after that it's quite easy.
Have you tried anything yet?
Use language Files in your website.
For example,
For english -en.php
For French -fr.php
This files will have variables that will be used in front User end.User can select any language and on selecting, your code will load the following language
In your case, you are using database, so you can have this thing in your tables instead of Database
so your Front files will consist of variables instead of Text and this variables will be replace by language variables(Either from database or from files, its your choice) when user select a language
And yes, you need to put a default language.
Use following links to implement :-
http://scriptdigital.com/divers/phplocalization.html
http://www.mind-it.info/2010/02/22/a-simple-approach-to-localization-in-php/
http://code.google.com/p/slsphp/
Use this method.
Its working for me.
Switching languages on a website with PHP
I've done it in oyutrade.com

Translations for non dynamic data

I am currently making a web page with dynamic content from a database (a restaurant menu) storing item names, prices, yada yada. The restaurant usually caters to English and Japanese. I have stored the titles of the items in the database as English and Japanese, however when it comes to the type of the item, for example, Type: Drink, Type of Drink: Coffee I can obviously store the type of item alongside the item in the database, but it hardly seems worth it to store it twice as two different languages in the the database which would also make the form to add a new item a pain.
What is the best way to go about translating those little tiny pieces of information, like "Coffee", "Wine", "Click Here", "Menus", and using them on the page?
You can use gettext extension for translation static or predefined info on web page.
See manual http://php.net/manual/en/book.gettext.php
Example http://mel.melaxis.com/devblog/2005/08/06/localizing-php-web-sites-using-gettext/
I would store it in both languages, it makes the most sense to me. I don't see why you would go and do a request to your translation system each time you want to show any page in your menu.
If you are using an automatic translation system that you really trust, one thing that you could do is use it when adding a new item to make a suggestion. So, after the user enters the word in English on the form, automatically attempt to translate that into Japanese and put it in the Japanese field. If the user is satisfied with the translation, that's it for that task, otherwise he/she inputs with a better one.
if you are using any phph5 based framework , major framework have localization support like zend has Zend_Locale
if you want your own translation dictionary , keep key value pair data in database table and memcached between application and DB , so static data will be cached for particular expiry time and data base load will be get decreased and fast retrieval of data

Internationalizing a web application

Not sure if stackoverflow is the most appropriate place for this question.
I am currently building a web application that will manage content, ecommerce and allow users to participate.
I am using php's intl extension to deal with different number formats and so on. gettext will be used to provide translations of the actual text in the application.
This question isn't really a technical one, but more as to how the user sets the language/version of the web app.
I have came up with a possible scenarios:
(Country regions) User selects his country and this determines his language. If the language for his country is not avaliable, default to default language.
(Language) User selects his own language, and he selects his own country/time zone settings. That means we can have users in say the United states reading content in Japanese.
With the first option, obviously it is quite fool-proof. The user claims he is from canada, so we display the content in english (en_ca) or french (fr_ca if avaliable).
However, not every web site built on top of the application will be big enough to warrant translations based on locale (i.e. canadian french (fr_ca) vs french french (fr_fr).
Some site would be quite small, so it doesn't matter where the user comes from, he should be able to select either French or English when browsing the site. However, this leads to another issue. If the user selects the content to be shown in french, how do we format things like numbers and currencies? We cannot directly infer his locale from the language he has chosen.
I have looked around the web at some "big" websites to look at how they do things.
Amazon - (Country Regions) Different sites for different countries, i.e. Amazon japan is only avaliable in japanese.
Microsoft - (Country Regions) Some countries have multiple languages. (fr_ca) vs (en_ca).
Engadget.com - (Language) English, Japanese, Chinese (both simplified and traditional).
FaceBook - (Language), but offers different versions of languages (fr_ca and fr_fr)
So I guess the question is, should I force all users of the web application to build their content around the countries/regions or languages? Or should I allow both scenarios?
Cheers :)
As a professional localizer, I reccomend "language" if there is no difference in content.
The reason why Amazon or MS ask for country is that the content is different for each territory. For stuff that is not available, say in Japan, why would they want to translate it to Japanese? Plus it's misleading to the Japanese reader, even if they are on the US site.
On the flip-side, if you offer Country as the selection, then users may expect to see different content based on the coutry they choose, and that might be misleading.
These days a lot of people are mutli-lingual so if the content is the same, defaulting by location then offering the language selection on top is more user-friendly.
I hope my thoughts might help. This is a tough question and depends on many factors - there is no 'right' answer. :)
We did something similar to this in our web app. We essentially just used the currency format of the "home" country for the language. So, if they choose French, they get France's formatting. German, Germany's formatting, etc. It won't always be the correct one but for our app we haven't had any complaints yet, so I assume that works the best if your users aren't too picky. We have a decently small userbase, however (<= 1000 visitors per instance of our app), so you may want to take this advice with a grain of salt ;)

PHP Localization and dynamic data

So I need to make a site available in different languages. Using PHP 5.x and MySQL 5.x. I believe I will be using gettext which seems fine for static text throughout the site but what about dynamic data that is stored in the db? Im referring to things like stories, events business listings etc. How do I get those to display in a different language? My initial thought was in the backend have them be able to enter multiple versions of a story, event or listing, one for each language they want to use on the site. But there could be thousands of entries times how many languages they want to show. Is there a better solution/idea that someone can point me to?
Also another issue I was thinking is currently the site allows you to search events/stories/listings, how would that work in different languages? Im assuming if someone selected the site to show in spanish they are going to use spanish words to search the site, but if the information in the db is in english I dont know that would work. Any suggestions on that?
If you want the stories to be correct in all languages, then you need them stored in all languages and provide the backend to do the translations or enter the stories on different languages as you initially thought.
If you don't like that, you might add the google translate element in your pages, to provide automatic (not correct) translation.
For the search question, I would only search for the keywords in the fields of the database in the same language as the user. If Joe is visiting your page in English, only look for the search terms in title_en, content_en, description_en fields of your database (or the ones with language='en' if you have one row per article and translation in the database, instead of one row per article (with all translations inside)). Obviously, this does only work if you put all the translations on the database.
The best solution I have seen described is this.
As i18n CMS sites as you describe are in a constant state of flux, with new articles being added, some of which have had translations, in some languages.
If an article in a chosen language has not yet been translated then show a default language (English?).
Then pick any ideas which appeal to your case:
a) If showing a default article in English also throw onto the page an input box and invite your audience to translate it for you.
b) If showing a default article in English also put on the page an offer to send the content to google translate as well as doing a) above
c) put a bounty on the translation and optionaly do a) and/or b) above.

Categories