Skip to content

Instantly share code, notes, and snippets.

@bazo
Created January 17, 2015 16:54
Show Gist options
  • Save bazo/4d4a4e363bb98135e837 to your computer and use it in GitHub Desktop.
Save bazo/4d4a4e363bb98135e837 to your computer and use it in GitHub Desktop.
$di->set('translator', function() use ($config, $di) {
$locale = $di->get('locale');
$whitelist = ['de', 'en'];
$fallback = ['de', 'en'];
$chainResolver = new \Kdyby\Translation\LocaleResolver\ChainResolver;//new \LocaleResolver($locale);
$localeResolver = new \LocaleResolver($locale);
$chainResolver->addResolver($localeResolver);
$selector = new \Symfony\Component\Translation\MessageSelector;
$fallbackResolver = new \Kdyby\Translation\FallbackResolver;
$fallbackResolver->setFallbackLocales($fallback);
$loader = new \Kdyby\Translation\TranslationLoader;
$neonLoader = new \Kdyby\Translation\Loader\NeonFileLoader;
$loader->addLoader('neon', $neonLoader);
$cacheStorage = $di->get('cacheStorage');
$catalogueFactory = new \Kdyby\Translation\CatalogueFactory($fallbackResolver, $loader);
$catalogueCompiler = new \Kdyby\Translation\CatalogueCompiler($cacheStorage, $fallbackResolver, $catalogueFactory);
$translator = new \Kdyby\Translation\Translator($chainResolver, $selector, $catalogueCompiler, $fallbackResolver, $loader);
$translator->setDefaultLocale('de');
$translator->setLocaleWhitelist($whitelist);
$format = 'neon';
$dir = realpath(__DIR__ . $config->translation->translationsDir);
$whitelistRegexp = \Kdyby\Translation\Translator::buildWhitelistRegexp($whitelist);
foreach (\Nette\Utils\Finder::findFiles('*.*.' . $format)->from($dir) as $file) {
/** @var \SplFileInfo $file */
if (!$m = Strings::match($file->getFilename(), '~^(?P<domain>.*?)\.(?P<locale>[^\.]+)\.' . preg_quote($format) . '$~')) {
continue;
}
if ($whitelistRegexp && !preg_match($whitelistRegexp, $m['locale'])) {
continue; // ignore in production mode, there is no need to pass the ignored resources
}
$translator->addResource($format, $file->getPathname(), $m['locale'], $m['domain']);
}
return $translator;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment