Asked  7 Months ago    Answers:  5   Viewed   30 times

I am currently researching the best methods to integrate i18n into projects.

There's several methods I have thought of doing this, first being a database scheme to store the strings and relevant locale, but the problem with this is that it would not be that easy to select the strings, because i would not like to perform quesries like so:

SELECT text FROM locales WHERE locale = 'en_GB' AND text_id = 245543

Or

SELECT text FROM locales WHERE locale = 'en_GB' AND text_primary = 'hello'

The next method would be to store them within files such as locales/en_gb/login/strings.php and then try and access them via an class specifically developed like so:

$Language = Registry::Construct('Language',array('en_GB'));
echo $Language->login->strings->hello;

The issue with this is I would have to build a system that would update these files via an administration panel witch is very time consuming, not just building the system to manage the strings but actually managing the strings as the site grows

  • What other methods are there that will be beneficial for a large system
  • Is there any automated way to do 'Translation' as such
  • Should I stick with a database method and build a system for users to translate strings with rating / suggest better version ?
  • What systems have you tried in the past and should I look into them or totally avoid them.

 Answers

42

In addition to gettext already mentioned, PHP 5.3 has native Internationalization support

If that's not an option, consider using Zend Framework's Zend_Translate, Zend_Locale and related components for that. Zend_Translate supports a number of adapters, including but not limited to simple arrays, gettext, XmlTm and others.

Wednesday, March 31, 2021
 
Revent
answered 7 Months ago
55

As discussed in the comments, the gettext extension relies on the standard locale specifiers containing the language and region code, i.e. ja_JP for "Japanese in Japan" or with the specified encoding ja_JP.utf-8. Even if there is an alias like japanese, the PHP implementation of gettext does not accept this. Note that the locale has to be installed and configured on your system.

Language and region specifiers can be found in IANA language-subtag-registry

This code already should work for the Japanese language:

$dir     = $_SERVER['DOCUMENT_ROOT'] . '/locale';
$domain  = 'messages';
$locale  = 'ja_JP.utf8';
$codeset = 'UTF-8';

setlocale( LC_MESSAGES, $locale);
bindtextdomain($domain, $dir);
textdomain($domain);
bind_textdomain_codeset($domain, $codeset);

Remember to also rename your directory into locale/ja_JP.utf8. Ensure your .po files are stored with the correct encoding, i.e. UTF-8 in this example, and contain the line

"Content-Type: text/plain; charset=UTF-8n"

(as you already have done).

Wednesday, March 31, 2021
 
Parfait
answered 7 Months ago
44

Yes, IIRC, you need to have locale on the system to make it work.

Saturday, May 29, 2021
 
DiglettPotato
answered 5 Months ago
59

I ran into the same issue and found the following solution in the documentation: http://symfony.com/doc/current/book/testing.html#your-first-functional-test

To run your functional tests, the WebTestCase class bootstraps the kernel of your application. In most cases, this happens automatically. However, if your kernel is in a non-standard directory, you'll need to modify your phpunit.xml.dist file to set the KERNEL_DIR environment variable to the directory of your kernel:

<phpunit>
    <!-- ... -->
    <php>
        <server name="KERNEL_DIR" value="/path/to/your/app/" />
    </php>
    <!-- ... -->
</phpunit>

So check your phpunit.xml.dist configuration file and try to add the absolute path to your app-directory.

Hope it helps.

Friday, July 30, 2021
 
Bálint Molnár
answered 3 Months ago
88

Silly mistake on my part... simply forgot to add phpunit as a dependency in the project. For anyone else that gets this error, to composer.json add:

"require-dev": {
    "phpunit/phpunit": "3.7.*"
},

And then run:

composer update

That solved the problem.

Friday, September 17, 2021
 
Samir Sabri
answered 1 Month ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :