Actions

RFC XLIFF extjs api

Actually many extensions (all ???) use directly TYPO3.lang object to access the localization. Some extension use standard API to populate this object key per key (with LANG->sL, ...) and other use directly t3lib_div::readLLfile(). In the second case, the object doesn't contain only string, but multi dimensional array with plural forms and source label. To simplify the localization of ExtJS class, we can create an standard API, like TYPO3.l10n

I have a basic implementation and a new version of the workspace extension based on the API.

The implementation of TYPO3.l10n is simple:


TYPO3.l10n = {
    localize: function(label, replace, plural) {
        if (typeof TYPO3.lang[label] === 'undefined') return false;

        var i = plural || 0,
                translationUnit = TYPO3.lang[label],
                label = null, regexp = null;

        // Get localized label
        if (Ext.isString(translationUnit)) {
            label = translationUnit;
        } else {
            label = translationUnit[i]['target'];
        }

        // Replace
        if (typeof replace !== 'undefined') {
            for (key in replace) {
                console.log('%' + key + '|%s');
                regexp = new RegExp('%' + key + '|%s');
                label = label.replace(regexp, replace[key]);
            }
        }

        return label;
    }
}

Extension developper can use this API:

# Singular
TYPO3.l10n.localize("result")

The API must offer more feature (plural form per language, ...) but this must be implemented later. Other feature like TYPO3.l10n.get() (with or without ExtDirect and cache usage can be implemented later)

Comments by Rens

  • My preferred namespace declaration would be Ext.ns('TYPO3.l10n');
  • Why add localize as function? It's just called once, and has only a simple return. It would be more efficient to use:
    Ext.ns('TYPO3', 'TYPO3.l10n');
    
    /** 
     * Localize label with plural support
     * 
     * @param label
     * @param plural
     */
    TYPO3.l10n = function (label, plural) {
        if (typeof TYPO3.lang[label] === 'undefined') return false;
    
        var i = plural || 0,
                translationUnit = TYPO3.lang[label];
    
        if (Ext.isString(translationUnit)) {
            return translationUnit;
        } else {
            return translationUnit[i]['target'];
        }   
    }
    
  • For TYPO3 phoenix we've been spoke about a javascript api based on the internal PHP localization classes, we came up with something like:
    /**
     * Get the localized version of the key if found
     *
     * @param {string} packageKey the package to which the key belongs
     * @param {string} localizationKey the key which should be translated
     * @param {object} placeholderValues an object containing the keys and values to replace
     * @param {string} quantity the quantity, could be one of the following: zero, one, two, few, many, other
     * @return {string} the localized string, with eventually processed placeholders or the key if no translation found
     */
    F3.TYPO3.Core.I18n.get(packageKey, localizationKey, placeholderValues, quantity);
    
    // You could call this simply using:
    F3.TYPO3.Core.I18n.get('TYPO3', 'foobar', {replace1: 'bar'}); // For replaces like we now also can have: Are you sure you want to %s?
    // Or:
    F3.TYPO3.Core.I18n.get('TYPO3', 'person', null, 'two'); // String value 'two' is based on the current PHP classes, could still change
    

    Have a look at https://review.typo3.org/#change,2609 for the actual code, labels are still static since we don't actually fill them using PHP yet.

P.S. Comments on our API suggestion are highly welcome!

Updated by Dominique Feyer almost 11 years ago ยท 6 revisions