2017 © Pedro Peláez
 

symfony-bundle jstranslations-bundle

JsTranslations for Symfony2

image

funddy/jstranslations-bundle

JsTranslations for Symfony2

  • Saturday, September 12, 2015
  • by funddy
  • Repository
  • 2 Watchers
  • 9 Stars
  • 1,992 Installations
  • CoffeeScript
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

JavaScript Translations Bundle

Build Status, (*1)

This bundle enables you to use your Symfony translator from JavaScript, allowing the generate translations in a very similar way., (*2)

Setup and Configuration

First, you should have the Translator enabled at app/config.yml, (*3)

framework:
    translator: { fallback: en }

Add the following to your composer.json file:, (*4)

{
    "require": {
        "funddy/jstranslations-bundle": "2.0.*"
    }
}

Update the vendor libraries:, (*5)

curl -s http://getcomposer.org/installer | php
php composer.phar install

Register the Bundle FunddyJsTranslationsBundle in app/AppKernel.php., (*6)

public function registerBundles()
{
    $bundles = array(
        new Funddy\Bundle\JsTranslationsBundle\FunddyJsTranslationsBundle()
    );
}

For finishing, override the default translator with one which implements the ReadableTranslator interface at app/parameters.yml file, (*7)

parameters:
    translator.class: Funddy\Bundle\JsTranslationsBundle\ReadableTranslator\SymfonyReadableTranslator

Usage

Expose the translations that you want to have accessible from your JavaScript code adding the languages and domains to the bundle configuration, (*8)

funddy_js_translations:
    languages: [es, en]
    domains: [messages]

Now it's time to decide whether you want to use static or dynamic generated translations., (*9)

Dynamic Translations

This is the most flexible option. Every time you make a change in translations you'll be able to access them from JavaScript. It's ideal for development environment or for those cases where you performance is not an issue. As everything in life, you have to pay a price, which is invoking an URL to regenerate all translations every time you make a request., (*10)

Include FunddyJsTranslationsBundle routing at app/routing.yml, (*11)

jstranslations:
    resource: "@FunddyJsTranslationsBundle/Resources/config/routing.yml"

Include the scripts, (*12)

<script>var TRANSLATIONS_LOCALE = "{{ app.request.locale }}"</script>
<script src="{{ path('funddy_jstranslations', {locale: app.request.locale}) }}"></script>
<script src="{{ asset('bundles/funddyjstranslations/js/lib/funddytranslations.js') }}"></script>

Static Translations

Is the best solution when we talk about performance, include compiled translations in order to avoid controller execution., (*13)

Compile the translations, (*14)

php app/console funddy:jstranslations:dump

Include translations, (*15)

<script>var TRANSLATIONS_LOCALE = '{{ app.request.locale }}'</script>
<script src="{{ asset('js/translations.' ~ app.request.locale ~ '.js') }}"></script>
<script src="{{ asset('bundles/funddyjstranslations/js/lib/funddytranslations.js') }}"></script>

Have fun!

<script>
    console.log(Translator.trans('Hello %name%!', {'%name%': 'Charlie'})); //Hello Charlie!
    console.log(Translator.transChoice('{1} There is one apple|]1,19] There are %count% apples', 3, {'%count%': 3}));//There are 3 apples
</script>

Defining Your Own Translator

What if you do not want to use the default "Translator" global var and define your own? Easy, include only translations runtime and define your own translator., (*16)








The Versions

12/09 2015

dev-master

9999999-dev https://github.com/funddy/jstranslations-bundle

JsTranslations for Symfony2

  Sources   Download

MIT

The Requires

 

The Development Requires

translations translator js translations

12/09 2015

v2.0.3

2.0.3.0 https://github.com/funddy/jstranslations-bundle

JsTranslations for Symfony2

  Sources   Download

MIT

The Requires

 

The Development Requires

translations translator js translations

24/11 2012

v1.0.0

1.0.0.0 https://github.com/funddy/jstranslations-bundle

JsTranslations for Symfony2

  Sources   Download

MIT

The Requires

 

The Development Requires

translations translator js translations