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.
- 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
3 Answers
Reset to default 9When 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
版权声明:本文标题:ruby on rails - How to parameterizetransliterate in Javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742363651a2460852.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论