Create a gist now

Instantly share code, notes, and snippets.

@psbots /jqueryhindi.js Secret
Created Mar 23, 2014

What would you like to do?
Hindi Language IME for Firefox OS
/*The following code is a quick proof of concept hack and is in no way complete.
The input method rules have been taken from jquery.ime and the implementation is specific to that required by Firefox OS (init() and click() functions)
The IME is not functional and has issues when large input is given.
To test it in Firefox browser, clone gaia, place this file inside apps/keyboard/js/imes/jqueryhindi/jqueryhindi.js
The layout is also to be specified and placed in apps/keyboard/js/layouts/
The code currently uses some jquery, so the jQuery has to be loaded before this file. This dependency will not be present in the final version of the IMEs
*/
var hiPhonetic = {
id: 'hi-phonetic',
name: 'फोनेटिक',
description: 'Phonetic keyboard for Hindi language',
date: '2013-02-09',
author: 'Parag Nemade',
license: 'GPLv3',
version: '1.0',
patterns: [
['्f', '\u200c'],
['\\~', ''],
['\\`',''],
['\\!', ''],
['1', ''],
['\\@', ''],
['2', ''],
['\\#', ''],
['3', ''],
['\\$',''],
['4', ''],
['\\%', ''],
['5', ''],
['6', ''],
['7', ''],
['8', ''],
['\\(', '('],
['9', ''],
['\\)', ')'],
['0', ''],
['\\_', '_'],
['\\-', '-'],
['\\+', '+'],
['\\=', '='],
['Q', ''],
['q', ''],
['W', ''],
['w', ''],
['E', ''],
['e', ''],
['R', ''],
['r', ''],
['T', ''],
['t', ''],
['Y', ''],
['y', ''],
['U', ''],
['u', ''],
['I', ''],
['i', 'ि'],
['O', ''],
['o', ''],
['P', ''],
['p', ''],
['\\{', ''],
['\\[', ''],
['\\}', ''],
['\\]', ''],
['A', ''],
['a', ''],
['S', ''],
['s', ''],
['D', ''],
['d', ''],
['F', ''],
['f', ''],
['G', ''],
['g', ''],
['H', ''],
['h', ''],
['J', ''],
['j', ''],
['K', ''],
['k', ''],
['L', ''],
['l', ''],
[':', ''],
[';', ''],
['"', ''],
['\'', ''],
['\\|', ''],
['\\\\', ''],
['Z', ''],
['z', ''],
['x', ''],
['C', ''],
['c', ''],
['V', ''],
['v', ''],
['B', ''],
['b', ''],
['N', ''],
['n', ''],
['M', ''],
['m', ''],
['\\<', ''],
[',', ','],
['\\>', ''],
['\\.', ''],
['\\?', ''],
['/', ''],
['\\^', 'ज्ञ'],
['X', 'क्ष'],
['\\*', 'श्र']
]
};
//transliterate
var imehindi = {};
imehindi.jquery = {};
imehindi.jquery.transliterate = {
parse: function (input) {
var patterns, regex, rule, replacement, i, retval;
patterns = hiPhonetic.patterns;
if ( $.isFunction( patterns ) ) {
// For backwards compatibility, allow the rule functions to return plain
// string. Determine noop by checking whether input is different from
// output. If the rule function returns object, just return it as-is.
retval = patterns.call( this, input, context );
if ( typeof retval === 'string' ) {
return { noop: input === retval, output: retval };
}
return retval;
}
for ( i = 0; i < patterns.length; i++ ) {
rule = patterns[i];
regex = new RegExp( rule[0] + '$' );
// Last item in the rules.
// It can also be a function, because the replace
// method can have a function as the second argument.
replacement = rule.slice( -1 )[0];
// Input string match test
if ( regex.test( input ) ) {
// Context test required?
if ( rule.length === 3 ) {
if ( new RegExp( rule[1] + '$' ).test( context ) ) {
console.log(input.replace(regex,replacement));
return { noop: false, output: input.replace( regex, replacement ) };
}
} else {
console.log(input.replace(regex,replacement));
return { noop: false, output: input.replace( regex, replacement ) };
}
}
}
// No matches, return the input
console.log(input.replace(regex,replacement));
return { noop: true, output: input };
}
};
//keyboard API Firefox OS
(function() {
var keyboard, buffer = "";
const SPACE = 32;
const TAB = 9;
const RETURN = 13;
const BACKSPACE = 8;
function isBufferEmpty() {
return buffer == "";
}
function clearBuffer() {
buffer = "";
}
function addToBuffer(keyCode) {
if (keyCode == SPACE || keyCode == RETURN || keyCode == TAB) {
buffer = buffer + String.fromCharCode(keyCode);
inputDone();
}
else if (keyCode == BACKSPACE) {
backspace();
}
else {
buffer = buffer + String.fromCharCode(keyCode);
}
}
function backspace() {
if (!isBufferEmpty()) {
buffer = buffer.substring(0, buffer.length -1);
keyboard.setComposition(imehindi.jquery.transliterate.parse(buffer).output);
}
else {
keyboard.sendKey(KeyEvent.DOM_VK_BACK_SPACE);
}
}
function inputDone() {
var parsedText = imehindi.jquery.transliterate.parse(buffer).output;
keyboard.endComposition(parsedText);
clearBuffer();
}
InputMethods.jqueryhindi = {
init: init,
click: click
};
function init(interfaceObject) {
keyboard = interfaceObject;
}
function click(keycode, x, y) {
addToBuffer(keycode);
if (!isBufferEmpty()) {
console.log(buffer);
keyboard.setComposition(imehindi.jquery.transliterate.parse(buffer).output);
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment