Skip to content

Instantly share code, notes, and snippets.

@psbots
Created March 23, 2014 10:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save psbots/aa1887f0a53c61611061 to your computer and use it in GitHub Desktop.
Save psbots/aa1887f0a53c61611061 to your computer and use it in GitHub Desktop.
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