admin管理员组

文章数量:1336631

In Ruby on Rails you can easily convert "any" text into a format which would work for subdomains/pathnames.

1) "I am nobody." -> "i-am-nobody"
2) "Grünkohl is a german word." -> "grunkohl-is-a-german-word"

I'd like to do this on the client-side for high responsiveness (alternative would be via Ajax).

The last example is called transliteration (converting Umlauts and other non-latin alphabets letters into latin ones). Transliteration would be a nice2have feature (in such cases I could fallback to Ajax to let Iconv do it).

Anybody knows how to do this with JavaScript? My current code works fine but has issues with multiple blank spaces, and Tête-à-tête bees Tte--tte which is just ugly.

In Ruby on Rails you can easily convert "any" text into a format which would work for subdomains/pathnames.

1) "I am nobody." -> "i-am-nobody"
2) "Grünkohl is a german word." -> "grunkohl-is-a-german-word"

I'd like to do this on the client-side for high responsiveness (alternative would be via Ajax).

The last example is called transliteration (converting Umlauts and other non-latin alphabets letters into latin ones). Transliteration would be a nice2have feature (in such cases I could fallback to Ajax to let Iconv do it).

Anybody knows how to do this with JavaScript? My current code works fine but has issues with multiple blank spaces, and Tête-à-tête bees Tte--tte which is just ugly.

Share Improve this question asked Jan 24, 2010 at 17:40 Marcel JackwerthMarcel Jackwerth 54.8k9 gold badges76 silver badges88 bronze badges 2
  • Out of curiousity, how is this related to high responsiveness? Usually this is done one time on the server - when the record is created. – August Lilleaas Commented Jan 24, 2010 at 17:48
  • Well, it's rather meant to be a "suggested" name which the user can still alter. So while the user fills <input name="topic">, a second <input name="permalink"> should be filled. – Marcel Jackwerth Commented Jan 24, 2010 at 21:55
Add a ment  | 

3 Answers 3

Reset to default 9

When I needed this I used the Django javascript implementation for this wich covers most of this and even more :)

It can be found here: https://code.djangoproject./browser/django/trunk/django/contrib/admin/static/admin/js/urlify.js

urlify for node.js npm package https://npmjs/package/parameterize

Here's a JS to transliterate the passed phrase Russian to English for URL needs. One can modify its data to apply for French or any other language. Anything besides letters and numbers is substituted with "-", double and ending "-" are removed.

function url(word, letterVersionOrder) {
        var letters = 'a b v g d e ["zh","j"] z i y k l m n o p r s t u f h c ch sh ["shh","shch"] ~ y ~ e yu ya ~ ["jo","e"]'.split(' ');
        var wordSeparator = '';
        word = word.toLowerCase();
        for (var i = 0; i < word.length; ++i) {
            var charCode = word.charCodeAt(i);
            var chars = (charCode >= 1072 ? letters[charCode - 1072] : word[i]);
            if (chars.length < 3) {
                wordSeparator += chars;
            } else {
                wordSeparator += eval(chars)[letterVersionOrder];
            }
        }
        return(wordSeparator.
                replace(/[^a-zA-Z0-9\-]/g, '-').
                replace(/[-]{2,}/gim, '-').
                replace(/^\-+/g, '').
                replace(/\-+$/g, ''));
    }

Here is a faster minified version:

function url(w, v) { var tr = 'a b v g d e ["zh","j"] z i y k l m n o p r s t u f h c ch sh ["shh","shch"] ~ y ~ e yu ya ~ ["jo","e"]'.split(' '); var ww = ''; w = w.toLowerCase(); for (var i = 0; i < w.length; ++i) { var cc = w.charCodeAt(i); var ch = (cc >= 1072 ? tr[cc - 1072] : w[i]); if (ch.length < 3) {ww += ch;} else {ww += eval(ch)[v];} } return(ww.replace(/[^a-zA-Z0-9\-]/g, '-').replace(/[-]{2,}/gim, '-').replace(/^\-+/g, '').replace(/\-+$/g, ''));}

Adopted from here.

本文标签: ruby on railsHow to parameterizetransliterate in JavascriptStack Overflow