Skip to content
Miles Rausch edited this page Jan 27, 2015 · 2 revisions

CoreConX supports simple translations. You set a translator for CoreConX to use, and it exposes a public method called _() that aliases your translation calls.

The translator Property

Getter and setter for your translator component path

Setter takes a string which is the component path to your translator CFC. Your translator CFC must expose a translate() method which the _() method aliases.

Example:

<cfscript>
    translatorComponent = 'com.translator';
    coreconx.setTranslator( translatorComponent );
    writeOutput( coreconx.getTranslator() ); // 'com.translator'
</cfscript>

The _() Method

Alias for the translate() method in your translator

Allows three arguments:

  • string locale = ''
  • string key = ''
  • string fallback = ''

Returns a string which is the translated string.

Locale is the ColdFusion locale that tells your translator which language to return. If the locale provided into the method is not in the list of the CFML server's supported locales, a fallback of 'en_US' will be used. The list of supported locales comes from server.coldfusion.supportedLocales.

Key is the unique identifier for that translation string. The specifics of this depend entirely on your translator. CoreConX makes no modifications or validity checks on this value.

Fallback is the fallback string to be used if the key is missing. Again, this is up to your translator to implement.

If no translator has been provided, the method will fake a translation by returning the fallback string (if provided) or the key (if provided).

Possible Example (depending on your translator):

<cfscript>
    translatorComponent = 'com.translator';
    coreconx.setTranslator( translatorComponent );

    writeOutput( coreconx._( 'en_US', 'greeting', 'Hello' ) ); // "hello"
    writeOutput( coreconx._( 'es_MX', 'greeting', 'Hello' ) ); // "hola"
</cfscript>

The _() Shortcut Method

If you use the CoreConX framework to extend your Application.cfc, you get the _() shortcutted for you. This means you can use it without prefixing coreconx before each call.

<cfscript>
    writeOutput( _( 'en_US', 'greeting', 'Hello' ) ); // "hello"
    writeOutput( _( 'es_MX', 'greeting', 'Hello' ) ); // "hola"
</cfscript>
Clone this wiki locally