Template:MDN:Common

  • Revision slug: Template:MDN:Common
  • Revision title: Template:MDN:Common
  • Revision id: 289184
  • Created:
  • Creator: trevorh
  • Is current revision? No
  • Comment

Revision Content

<% module.exports = { /** * Given a set of strings like this: * { "en-US": "Foo", "de": "Bar", "es": "Baz" } * Return the one which matches the current locale. */ localString: function (strings) { var lang = env.locale; if (!(lang in strings)) lang = 'en-US'; return strings[lang]; }, /** * DEVELOPMENTAL - DO NOT USE THIS IN PRODUCTION * Creates a localized internal link * path - path of the document to create link to. (Will handle old style links if necessary) * text (optional) - text of the link, defaults to page title * title (optional) - link title * locale (optional) - locale to link to. Only use if the link MUST be to another locale */ linkInternal: function (in_path, in_text, in_title, in_locale) { var out = '(Missing path)'; // No use doing anything if there is no path if (in_path) { var p = kuma.url.parse(env.url, true); var base_url = p.protocol + '//' + p.host; // Fix path - remove any locale, add /docs, replace space with underscore and do a htmlEscape var path = '/docs/' + kuma.htmlEscape(in_path.replace(/^/?(?:\w{2}+(?:-\w{2}+)?)?/(?:docs/)?/,'').replace(/\s/g,'_')); // Figure out locale var locale = env.locale; if (in_locale) { locale = kuma.htmlEscape(in_locale.replace(/\//g,'')); } out = ''; } return out; } } %>

Revision Source

<% module.exports = {
    
    /**
     * Given a set of strings like this:
     *     { "en-US": "Foo", "de": "Bar", "es": "Baz" }
     * Return the one which matches the current locale.
     */
    localString: function (strings) {
        var lang = env.locale;
        if (!(lang in strings)) lang = 'en-US';
        return strings[lang];
    },

    /**
     * DEVELOPMENTAL - DO NOT USE THIS IN PRODUCTION
     * Creates a localized internal link
     * path - path of the document to create link to. (Will handle old style links if necessary)
     * text (optional) - text of the link, defaults to page title
     * title (optional) - link title
     * locale (optional) - locale to link to. Only use if the link MUST be to another locale
     */

    linkInternal: function (in_path, in_text, in_title, in_locale) {
        var out = '(Missing path)';

        // No use doing anything if there is no path
        if (in_path) {
            var p = kuma.url.parse(env.url, true);
            var base_url = p.protocol + '//' + p.host;

            // Fix path - remove any locale, add /docs, replace space with underscore and do a htmlEscape
            var path = '/docs/' + kuma.htmlEscape(in_path.replace(/^/?(?:\w{2}+(?:-\w{2}+)?)?/(?:docs/)?/,'').replace(/\s/g,'_'));

            // Figure out locale
            var locale = env.locale;
            if (in_locale) {
                locale = kuma.htmlEscape(in_locale.replace(/\//g,''));
            }

            out = '<a href="' + base_url + '/' + locale + path + '"';

            // Link title
            if (in_title) {
                out += ' title="' + kuma.htmlEscape(in_title) + '"';
            }

            // Get text of link (title of page), if no override
            var text = '';
            if (in_text) {
                text = kuma.htmlEscape(in_text);
            }
            else {
                // Use path as fallback
                text = '/' + locale + path;
                // If the locale version exists, get its title
                var pageLocale = wiki.getPage ('/' + locale + path);
                if (pageLocale) {
                    text = pageLocale.title;
                }
                else if (locale.toLowerCase() != 'en-us') {
                    // Try the English version
                    var pageEnglish = wiki.getPage('/en-US' + path);
                    if (pageEnglish)  {
                        text = pageEnglish.title;
                    }
                }
            }
            out += '>' + text + '</a>';
        }
        return out;
    }

} %>
Revert to this revision