Get started with PHP-Fusion

Start a New Thread

Users Participated

  • Craig
    Post made: 5
  • Basti
    Post made: 1
  • Chan
    Post made: 2
  • JoiNNN
    Post made: 6

  1. PHP-Fusion Support Forums
  2. Development & Design
  3. Roadmap

Use gettext for locale

[Development Team Access] All Roadmap items end up here for discussions.

13 Replies 4,032 Views Last Updated on 3 years ago

JoiNNN


Veteran Member

#1

Posted 3 years ago

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.pontik...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
Code Gist: Download source  

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
Code Gist: Download source  

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


Go to roadmap item #1499

Posts: 862

Joined: 05/02/2011

Craig


Moderator

#2

Posted 3 years ago

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

Posts: 4531

Joined: 27/09/2005

JoiNNN


Veteran Member

#3

Posted 3 years ago

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
Code Gist: Download source  

echo _("Hello world!");

but not the translated string in Romanian then
Code Gist: Download source  

Hello world!
will be displayed.

Posts: 862

Joined: 05/02/2011

JoiNNN


Veteran Member

#4

Posted 3 years ago

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:
Code Gist: Download source  

$locale['123'] = _("Hello world!");

Posts: 862

Joined: 05/02/2011

Craig


Moderator

#5

Posted 3 years ago

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...

Code Gist: Download source  

 echo _("Hello world!"); 


Where will it find the Romanian words for this?

Posts: 4531

Joined: 27/09/2005

Chan


Super Admin

#6

Posted 3 years ago

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.

Posts: 3454

Joined: 25/09/2007

Craig


Moderator

#7

Posted 3 years ago

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

Posts: 4531

Joined: 27/09/2005

JoiNNN


Veteran Member

#8

Posted 3 years ago

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
Code Gist: Download source  

msgid "Unknown system error"
msgstr "Eroare de sistem necunoscuta"

Edited by JoiNNN on 23-06-2014 18:50

Posts: 862

Joined: 05/02/2011

Craig


Moderator

#9

Posted 3 years ago

Ah, ok! Good

Posts: 4531

Joined: 27/09/2005

Chan


Super Admin

#10

Posted 3 years ago

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. Smile

Posts: 3454

Joined: 25/09/2007

JoiNNN


Veteran Member

#11

Posted 3 years ago

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

Code Gist: Download source  

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.phpettext.php
Code Gist: Download source  

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.

Edited by JoiNNN on 23-06-2014 19:58

Posts: 862

Joined: 05/02/2011

Craig


Moderator

#12

Posted 3 years ago

I did some researching in the past hour and have concluded my thoughts on it.

Go for it, implement it. Good idea. Looks good. Good

Posts: 4531

Joined: 27/09/2005

Basti


Moderator

#13

Posted 3 years ago

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
Code Gist: Download source  

include LOCALE.LOCALESET."members.php";


replace with.
Code Gist: Download source  

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


Problem solved.
Just my two cents

Posts: 1103

Joined: 09/04/2007

JoiNNN


Veteran Member

#14

Posted 3 years ago

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.

Posts: 862

Joined: 05/02/2011

Jump to Forum:
4 users are online
1 member and 3 guests

afoster