Reputation: 901
I have been trying to figure out another way of handling i18n within FuelPHP (see here).
I decided to import the Symfony2 Translation component (using composer) to Fuel as a vendor and manage i18n with xliff files.
Here is my (simplified) code:
use \Symfony\Component\Translation\Translator;
use \Symfony\Component\Translation\MessageSelector;
use \Symfony\Component\Translation\Loader\XliffFileLoader;
...
class I18N
{
private static $translator = NULL;
....
public static function get($key)
{
# Load and configure the translator
self::$translator = new Translator('en_GB', new MessageSelector());
self::$translator->addLoader('xliff', new XliffFileLoader());
self::$translator->addResource('xliff', 'path/to/xliff/file', 'en');
# Get the translation
$translation = self::$translator->trans($key, $params);
# Return the translation
return $translation;
}
}
So at first I thought that was working great since I was testing it on a very small xliff file but now that I've generated the complete xliff catalogue (about 1400 entries) for my entire application, each request is really slow.
So the question is, is there a way to cache translations when using the Translation component the same way the whole Symfony2 Framework caches it natively?
The Translator Class from the FrameworkBundle has a constructor that accepts options in which you can define the cache_dir. Anyway I can achieve that using the Translation component?
Thanks for any help on that matter.
Upvotes: 2
Views: 1334
Reputation: 901
So what I did was to generate my own cache from xliff files, if it doesn't exist, which is nothing more than the translations as a php array and make the Translator Component load resources as ArrayLoader instead of XliffFileLoader. It's lightning fast now. Thanks to Touki in the comments for your interest.
Upvotes: 2