I'm following the instructions from BryanH's answer here: gettext() equivalent in Intl library? and trying to implement localization (translation) with php-intl, but I keep getting the same problem this person had: ResourceBundle returns NULL without any errors being raised
He mentions he created the dat files with a tool (which I cannot figure out how to work) while the person in the former answer simply appears to be using txt files with a .res extension.
How do I properly implement localization with php-intl and ResourceBundle, and what am I doing wrong?
The goal is to have various data files with different languages so I can do something similar to
$t = new Translator();
$t->setResource(new \ResourceBundle('es', 'locales_folder/'));
$t->echo("somestring"); // "el stringo"
..much like the person in the first answer had. Also, the aim is to have easily editable files, so I can give them to translators for fixes, updates, and so on. I realize I could easily do this with custom solution through a simple text file which gets parsed and saved into memcache on first request, where it then persists and gets served from without having to re-read the .dat files, but I'd rather take the suggested route here.
Edit: Just to get it out there - I implemented the same thing with gettext successfully and it was dead easy - save for one bug that persists across linux systems (http://www.php.net/manual/en/book.gettext.php#91187) - but I'd like to rely on the more modern and all-inclusive intl extension if possible.
I can provide a solution to your question on how to create and use .res files for the intl PHP extension, however I have no experience with speed and use in production systems, you will have to see for yourself if this can be a replacement for gettext.
In my opinion a great benefit of gettext are additional tools such as xgettext which can extract strings to be translated. Still, using resources might be more useful in your use-case.
To generate a .res file you need to use the program genrb which is bundled with ICU. (for example when installing ICU on OSX using brew install icu4c (see this tutorial) you can find the tool at /usr/local/Cellar/icu4c/*/bin/genrb (replace * with the version number))
Next you prepare a file for each locale. Let's do this for two languages in this example, German and Spanish.
$ cat de.txt
de {
hello { "Hallo" }
}
$ cat es.txt
es {
hello { "Hola" }
}
$ genrb *.txt
genrb number of files: 2
You now have 2 additional files de.res and es.res, which you can now access in PHP
<?php
foreach (array("de", "es") as $locale) {
$r = ResourceBundle::create($locale, __DIR__);
echo $r["hello"], "\n";
}
which will output
Hallo
Hola
So in essence, you can hand those *.txt files to your translaters and prepare them for PHP using genrb.
Finally, a small example for the class you were trying to write:
<?php
class Translator {
private $resourceBundle = null;
public function __construct(\ResourceBundle $r) {
$this->resourceBundle = $r;
}
public function __get($string) {
return $this->resourceBundle[$string];
}
}
$t = new Translator(new \ResourceBundle('es', __DIR__));
echo $t->hello;
Related
I'm trying to understand what options are available for me in my configuration.nix for particular programs, by searching the pkgs sub-folder of nixpkgs's source tree, picking out the module's definitions to understand the available options. However, I'm running into a troublesome case for PHP - it's a special derivation, a composable derivation. I'm not able to see what options I have available with PHP - something that would be tremendously helpful for enabling special modules like mcrypt, gd, etc. Any help with this would be greatly appreciated!
It took me a while to figure this out but the right way to use composeDerivation for setting the php package build features is this:
# config.nix
{
packageOverrides = pkgs: rec {
php = pkgs.php.merge {
cfg = {
imapSupport = false;
intlSupport = false;
fpmSupport = false;
};
};
};
}
This overrides the default values in cfg specified in php/default.nix (imapSupport, intlSupport and fpmSupport get turned off). You can either place that file in ~/.nixpkgs/config.nix to be active system-wide or use it in another nix file like so to customize the global nixpkgs:
pkgs = import <nixpkgs> { config = (import ./config.nix); };
Try tracking the file interactions from configuration.nix and also try to understand all those flags at first, the PHP file is an "startup engine" of some kind I doesn't seem to have any possible configuration options it is just used as a run switch of some type and read rows 234-236(PHP) it says it needs config
also the http://nixos.org/nixos/manual/sec-configuration-syntax.html#sec-nix-syntax-summary is showing very clearly the possibilities. I say again I'm not into that engine but I think everything further to configure is done with the NixOS admin commands, it will be easier for me to help you if you explain what exactly you need done.
I have started using v8js with php for a while now but the documentation is really thin.
One thing that is not explained is Extensions.
It is possible to registerExtension but it is not explained in detail how these behave or whats their purpose or benefits.
Can anyone provide a good description or link to a documentation that explains Extensions?
Thanks to everyone for taking time to read and answer :-)
Original Answer
My original answer indicated that the extension was called every time executeString was.
Corrected answer
An extension is a bit of code that is executed before the first executeString call for a given V8Js instance.
Extension can be global to all V8Js instances or local to a specific instance.
I have experimentally determined that this isn't always very reliable. If you frantically refresh a page, you may not always see the extension get run... This is probably why this is beta quality software.
Here are two examples that I whipped up
Global Extension Example
Code
V8Js::registerExtension('say_hi', 'print("hey from extension! "); var said_hi=true;', array(), true);
$v8 = new V8Js();
$v8->executeString('print("hello from regular code!")', 'test.php');
$v8->executeString('if (said_hi) { print(" extension already said hi"); }');
Output
hey from extension! hello from regular code! extension already said hi
Non-Global Example
Code
V8Js::registerExtension('say_hi', 'print("hey from non global extension! "); var said_hi=true;');
$v8 = new V8Js('PHP', array(), array('say_hi'));
$v8->executeString('print("hello from regular code!");', 'test.php');
$v8->executeString('if (said_hi) { print(" extension already said hi"); }');
Output
hey from non global extension! hello from regular code! extension already said hi
I have to develop a pretty simple php website so I don't need framework.
But it's must support multi language (EN/FR/CHINESE).
I have looked for php built in system and I found two ways :
intl module from php5.3 (http://php.net/manual/fr/book.intl.php)
gettext (http://php.net/manual/fr/book.gettext.php)
I have no experience in i18n without framework, so any advices about what's the simplest way to support multi language ?
At end I just need a function that search translation into file (one file by language).
EQ :
trans('hello');
=> en.yaml (yaml or not, it's an example)
hello: "Hello world!"
=> fr.yaml
hello: "Bonjour tout le monde !"
And if possible I prefer Pure PHP implementations
Although ext/gettext and ext/intl are both related to i18 (internationalization), gettext deals with translation while intl deals with internationalizing things like number and date display, sorting orders and transliteration. So you'd actually need both for a complete i18-solution. Depending on your needs you may come up with an home-brew solution relying on the extensions mentioned above or your use components provided by some framework:
Translation
Symfony 2 Translation component: https://github.com/symfony/Translation
Zend Framework Zend_Translate
Internationalization
Zend Framework Zend_Locale
If you only need translation and the site is simple enough, perhaps your simple solution (reading a translation configuration file into an PHP array, using a simple function to retrieve a token) might be the easiest.
The most simple solution I can think of is:
$translation = array(
'Hello world!' => array(
'fr' => 'Bonjour tout le monde!',
'de' => 'Hallo Welt!'
)
);
if (!function_exists('gettext')) {
function _($token, $lang = null) {
global $translation;
if ( empty($lang)
|| !array_key_exists($token, $translation)
|| !array_key_exists($lang, $translation[$token])
) {
return $token;
} else {
return $translation[$token][$lang];
}
}
}
echo _('Hello World!');
I know this is an old question, but I feel that the answers are lacking a more hands-on approach from start to finish. This is what I did to get translation working using PHP's gettext library and Poedit without using any additional PHP libraries on a Debian server:
Preparation step 1: Install gettext and the locales on the server
I am not sure how this is done with other operating systems, but for Debian, you do:
sudo apt-get install gettext
sudo dpkg-reconfigure locales
Edit: I assumed Ubuntu would be the same as Debian, but apparently it's slightly different. See this page for instructions for installing locales on Ubuntu.
Make sure you select all of the locales that you want to use. You should then see something like:
Generating locales (this might take a while)...
en_US.UTF-8... done
es_MX.UTF-8... done
fr_FR.UTF-8... done
zh_CN.UTF-8... done
Generation complete.
Note: Make sure you select the right variants and character encodings (most likely UTF-8) for each language. If you install es_MX.UTF-8 and try to use es_ES.UTF-8 or es_MX.ISO-8859-1 it won't work.
Preparation step 2: Install Poedit on the translators' computers
Poedit is available from the software repository for most Linux operating systems. For Debian-based, just execute:
sudo apt-get install poedit
For Windows and Mac, go to: https://poedit.net/download
Start coding:
Ok, now you're ready to get started coding. I wrote the following gettext() wrapper function to translate both singular and plurals:
function __($text, $plural=null, $number=null) {
if (!isset($plural)) {
return _($text);
}
return ngettext($text, $plural, $number);
}
Example usage:
// Singular
echo __('Hello world');
// Plural
$exp = 3;
printf(
__(
'Your account will expire in %d day',
'Your account will expire in %d days',
$exp
),
$exp
);
This will work for all languages, not only languages where plural is anything where n != 1 - this includes languages with multiple plural types.
You can also add translator notes like this:
/** NOTE: The name Coconut Hotel is a brand name and shouldn't be
translated.
*/
echo __('Welcome to Coconut Hotel');
You can change the text from NOTE to whatever you want, but you will have to alter it in the shell script below. Important: The translators note must be part of a comment on the line immediately preceding the __() function or it won't be picked up when we scan the PHP files for translatable strings.
// Warning! THIS WILL NOT WORK!
/* NOTE: This translator's note will not be picked up because it is
not immediately preceding the __() function. */
printf(
__(
'Your account will expire in %d day',
'Your account will expire in %d days',
$exp
),
$exp
);
// Warning! THIS WILL NOT WORK!
After you are ready to send the strings off to the translators, save the following as a shell script (e.g. update.sh) in your application's root directory:
#!/bin/sh
find . -iname "*.php" | xargs xgettext --add-comments=NOTE --keyword=__:1,2 --keyword=__ --from-code=UTF-8 -o i18n.pot
find . -name '*.po' | xargs -I{} msgmerge -U {} i18n.pot
To execute it, just do:
cd /path/to/script && sh update.sh
This will recursively scan for all PHP files in that directory and create a .pot file (I called it i18n.pot, but feel free to name it whatever you like) and update any existing .po files it finds with the new strings.
We then need to create the directories that all the locale files will be stored, one for each locale. They need to be of the format ./locale/{locale}/LC_MESSAGES. For example:
cd /path/to/your/project
mkdir -p ./locale/en_US.UTF-8/LC_MESSAGES
mkdir -p ./locale/es_MX.UTF-8/LC_MESSAGES
# ...etc.
You need to decide on a text domain to use. This can be anything you want, but the script will look for a file called {yourTextDomain}.mo within the LC_MESSAGES folder for that language. Put the following in your PHP script:
define('TEXT_DOMAIN', 'yourdomain');
bindtextdomain(TEXT_DOMAIN, __DIR__.'/locale');
textdomain(TEXT_DOMAIN);
bind_textdomain_codeset(TEXT_DOMAIN, 'UTF-8');
Then to actually switch to another locale, do:
$lang = 'es_MX.UTF-8'; // Change this to the language you want to use
if (setlocale(LC_ALL, $lang) === false) {
throw new Exception("Server error: The $lang locale is not installed. Please update the server's localisations.");
}
putenv('LC_ALL='.$lang);
Initially, you send the .pot file generated by the script above to the translators. They then open Poedit and click on File > New from POT/PO file. When they save it, they need to save it as {yourTextDomain}.po. The {yourTextDomain} needs to be exactly the same as the text domain you have in your PHP script. When they save it, it will automatically create both the .po file and the .mo file. Both of these need to be saved in that language's LC_MESSAGES directory when they are done translating.
Now when you update the strings in your PHP file, just re-execute the shell script and send the newly updated .po files to the translators. They then translate the strings and both the .po and .mo files need to be re-uploaded.
That's it. It may seem slightly difficult to get set up, but once you have it up and running, it's really easy.
Gettext seems to be what you need.
There is a file by langage (except for the original one) and it's very easy to use :
echo _('Bonjour, ça va ?');
will print Hello , how are you ? in english.
There is some tools with gettext that could scan your php file and search for translatable string (in fact all string in _() or gettext()). Thanks to that you don't have to worry about the different langage file. You just code your website in the original langage and the langage file will automatically created later.
Nevertheless gettext is more a translation tools whereas intl is really an i18n one (number formating for example)
Althought you don't need a framework you can use a framework. The internationalization features in Zend Framework is pretty good and you can just use that part of it instead of using all the parts (including MVC)
I'm looking into storing Zend Framework on Microsoft Azures Blob Storage. Azure Blob storage has a flat file system where as Zend Framework has a deeply nested structure.
I want to create a folder containing all Zend Framework files using the following naming convention / method.
eg: The Zend_Log_Writter class is stored at:
library/Zend/Log/Writer.php
In the new flat output folder the file would be named:
Zend.Log.Writter.php
I'll use any tool that will accomplish the job linux or windows. I could write a PHP script that would do this but I'm guessing there is a piece of Linux Foo out there that could accomplish what I'm after with a few linked commands.
If I'm reading this question correctly, it sounds like you're trying to map a file system to individual blobs in Windows Azure Storage. You'd need some type of in-between tier to map requested files to individual blobs (and php has a very robust SDK you can use for accessing blobs). One correction about blobs: it's not exactly flat: The URI would be https://mystorageaccount/containername/blobname. You have one native directory (container), and you can then simulate further levels with the technique Pekka provided a link to.
However: To me, this mapping sounds like it may have performance implications, as well as transaction implications.
As an alternative, why not mount an NTFS-formatted Cloud Drive in a Windows Azure page blob? Your drive can be up to 1TB, accessible via drive-letter. It's durable (meaning triple-replicated within the data center), and would let you then install pretty much anything to it (such as the file directory structure for Zend). Pretty easy to set up - maybe a dozen lines of code.
The one caveat to Cloud Drives: only one writer. You'd have to keep that in mind when scaling your web app to multiple instances. There are workarounds (such as having one Cloud Drive per instance), and taking advantage of caching (such as the new AppFabric Cache which recently went into production).
EDIT: Here's a great Cloud Drive sample by Maarten Balliauw, demonstrating the steps needed for creating/allocating/mounting a Cloud Drive.
I know how to do this in python. Here is an example. Test it on some dummy directory first just to make sure you're using it right.
import os
import shutil
directory = '/path/to/your/directory/'
for root, folder, files in os.walk(directory):
for file in files:
print(directory+'renamed/'+root.replace('/', '.')[1:]+'.'+file)
shutil.copy(root+'/'+file, directory+'renamed/'+root.replace('/', '.')[1:]+'.'+file)
Edit as to why I used python: I did try using the find command and the -exec option in Linux initially....but much cleaner and easier to understand this way. I'm guessing you could go with one uber awesome line at the bash shell. However not sure the extra time needed to try and figure that out is worth it being as this is basically only 4 lines of relevant code.
Try this code. It makes use of StorageClient library from Microsoft.
namespace RenameBlobs
{
class Program
{
static void Main(string[] args)
{
CloudStorageAccount csa = CloudStorageAccount.DevelopmentStorageAccount;
string blobContainerName = "png1";
string oldDelimiter = "/";
string newDelimiter = ".";
CloudBlobClient blobClient = csa.CreateCloudBlobClient();
var blobContainer = blobClient.GetContainerReference(blobContainerName);
string blobContainerUriString = blobContainer.Uri.AbsoluteUri;
BlobRequestOptions blobRequestOptions = new BlobRequestOptions()
{
UseFlatBlobListing = true,
};
var blobsList = blobContainer.ListBlobs(blobRequestOptions);
foreach (var blob in blobsList)
{
var blockBlob = (CloudBlockBlob) blob;
var abc = blockBlob.Metadata;
blockBlob.FetchAttributes();
string blobName = blockBlob.Uri.AbsoluteUri;
blobName = blobName.Replace(blobContainerUriString, string.Empty);
if (blobName.StartsWith(oldDelimiter))
{
blobName = blobName.Substring(1);
}
if (blobName.Contains(oldDelimiter))
{
blobName = blobName.Replace(oldDelimiter, newDelimiter);
string newBlobUriString = string.Format("{0}/{1}", blobContainerUriString, blobName);
var cloudBlob = blobContainer.GetBlobReference(newBlobUriString);
cloudBlob.CopyFromBlob(blockBlob);
}
}
}
}
}
Let me know if you have any questions about this code.
Hope this helps.
Thanks
I'm not going to lie. I'm not all the familiar with Windows and COM objects. That's why i'm here. First of all is it possible to access a DLL from within a PHP script running out of Apache? In my journey around the internets i believe that i have 2 options:
compile the dll as an extension for PHP. (i didn't make this dll)
access the DLL as a COM object which is sort of what it's designed for anyways.
So i'm taking the COM approach.
try{
$com = new COM('WHAT_GOES_HERE');
} catch(Exception $e){
echo 'error: ' . $e->getMessage(), "\n";
}
How do i go about finding out what would go into the initialization string? is there a com viewer type program i could/should be using to find this out? the documentation associated with this DLL doesn't seem to specify what strings i should be using to initialize but gets very in-depth into what streams are available, and all sorts of fun stuff. just gotta past this initial hump. Please help!
WHAT_GOES_HERE is the ProgID, Class ID or Moniker registered on the Operating System.
Each of these can change for the same DLL registered on different machines. There are several ways to find what's the ProgID/CLSID/Moniker of a registered dll. You can search the web for "dll debugger", "dll export", "dll inspect" and you'll see several solutions, and also ways to show what functions the dll export so you can use them.
The easiest way, you can just register the dll with Regsvr32.exe and search Window's register with regedit.exe for the dll's name, you might need to search several times until you find the key under \HKEY_CLASSES_ROOT\, which is the ProgID.
The command dcomcnfg.exe shows a lot of information about COM objects.
If you have Visual Studio, the OLE/COM Object Viewer (oleview.exe) might be useful.
You can run dll functions (from dlls which are not php extensions) with winbinder.
http://winbinder.org/
Using it is simple. You have to download php_winbinder.dll and include it in php.ini as an extension.
In the php script you have to use something similar:
function callDll($func, $param = "")
{
static $dll = null;
static $funcAddr = null;
if ($dll === null)
{
$dll = wb_load_library(<DLL PATH AND FILENAME>);
}
$funcAddr = wb_get_function_address($func, $dll);
if ($param != "")
{
return wb_call_function($funcAddr,array(mb_convert_encoding($param,"UTF-16LE")));
}
else
{
return wb_call_function($funcAddr);
}
}
You can simply develop a wrapper around your main dll and use this wrapper as an extension in your PHP. Some free tools like SWIG can generate this wrapper for you automatically by getting the header of your dll functions. I myself use this approach and it was easy and reliable.
With PHP>=7.4.0's new FFI/Foreign Function Interface (which didn't exist yet when this question was posted), this is now easier than ever before! For example, to call the GetCurrentProcessId(); function from kernel32.dll:
<?php
declare(strict_types=1);
$ffi = FFI::cdef(
'unsigned long GetCurrentProcessId(void);',
"C:\\windows\\system32\\kernel32.dll"
);
var_dump($ffi->GetCurrentProcessId());
outputs
C:\PHP>php test.php
int(24200)
:)