Last active
November 26, 2021 13:50
-
-
Save nkrapivin/5196cc84c72329354c3a3600a8f2c159 to your computer and use it in GitHub Desktop.
Rutranslit GML library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// @function Rutranslit([useSoftSigns]) | |
/// @param {bool} [useSoftSigns] (optional) true - Translit soft signs, false - ignore them. | |
/// @description This class allows you to convert Russian strings to English translit. | |
/// @returns {Rutranslit} self | |
function Rutranslit(useSoftSigns) constructor { | |
// private: | |
m_lookup = undefined; | |
/// @description (private) Initializes the lookup table. | |
static __init = function(__doUseSoftSigns) { | |
// боже мопсовый.... | |
// я бы добавил сюда украинский, но моих познаний данного языка недостаточно :( | |
var __initial = [ | |
[ "а", "а" ], | |
[ "б", "b" ], | |
[ "в", "v" ], | |
[ "г", "g" ], | |
[ "д", "d" ], | |
[ "е", "e" ], | |
[ "ё", "yo" ], | |
[ "ж", "zh" ], | |
[ "з", "z" ], | |
[ "и", "i" ], | |
[ "й", "y" ], | |
[ "к", "k" ], | |
[ "л", "l" ], | |
[ "м", "m" ], | |
[ "н", "n" ], | |
[ "о", "o" ], | |
[ "п", "p" ], | |
[ "р", "r" ], | |
[ "с", "s" ], | |
[ "т", "t" ], | |
[ "у", "u" ], | |
[ "ф", "f" ], | |
[ "х", "h" ], | |
[ "ц", "ts" ], | |
[ "ч", "ch" ], | |
[ "ш", "sh" ], | |
[ "щ", "sch" ], | |
[ "ы", "y" ], // ????????? | |
[ "э", "e" ], // one does not usually do this | |
[ "ю", "yu" ], | |
[ "я", "ya" ] | |
]; | |
if (__doUseSoftSigns) { | |
array_push(__initial, | |
[ "ь", "'" ], // Любить (to love) -> "Lyubit'", Школьник (schoolkid) -> Shkol'nik | |
[ "ъ", "i" ] // replace with 'j' if you want Polish latin pronounciation (Podiezd -> Podjezd) | |
); | |
} | |
else { | |
array_push(__initial, | |
[ "ь", "" ], | |
[ "ъ", "" ] | |
); | |
} | |
// initialize to empty structs. | |
m_lookup = { }; | |
// here we're completely fine with iterating backwards. | |
// i am too lazy to make a new local variable. | |
for (var __i = array_length(__initial) - 1; __i > -1; --__i) { | |
var __pair = __initial[@ __i]; | |
var __first = __pair[@ 0]; | |
var __second = __pair[@ 1]; | |
// just in case :p | |
var __firstU = string_upper(__first); | |
var __secondU = string_upper(__second); | |
variable_struct_set(m_lookup, __first, __second); | |
variable_struct_set(m_lookup, __firstU, __secondU); | |
variable_struct_set(m_lookup, __second, __first); | |
variable_struct_set(m_lookup, __secondU, __firstU); | |
} | |
}; | |
// TODO: pass translit arguments..? i.e. do not include a ъ or whatever... | |
__init(is_undefined(useSoftSigns) ? false : bool(useSoftSigns)); | |
// public: | |
/// @param {string} russianString Russian language string to translit. | |
/// @returns {string} Translited English string | |
static doRuToEn = function(russianString) { | |
var __result = ""; | |
// if GM gets 0 indexed strings | |
// get rid of + 1 | |
// and set __i to 0 | |
var __end = string_length(russianString) + 1; | |
for (var __i = 1; __i < __end; ++__i) { | |
var __chr = string_char_at(russianString, __i); | |
if (variable_struct_exists(m_lookup, __chr)) { | |
__result += variable_struct_get(m_lookup, __chr); | |
} | |
else { | |
__result += __chr; | |
} | |
} | |
return __result; | |
}; | |
/// @returns {string} This object as a string. | |
static toString = function() { | |
return "Rutranslit(): " + string(ptr(self)); | |
}; | |
} | |
/// @function __Testcase() | |
/// @description Go figure. | |
function __Testcase() { | |
var __original = "Привет, я тут должен написать что-то нормальное, но мне лень."; | |
var __inst = new Rutranslit(false); | |
var __instsoft = new Rutranslit(true); | |
var __toshow = __inst.doRuToEn(__original); | |
var __toshows = __instsoft.doRuToEn(__original); | |
var __outmsg = string("Rutranslit Testcase:" + "\r\n" + "Original: " + string(__original) + "\r\n" + "Translit: " + string(__toshow) + "\r\n" + "Translit Soft: " + string(__toshows)); | |
show_message_async(__outmsg); | |
show_debug_message(__outmsg); | |
} | |
// comment out this line in production. | |
__Testcase(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment