3

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 becomes Tte--tte which is just ugly.

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. 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. Commented Jan 24, 2010 at 21:55

3 Answers 3

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.com/browser/django/trunk/django/contrib/admin/static/admin/js/urlify.js

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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.

1 Comment

Also for some multilanguage URLs needs you may want to transliterate your URL back. This is hardly possible as forward transliteration looses some information (casting 33 Russian letters to 26 English, for example). This could also be time consuming for dynamically opened pages. The solution is to transliterate each URL element beforehand and store it as a field for this substance in database. So you do transliteration when you change the substance name and save result to something like url_name field.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.