I am using typeaheadjs and I would like to transliterate search query -before- it is sent to server.
@Edvad Zagorski gave excellent php array for doing this: https://stackoverflow.com/a/6837302/377192
But what I would need is to do the same in beforeSend() method. So if user starts typing something like
čikago
it would send
cikago
to server.
I tried countless tricks of twig: json_encode, raw, url_encode... None worked as I don't really get those encodings.
Is it even possible? I guess the problem is that beforeSend() method receives urlEncodedQuery, not the real one.
The tokenizer-based solution I proposed on GitHub:
var charMap = {'àáâããäå': 'a', 'èéêë': 'e', 'ç': 'c', 'ß': 'ss', /* ... */};
var normalize = function(str) {
$.each(charMap, function(chars, normalized) {
var regex = new RegExp('[' + chars + ']', 'gi');
str = str.replace(regex, normalized);
});
return normalized;
}
var queryTokenizer = function(q) {
var normalized = normalize(q);
return Bloodhound.tokenizers.whitespace(normalized);
};
var engine = new Bloodhound({
// ...
queryTokenizer: queryTokenizer
});
I had this same issue. Get the latest version of Typeahead (0.10.1 at the time of this post). This version integrates the use of Bloodhound which allowed be to use a .replace() on the query before submitting.
Here are the Bloodhound docs:
https://github.com/twitter/typeahead.js/blob/master/doc/bloodhound.md#remote