Oh no! Where's the JavaScript?
Your Web browser does not have JavaScript enabled or does not support JavaScript. Please enable JavaScript on your Web browser to properly view this Web site, or upgrade to a Web browser that does support JavaScript.
Welcome to the Official Site of PHP-Fusion
Sign In
Not a member yet? Click here to register.
Navigation
13 Replies | Latest reply on 03-01-2017 04:08 by JoiNNN
JoiNNN
JoiNNN Posted 4 years ago 859 posts

Use gettext for locale

This feature has been requested in the past for older versions of Fusion but mostly got ignored because most people didn't know what it was and how it works. I simply can't stress enough how much better and easier will gettext make everything for implementing locales, translating and managing locale files.
Please before forming an opinion about this go get some information about it. There is one of the many implementation examples around the web http://www.pontikis.net/blog/php-java...ext-poedit, might seem tricky to implement at 1st but once it's done everything will come into place.
It's also safe for backwards compatibility as long as the infusions and themes use their own old locale files and variable references. Either way sacrificing such a great tool for localization just for backwards compatibility makes no sense.

To give you an idea about it, here is how a locale looks now
echo $locale['123']; // yea... no fkin' idea what this is it, better go open locales files find what it is

this is how gettext does it
echo _("Hello world!"); // it's pretty clear what this text is about


Go to roadmap item #1499

13 Replies

Sort replies by
Craig
CraigPosted 4 years ago 4531 posts
Hi,

Can you please show me an exact example of how this would work in PHP-Fusion?

I mean can you show me how this would work in English.php and the file.php?

Thanks
JoiNNN
JoiNNNPosted 4 years ago 859 posts
Is all well explained in the example link , just have a look Craig.
Also wanted to mention that there is also cross-platform tools like Poedit specifically developed for extracting and then translating the text making everything so much more easier. So all you have to do after developing something is to indicate to Poedit where .php source files are and it will search and extract the locale text lines automatically then all you have to do is to translate them and click save.

Oh and there will no longer have missing text and undefined variable indexes due to missing locales, if a translation for a string is not found the original string will simply be displayed. So if my website has Romanian set as locale and I have somewhere in the code
echo _("Hello world!");

but not the translated string in Romanian then
Hello world!
will be displayed.
JoiNNN
JoiNNNPosted 4 years ago 859 posts
For backwards compatibility we can develop a plugin as a layer that will make old locales available. It will be made as an optional download for those who are too lazy to translate their code.
Example:
$locale['123'] = _("Hello world!");
Craig
CraigPosted 4 years ago 4531 posts
Sorry, me and reading do not go well but tell me something so I don't need to read this stuff to find the answer because these pages of documentation never give me an answer I understand.

Is all words for languages in a flat file or something where are the words?

I mean if I code in...

 echo _("Hello world!"); 


Where will it find the Romanian words for this?
Chan
ChanPosted 4 years ago 3478 posts
A locale file? It's an engine / medium to pair the word to word instead by array keys. I am afraid of sentence structure out of the translations...?

My questions is administration interface. We need a core admin file if we do this.
Craig
CraigPosted 4 years ago 4531 posts
Ah,

So it could happen to translate wrong like Google Translate does 90% of the time in the wrong context?

Example: This bad is a translation very.

In that case, no. Leave locales the way they are. We discussed locales plenty in the past so there is already lots of nice ideas lying around or at least there was. Someone suggested 1 file for admin 1 file for front end. I say just leave it for now then when version 9 comes change the locale way big time to the simple solution not yet invented. All is good either way as long as it works nice eh.

Regradss
JoiNNN
JoiNNNPosted 4 years ago 859 posts
gettext has a standard folder structure so it would look something like this
locale/ro_RO/LC_MESSAGES/global.po
ro_RO is the language and LC_MESSAGES must not change as is the standard
also there will be no en_US folder since the default language is in the code.
This is how a string is translated inside a .po file
msgid "Unknown system error"
msgstr "Eroare de sistem necunoscuta"
Craig
CraigPosted 4 years ago 4531 posts
Ah, ok! :G
Chan
ChanPosted 4 years ago 3478 posts
In a way, It can stabilize isset errors of missing locale files for sure, and coding can get heck lots faster. I like it on these points. The site can be switched to installed languages by user preference with this.

My ... the move would require us to update almost all files. :)
JoiNNN
JoiNNNPosted 4 years ago 859 posts
Let me give you more details about how this is better, I just want you guys to get a good idea about this:
- we will have the plain default text directly in the code making it way more easier to read and what surrounding code is about [no more arrays and number keys]
- if a string is not translated the default EN string will be displayed [no more missing variable errors and missing text]
- has native support for plurals, you simply translate all the plural versions and in then add the code based on values you expect. Looks like this
file.po, we define in the header of the file how many plural versions a language has, Romanian has 3 and it looks like this

nplurals=3; plural=(n==1 ? 0 : (n==0 || (n%100 > 0 && n%100 < 20)) ? 1 : 2);

msgid "%d term"
msgid_plural "%d terms"
msgstr[0] "%d termen"
msgstr[1] "%d termeni"
msgstr[1] "%d termene"


file.php, more details here http://www.php.net/manual/en/function.ngettext.php
printf(ngettext("%d term", "%d terms", 1), 1); // if just one termen
printf(ngettext("%d term", "%d terms", 2), 2); // if 2 termene
printf(ngettext("%d term", "%d terms", 20), 20); // if 20 termeni (this is incorrect grammatically but is just for demo)

- there are dedicated tools for translating the files, they automatically extract all the localized strings from source code

DISCLAIMER:
Keep in mind that I might be wrong about some things as all i explained you was not put in practice at all as I don't have the possibility at the moment, this is all from research. If you want to understand better gettext go google it and do research and testing.
Craig
CraigPosted 4 years ago 4531 posts
I did some researching in the past hour and have concluded my thoughts on it.

Go for it, implement it. Good idea. Looks good. :G
Basti
BastiPosted 4 years ago 1103 posts
This is a good idea. I would go for it.
But maybe not for v8.0. This could be a todo for v8.1. because you need to rewrite all files.
This rewrite will last weeks.

The problem with the missing Locals can be fixed temporary very easy:

example:
open files/members.php

Search for
include LOCALE.LOCALESET."members.php";


replace with.
include LOCALE."English"."members.php";
include LOCALE.LOCALESET."members.php";


Problem solved.
Just my two cents
JoiNNN
JoiNNNPosted 4 years ago 859 posts
Nice trick Basti with overwriting locales.
About implementation it's either done now in 1st 8 stable release or 9(?), it's quite a change and should be done in a major version.

Actions

Sharing

More topics like this

You can view all discussion threads in this forum.
You can start a new discussion thread in this forum.
You cannot reply in this discussion thread.
You cannot start on a poll in this forum.
You cannot upload attachments in this forum.
You cannot download attachments in this forum.
Moderator: Development Team
JoiNNN

Use gettext for locale
by JoiNNN Posted 4 years ago