xgettext vs gettext - php

I have a few questions:
I know what gettext is. I've read a few posts where they mentioned xgettext and was curious as to what is the difference between the two.
How can I install xgettext on Windows?
And finally, does anybody have a tutorial on how to install the library php-gettext http://savannah.nongnu.org/projects/php-gettext/ (this one usually doesn't come with PHP) I've read about it in an article but I'm not sure how to get it working in Windows. The thing is, sometimes when you make changes, you need to restart Apache to see the new data with the gettext that comes with PHP (but with the library you don't need to restart it) so I wanted to use the library for development. Thanks!

In regards to the question:
I know what gettext is. I've read a few posts where they mentioned xgettext and was curious as to what is the difference between the two.
In short, gettext() is a function and xgettext is a utility program for extracting messages from source code.
In long, SO answer to Complete C++ i18n gettext() “hello world” example shows as part of the C++ source code file hellogt.cxx:
gettext("hello, world!")
The gettext() function is passed a text string that is used as an index to the message to be used at run-time. It returns the specified message for the language which is specified either in the code or at the time the program is invoked.
Then it shows:
xgettext --package-name hellogt --package-version 1.2 --default-domain hellogt --output hellogt.pot hellogt.cxx
which is a utility program used at build time to examine the source code file hellogt.cxx for text strings passed to gettext(). These are extracted and used to create the Portable Object Template file hellogt.pot.
The .pot file template is used by translators in the process of delivering the binary translated message file hellogt.mo used at run-time by gettext().

Install Cygwin and select the gettext-devel package.
This will install the xgettext.exe
The Zend Framework has a gettext Zend_Translate adapter that doesn't require the php gettext extention.

xgettext is part of gettext, it's a program that extracts translatable strings from program sources. See gettext's manual.
I don't know about its availability on Windows, Google tells me there's a port.

The online function reference reference tells me there is no xgettext.
Maybe they mean one of
ngettext dgettext dngettext dcgettext dcngettext
treating the 'x' like a wildcard

Related

how to check php function source code using eclipse

I'm curious on how certain php functions are implemented internally. e.g. array_values().
So in eclipse, I control click on the function name, which took me to a page that contain function prototype definition, but contains no internal source code.
Is there any way to see the internal implementation of php function using eclipse? (whether the function is written in php or c)
If it is not possible to see the php source code using eclipse, then does anyone have any good strategies at searching through the php source code on github?
Is there any way to see the internal implementation of php function using eclipse?
Unfortunately, no.
what is the best search strategy to search through the php source code, especially for a beginner like me, who feels very much lost in the vast amount of php source code
I'm assuming what you are really after here is a reference for native PHP functions and their input parameters and out types. In which case the official documentation is probably the best way to go about it.
Some (most?) popular IDEs such as Eclipse and Phpstorm can also give you an auto-generated phpdoc block for PHP's built-in functions that will give that information directly in your IDE.
If you are interested in the actual C implementation of most php functions, you can either navigate through the GitHub repository directly or clone it on your computer and open it in an IDE (Eclipse, CLion, etc...) and use the IDE navigation.
You can download the PHP source code from GitHub (https://github.com/php/php-src)
but the core of PHP is written in C language.
Use notepad++ to search the required details from the downloaded source code.
For example code for PHP array will be in this file :
https://github.com/php/php-src/blob/master/ext/standard/array.c
Notepad++ is the good editor with a lot of additional functionality like search string through files. (Find in files)
Hope it helps
Thanks
As an alternative, consider running OpenGrok in a Docker container. OpenGrok is an open-source and free source code indexer with advanced search mechanisms. (I am not affiliated with the project in any way)
Setting it up is easy (assuming you have Docker already installed):
# Make a directory that will contain source files for opengrok to index
mkdir ~/opengrok
# Clone the PHP source into that directory
git clone https://github.com/php/php-src.git ~/opengrok/php-src
# Start the OpenGrok container
docker run -d -v ~/opengrok:/src -p8081:8080 opengrok/docker
Now you should wait a minute or two for OpenGrok to fully index the source tree.
Open your browser
We want to search for the array_values implementation
Select the implementation in array.c
There you have it! The C implementation of array_values.

PHP Libraries - what are they and how to create one

1) Is a PHP library (as in the GD Library) a compiled DLL (or other appropriate name if that is not used outside of Windows) written in a language such as C, compiled, and then "loaded" and made available to PHP code?
2) If this is the case, where can I find documentation on libraries which, among other things, includes calling, argument passing, and value return standards and protocals, and other information which I can use to get started writing PHP "libraries"? I am not looking for documentation on how to program in C or another language, I am looking only for specific, and detailed, information on creating "libraries" for PHP.
Bob
In the php world I believe it's called an extension and it's behave similar to a windows dll and maybe has something similar when you want to create one.
theserverpages.com/php/manual/en/zend.php. I think its part of the php documentation, the url www.php net may work also.

.PO to .MO - Programmatic Conversion (PHP)

