Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I recently struggled with the same problem.</p> <p>Finding a clean way to do this was quite a challenge - most alternatives were either..</p> <p>1) Duplicate your code base per locale (WTH)</p> <p>2) Download localized files overriding each of your components (Maintenance hell? What about the poor translators?)</p> <p>3) Use/generate a static file containing translations and refer to it (All languages are downloaded? Extra build step to generate it? How do you keep them in synch?)</p> <p>I tried to get the best of all worlds and ended up with a utility class responsible for:</p> <p>1) Loading the ExtJS translation files (which basically apply overrides to extjs base components)</p> <p>2) Loading a locale specific property resourcebundle (specifying which locale to load) from the server.</p> <p>3) Prototyping String with a translate() method which queries the loaded store (containing the message bundle from the server) and returns the translation based on the value of the string.</p> <p>This is the gist of things:</p> <p>Bundle &amp; prototyping: </p> <pre><code>localeStore.load({ callback : function(records, operation, success) { // Define translation function (NB! Must be defined before any components which want to use it.) function translate() { var record = localeStore.getById(this.valueOf()) ; if(record === null) { alert('Missing translation for: ' + this.valueOf()); // Key is not found in the corresponding messages_&lt;locale&gt;.properties file. return this.valueOf(); // Return key name as placeholder } else { var value = record.get('value'); } return value; } String.prototype.translate = translate; callback.call(); // call back to caller(app.js / Ext.Application), loading rest of application } }); </code></pre> <p>As an example from a view:</p> <pre><code>this.copyButton = Ext.create('Ext.button.Button', { disabled: true, text: 'DOCUMENT_LIBRARY_MENU_COPYTO_BUTTON'.translate(), action: 'openCopyDialog' }); </code></pre> <p>Bundle on the server (mesages_en.properties): DOCUMENT_LIBRARY_MENU_COPYTO_BUTTON=Copy file etc..</p> <p>Pros: </p> <ul> <li>No-fuss code, 'Your_key'.translate() makes it easy to read and aware that this is a localized string</li> <li>None/little maintenance overhead (Keeping an override file for each locale? Jesus..)</li> <li>You only load the locale you need - not the whole shabang.</li> <li>If you really want to, you could even have your own translation for the ExtJS locale files in the same bundle.</li> <li>You could write unit tests to ensure that all bundles contain the same keys, thus avoiding orphaned translations later</li> </ul> <p>Cons:</p> <ul> <li>Synchronous - the store must be loaded before your main app starts. I solved this by adding a callback from the utility class which was called once all texts were loaded.</li> <li>No real-time population of texts.. though I didn't want to make my users overload the server either :P</li> </ul> <p>So far my approach has worked out pretty well for my requirements. Site load isn't noticeably slower and the bundles (containing ~200 keys/values per bundle) measure out at ~10kb during load.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload