QString text1, text2;text1 = labelEdit->text();for (int i=0; i < labelEdit->text().lenght; i++){ switch (text1[i]) { case 'a': text2[i] = 'a'; break; case 'b': text2[i] = 'б'; break; ..... ..... }}labelEdit2->setText(text2);
C++ (Qt)QString unTranslit(const QString &str){ QString result = str; QTextCodec::setCodecForCStrings(QTextCodec::codecForName("Windows-1251")); QString rule = "a=а;b=б;v=в;g=г;d=д;e=е;yo=ё;zh=ж;j=ж;z=з;i=и;y=й;k=к;ck=к;l=л;m=м;n=н;o=о;p=п;" "r=р;s=с;t=т;u=у;f=ф;h=х;c=ц;ch=ч;sh=ш;sch=щ;''=ъ;yi=ы;'=ь;ye=э;ju=ю;yu=ю;ja=я;" "ya=я;A=А;B=Б;V=В;G=Г;D=Д;E=Е;Yo=Ё;Zh=Ж;J=Ж;Z=З;I=И;Y=Й;K=К;L=Л;M=М;N=Н;O=О;" "P=П;R=Р;S=С;T=Т;U=У;F=Ф;H=Х;C=Ц;Ch=Ч;Sh=Ш;Sch=Щ;Yi=Ы;Ye=Э;Yu=Ю;Ju=Ю;Ya=Я;Ja=Я"; QStringList ruleList = rule.split(";"); QStringListIterator it(ruleList); while (it.hasNext()) { QString s = it.next(); result.replace(s.section('=',0,0), s.section('=',1,1)); } return result;} ... ui->textEdit2->setText(unTranslit(ui->textEdit->toPlainText()));
QStringListIterator it(ruleList); while (it.hasNext()) { QString s = it.next(); result.replace(s.section('=',0,0), s.section('=',1,1)); } return result;}
enum { translate_Forward = 0, translate_Backward = 1};int LookupDictionary( const QString & src, const QStringList & lst, int direction ){ for (int i = 0; i < lst.size(); i += 2) if (src.startsWith(lst[i + direction])) return i; return -1;}QString unTranslit(const QString & theStr, int translateDir){ QTextCodec::setCodecForCStrings(QTextCodec::codecForName("Windows-1251")); static const QString rule = {"..."}; // dictionary static QStringList ruleList; if (!ruleList.size()) { QStringList temp = rule.split(";"); for (int i = 0; i < temp.size(); ++i) { ruleList.append(temp[i].section('=', 0, 0).trimmed()); ruleList.append(temp[i].section('=', 0, 1).trimmed()); } } QString dst, temp, src = theStr; while (src.size()) { int index = LookupDictionary(src, ruleList, translateDir); temp = (index < 0) ? src[0] : ruleList[index + (translateDir ? -1 : 1)]; // handle upper-lower case here dst += temp; int numDel = (index < 0) ? 1 : ruleList[index + (translateDir ? 1 : 0)].size(); src.remove(0, numDel); } return dst;}