Skip to content

Commit

Permalink
Merge pull request #3338 from zikula/js-translation
Browse files Browse the repository at this point in the history
Js translation
  • Loading branch information
craigh authored Dec 30, 2016
2 parents 9d37184 + d5d187c commit b582934
Show file tree
Hide file tree
Showing 19 changed files with 410 additions and 30 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG-1.4.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ CHANGELOG - ZIKULA 1.4.x
- Template overrides are also considered in sub requests (e.g. embedding a Formicula form into a Content page) (#3234).
- Implemented PasswordApi in ZAuthModule (#3175, #3289).
- Migrated many parts of the Categories module to Symfony forms (#2924, #3334).
- Added new Javascript Translation via BazingaJsTranslationBundle (#2601, #2487, #3338)

- Vendor updates:
- afarkas/webshim installed at 1.16.0
Expand All @@ -45,11 +46,13 @@ CHANGELOG - ZIKULA 1.4.x
- sensio/generator-bundle updated from 3.0.11 to 3.1.2
- symfony updated from 2.8.14 to 2.8.15
- symfony/swiftmailer-bundle updated from 2.3.11 to 2.4.2
- swiftmailer updated from 5.4.3 to 5.4.5
- twig updated from 1.28.2 to 1.30.0
- twig-extensions updated from 1.3.0 to 1.4.1
- typeahead.js updated from 1.0.1 to 1.1.0
- underscore updated from 1.4.3 to 1.8.3
- underscore.string updated from 2.3.0 to 3.3.4
- willdurand/js-translation-bundle installed at 2.6.2

* 1.4.4 (2016-12-08)

Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"jms/translation-bundle": "1.3.*",
"nikic/php-parser": "~1.4.0",
"friendsofsymfony/jsrouting-bundle": "1.6.*",
"willdurand/js-translation-bundle": "^2.6",


"doctrine/orm": "2.5.*",
Expand Down
84 changes: 75 additions & 9 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/app/ZikulaKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public function registerBundles()
new Knp\Bundle\MenuBundle\KnpMenuBundle(),
new Liip\ImagineBundle\LiipImagineBundle(),
new Doctrine\Bundle\DoctrineCacheBundle\DoctrineCacheBundle(),
new Bazinga\Bundle\JsTranslationBundle\BazingaJsTranslationBundle(),
];

$this->registerCoreModules($bundles);
Expand Down
5 changes: 5 additions & 0 deletions src/app/config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,11 @@ stof_doctrine_extensions:
uploadable: Gedmo\Uploadable\UploadableListener
reference_integrity: Gedmo\ReferenceIntegrity\ReferenceIntegrityListener

bazinga_js_translation:
default_domain: zikula_javascript
locale_fallback: "%jms_i18n_routing.default_locale%"
active_locales: "%jms_i18n_routing.locales%"

parameters:
# zikula core stylesheet and javascript component locations
zikula.stylesheet.bootstrap-font-awesome.path: web/bootstrap-font-awesome.css
Expand Down
3 changes: 3 additions & 0 deletions src/app/config/routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ hook:
fos_js_routing:
resource: "@FOSJsRoutingBundle/Resources/config/routing/routing.xml"

_bazinga_jstranslation:
resource: "@BazingaJsTranslationBundle/Resources/config/routing/routing.yml"

installer:
resource: "@ZikulaCoreInstallerBundle/Resources/config/routing/routing.xml"

Expand Down
57 changes: 57 additions & 0 deletions src/docs/Core-2.0/TranslationAndLanguage/Javascript.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
Javascript Translation
======================

Zikula includes `willdurand/js-translation-bundle` also known as "BazingaJsTranslationBundle"

refs: https://github.com/willdurand/BazingaJsTranslationBundle

Bazinga adds native symfony translation support like so:

var myText = Translator.trans('Foo bar baz');
var myChoiceText = Translator.transChoice('%count% foo|%count% foos', 5);

The methods are defined like so:

Translator.trans(key, params, domain, locale);
Translator.transChoice(key, count, params, domain, locale);

Additionally, Zikula adds standard zikula translation functions:

var myText = Translator.__('Foo bar baz');
var myStrReplaceText = Translator.__f('Free %stuff%', {stuff: 'beer'});
var myPluralText = Translator._n('%count% apple', '%count% apples', count);
var myPluralReplaceText = Translator._fn('%count% %desc% apple', '%count% %desc% apples', 5, {desc: 'fresh'});

The methods are defined like so:

Translator.__(key, domain, locale);
Translator.__f(key, params, domain, locale);
Translator._n(singular, plural, count, domain, locale);
Translator._fn(singular, plural, count, params, domain, locale);


Extraction from Javascript Files
--------------------------------

Zikula also provides an Extractor for both native Symfony and Zikula translation functions. This is run automatically
when translations are extracted. By default, **all javascript translations** are added to the `zikula_javascript` domain.
**This catalog is automatically added to every page**.

If an extension _chooses_, it can set the domain manually in each method call, e.g.:

var myText = Translator.__('Foo bar baz', 'my_special_domain');

In this case, the extractor will export these strings to its own translation file:

/MyModule/Resources/translatiosn/my_special_domain.js

Then, your extension **must** manually include each of these files in the required template like so:

{{ pageAddAsset('javascript', url('bazinga_jstranslation_js', {domain:my_special_domain}), constant('Zikula\\ThemeModule\\Engine\\AssetBag::WEIGHT_JS_TRANSLATIONS')) }}


Javascripts in Twig Templates
-----------------------------

Translation usage in scripts that are created *within* a twig template (not a standalone file) should utilize standard
Twig template translation and not utilize javascript translation.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ services:
tags:
- { name: jms_translation.file_visitor }

zikula_core.internal.translation_extractor.file.js_extractor:
class: Zikula\Bundle\CoreBundle\Translation\ZikulaJsFileExtractor
public: false
tags:
- { name: jms_translation.file_visitor }

zikula_core.internal.translation_dumper.pot_dumper:
class: %zikula.dumper.pot_dumper.class%
public: false
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright Zikula Foundation, licensed MIT.

// This function extends vendor/willdurand/js-translation-bundle/Bazinga/Bundle/JsTranslationBundle/Resources/js/translator.js
// it MUST be loaded afterwards
( function($) {
$(document).ready(function() {
Translator.__ = function (key, domain, locale) {
domain = typeof domain !== 'undefined' ? domain : Translator.defaultDomain;
locale = typeof locale !== 'undefined' ? locale : Translator.locale;
return Translator.trans(key, {}, domain, locale);
};
Translator.__f = function (key, params, domain, locale) {
domain = typeof domain !== 'undefined' ? domain : Translator.defaultDomain;
locale = typeof locale !== 'undefined' ? locale : Translator.locale;
return Translator.trans(key, params, domain, locale);
};
Translator._n = function (singular, plural, count, domain, locale) {
domain = typeof domain !== 'undefined' ? domain : Translator.defaultDomain;
locale = typeof locale !== 'undefined' ? locale : Translator.locale;
return Translator.transChoice(singular+'|'+plural, count, {count: count}, domain, locale);
};
Translator._fn = function (singular, plural, count, params, domain, locale) {
domain = typeof domain !== 'undefined' ? domain : Translator.defaultDomain;
locale = typeof locale !== 'undefined' ? locale : Translator.locale;
params.count = count;
return Translator.transChoice(singular+'|'+plural, count, params, domain, locale);
};
});
})(jQuery);
16 changes: 16 additions & 0 deletions src/lib/Zikula/Bundle/CoreBundle/Tests/Translation/Fixture/Test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright Zikula Foundation, licensed MIT.

( function($) {
$(document).ready(function() {
alert(Translator.__("Hi there!") + '\n'
+ Translator.__f('My name is %n%', {"n": 'foo'}) + '\n'
+ 'count:' + '\n'
+ Translator._n('%count% apple', '%count% apples', 1) + '\n'
+ Translator._fn('%count% %desc% apple', '%count% %desc% apples', 5, {desc: 'ugly'}) + '\n'
+ Translator.defaultDomain + '\n'
+ Translator.locale
);
alert(Translator.trans("Original Translator!"));
var someText = Translator.transChoice("someText|someTexts", 5);
});
})(jQuery);
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

/*
* This file is part of the Zikula package.
*
* Copyright Zikula Foundation - http://zikula.org/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Zikula\Bundle\CoreBundle\Tests\Translation;

use JMS\TranslationBundle\Model\Message;
use JMS\TranslationBundle\Model\MessageCatalogue;
use JMS\TranslationBundle\Translation\Extractor\FileVisitorInterface;
use JMS\TranslationBundle\Translation\FileSourceFactory;
use Zikula\Bundle\CoreBundle\Translation\ZikulaJsFileExtractor;

class JsFileExtractorTest extends \PHPUnit_Framework_TestCase
{
public function testExtractController()
{
$fileSourceFactory = $this->getFileSourceFactory();
$fixtureSplInfo = new \SplFileInfo('/' . __DIR__ . '/Fixture/Test.js');
$expected = new MessageCatalogue();

$message = new Message('My name is %n%', 'zikula_javascript');
$message->addSource($fileSourceFactory->create($fixtureSplInfo));
$expected->add($message);

$message = new Message('%count% apple|%count% apples', 'zikula_javascript');
$message->addSource($fileSourceFactory->create($fixtureSplInfo));
$expected->add($message);

$message = new Message('%count% %desc% apple|%count% %desc% apples', 'zikula_javascript');
$message->addSource($fileSourceFactory->create($fixtureSplInfo));
$expected->add($message);

$message = new Message('someText|someTexts', 'zikula_javascript');
$message->addSource($fileSourceFactory->create($fixtureSplInfo));
$expected->add($message);

$message = new Message('Hi there!', 'zikula_javascript');
$message->addSource($fileSourceFactory->create($fixtureSplInfo));
$expected->add($message);

$message = new Message('Original Translator!', 'zikula_javascript');
$message->addSource($fileSourceFactory->create($fixtureSplInfo));
$expected->add($message);

$this->assertEquals($expected, $this->extract('Test.js'));
}

protected function extract($file, FileVisitorInterface $extractor = null)
{
if (!is_file($file = __DIR__ . '/Fixture/' . $file)) {
throw new \RuntimeException(sprintf('The file "%s" does not exist.', $file));
}
$file = new \SplFileInfo($file);

if (null === $extractor) {
$extractor = new ZikulaJsFileExtractor();
}

$catalogue = new MessageCatalogue();
$extractor->visitFile($file, $catalogue);

return $catalogue;
}

protected function getFileSourceFactory()
{
return new FileSourceFactory('/');
}
}
Loading

0 comments on commit b582934

Please sign in to comment.