I am planning to allow users to generate .POT files/.PO files through a PHP user interface as part of CMS solution. Once these files have been generated (the easy bit) I would like to allow my system to automatically convert these files into .MO files in response to a user (POST) request.
I have seen the following question on SO:
.po to .mo convertor in php?
I understand that I could run msgfmt by using PHP's exec() function, but that seems to be a Linux only solution, if I am correct? How would I do this on other operating systems? Some example code of how this may be done in practice would also be really useful, if anybody would be kind enough to demonstrate. This is quite different from the work I usually do!
This is only a concept at the moment but I hope I'm going along the right lines. If there are any additional thoughts/suggestions you have regarding this method, I'd be glad to hear them. Background information follows.
Additional Background Information - Not required:
I am retrieving the original English text by parsing simple template files that consist of nothing more than basic HTML and calls to <?php _('the gettext method'); ?>. These templates are parsed when edited/saved and the language entries are retrieved. The .POT file will then be generated. The user would now have to edit translations manually (through a simple interface, not directly) to update/prepare all the .PO files. Once this is done, I would need to be able to convert them to .MO files, as is the title of my question.
There are also PHP-only reimplementations of msgfmt if that is what you are looking for:
php-msgfmt
php.mo/gh
As alternative there would also be the Translate/Pootle webapp, with its php2po script, but there must also be some .mo conversion functions in it... (Ooops no, it's in Python.)
Both PHP's exec and the msgfmt GNU gettext utility are not a linux only solution. They work on multiple computer systems. As with PHP you can compile for multiple platforms (as it's done naturally), so the exec command is available on mutliple systems, the same applies to msgfmt. Start on the GNU gettext homepage to obtain a version for your system.
try https://github.com/oscarotero/Gettext.git
use Gettext\Translations;
chdir('....');
$translations = Translations::fromPoFile('messages.po');
$translations->toMoFile('messages.mo');

Is there a tool to convert php built-in functions to c implementation?

I'm curious about how some built in functions are implemented,but it's very time consuming to look it up directly in the source,is there a tool that can automate this?
EDIT
Or is there a tool that can debug into the c code that's actually executed?
Most (all?) of the functions that can be accessed from PHP are defined under the ext/ directory in the PHP source code. If you have a recursive search tool, search for PHP_FUNCTION - if you saved the results of that search into a text file, it would be a pretty good "index" for figuring out where a PHP builtin is defined.
The really core stuff is in ext/standard.
Some rare "functions" are implemented directly as opcodes in the Zend virtual machine that PHP compiles to, so there isn't a well defined C function as such. I think strlen is such a function, for instance.
About the debugging the C code that's executed, I suppose it's possible to use something like dbg ; you'll first have to recompile PHP with the --enable-debug mode, though.
For more informations, you can take a look at :
Building PHP for extension development
Generating a gdb backtrace
I've never used this to debug PHP itself, but I've used those two pages to generate some backtraces of a crash I had with an extension, and it worked OK, from what I remember.
As a sidenote : using a PHP compiled with --enable-debug, you might have to recompile some of the extensions you're using and change the way they're loaded (it's the case for Xdebug, for instance) ; and some other might just not work at all anymore.
I believe that you should take a look at this.
Facebook has developed a tool to convert PHP code into c++.
So I guess it can handle C as well to some extent.

Extending PHP with C++?

I have a performance intensive routine that is written in PHP that I'd like to port to C++ for a performance increase. Is there any way to write a plugin or extension or something using C++ and interface with it from PHP? WITHOUT manually editing the actual PHP source?
As Remus says, you can extend PHP with C/C++ using the Zend API. The linked tutorial by Sara Golemon is a good start, and the book Extending and Embedding PHP by the same author covers the subject in much more detail.
However, it's worth noting that both of these (and pretty much everything else I found online) focus on C and don't really cover some tweaks you need to get C++ extensions working.
In the config.m4 file you need to explicitly link to the C++ standard library:
PHP_REQUIRE_CXX()
PHP_ADD_LIBRARY(stdc++, 1, PHP5CPP_SHARED_LIBADD)
Any C++ library compile checks in the config.m4 file will also require linking the C++ lib:
PHP_CHECK_LIBRARY($LIBNAME,$LIBSYMBOL,,
[
AC_MSG_ERROR([lib $LIBNAME not found.])
],[
-lstdc++ -ldl
])
EDIT - and here's how to specify g++:
Last, and not least, in order to choose the C++ rather than C compiler/linker when building the extension, the 6th parameter to PHP_NEW_EXTENSION() should be "yes". ie:
PHP_NEW_EXTENSION(your_extension,
your_extension.cpp,
$ext_shared,
,
"-Wall -Werror -Wno-error=write-strings -Wno-sign-compare",
"yes")
From the PHP build system manual, the parameters are:
The name of the extension
List of all source files which are part of the extension.
(optional) $ext_shared, a value which was determined by configure when PHP_ARG_WITH() was called for
(optional) "SAPI class", only useful for extensions which require the CGI or CLI SAPIs specifically. It should be left empty in all other cases.
(optional) A list of flags to be added to CFLAGS while building the extension.
(optional) A boolean value which, if "yes", will force the entire extension to be built using $CXX instead of $CC.
I couldn't work out how to get the configure script to set g++ as the compiler/linker instead of gcc, so ended up hacking the Makefile with a sed command to do a search replace in my bash build script:
phpize
./configure --with-myextension
if [ "$?" == 0 ]; then
# Ugly hack to force use of g++ instead of gcc
# (otherwise we'll get linking errors at runtime)
sed -i 's/gcc/g++/g' Makefile
make clean
make
fi
Presumably there's an automake command that would make this hack unnecessary.
I've written a PHP plugin in C++ with the help of SWIG. It's doable, but it may take a while to get used to the SWIG-compilation cycle. You can start with the SWIG docs for PHP.
Update
As #therefromhere has mentioned, I greatly recommend that you get the book Extending and Embedding PHP. There is almost no documentation to be found online (at least there wasn't in late 2008, early 2009 when I did my PHP plugin). I had to rely on the book for everything. Although sometimes Google Code Search is helpful for finding sample code.
PHP itself a collection of loosely related libraries. See http://devzone.zend.com/article/1021 for a tutorial how to write your own.

Categories