-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3338 from zikula/js-translation
Js translation
- Loading branch information
Showing
19 changed files
with
410 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
src/lib/Zikula/Bundle/CoreBundle/Resources/public/js/Zikula.Translator.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
16
src/lib/Zikula/Bundle/CoreBundle/Tests/Translation/Fixture/Test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
76 changes: 76 additions & 0 deletions
76
src/lib/Zikula/Bundle/CoreBundle/Tests/Translation/JsFileExtractorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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('/'); | ||
} | ||
} |
Oops, something went wrong.