tr字符串的梗

时间:2023-03-09 01:20:23
tr字符串的梗

工作问题总结

声明:应用程序代码为前人所写,仅仅只是维护代码,暂且不论代码风格瑕疵。

结论:

使用tr“包”起来的字符串,可能会被翻译为不同的语言,此时,应用程序在不同语言环境下可能会表现BUG。

1.问题及源码追溯过程

 // 应用程序调用函数1
void WinHelpManual::openFunction(QString func)
{
QWebPage *webPage = m_view_list.page();
QWebFrame *webFrame = webPage->frameAt(QPoint(, ));
QWebElement webElement = webFrame->documentElement();
webElement.evaluateJavaScript(func);
}
// 应用程序调用函数2
void WinHelpManual::openUrl()
{
QString Html = m_view_list.selectedHtml();
QString strUrlExp = "outline_[0-9]*.htm";
QRegExp urlRegExp(strUrlExp, Qt::CaseSensitive);
if (urlRegExp.indexIn(Html) != -)
{
QString strWebUrl = urlRegExp.cap(); // 匹配到的url
m_view_contents.setUrl(QUrl(m_path + strWebUrl));
}
}
// 应用程序调用函数3
bool WinHelpManual::event(QEvent *event)
{
if (event->type() == QEvent::KeyRelease)
{
QKeyEvent *ke = static_cast<QKeyEvent*> (event);
if (ke->modifiers() == PANEL_MODIFIER)
{
static int Ekey;
if (ke->key() == PANEL_BAR || ke->key() == PANEL_WAVE || ke->key() == PANEL_NUMERIC || ke->key() == PANEL_VECTOR || \
ke->key() == PANEL_MARK || ke->key() == PANEL_ITEM || ke->key() == PANEL_FORM || ke->key() == PANEL_USER || \
ke->key() == PANEL_VOLTRANGE_CLOCKWISE || ke->key() == PANEL_VOLTRANGE_ANTICLOCKWISE || ke->key() == PANEL_VOLTRANGE_AUTO\
|| ke->key() == PANEL_CURRRANGE_CLOCKWISE || ke->key() == PANEL_CURRRANGE_ANTICLOCKWISE || ke->key() == PANEL_CURRANGE_AUTO\
|| ke->key() == PANEL_EXT || ke->key() == PANEL_ACCURATE || ke->key() == PANEL_UTILITY || ke->key() == PANEL_MEASURRE\
|| ke->key() == PANEL_OTHERS || ke->key() == PANEL_SOURCE || ke->key() == PANEL_FILE || ke->key() == PANEL_INFO \
|| ke->key() == PANEL_IMAGESAVE || ke->key() == PANEL_HOLD || ke->key() == PANEL_SINGLE || ke->key() == PANEL_STROE \
|| ke->key() == PANEL_INTEG || ke->key() == PANEL_LOCALLOCK)
{
openFunction();
}
m_view_list.page()->findText("");//清除
switch (ke->key())
{
case PANEL_ESC:
case PANEL_HELP:
hide();
return true;
case PANEL_BAR:
{
Ekey = PANEL_BAR;
m_view_list.page()->findText(tr("Bar Graph Display(Bar)"));
}
break;
case PANEL_WAVE:
{
Ekey = PANEL_WAVE;
m_view_list.findText(tr("Waveform Display(WAVE)"));
}
break;
case PANEL_NUMERIC:
{
Ekey = PANEL_NUMERIC;
m_view_list.findText(tr("Numetic Data Display(NUMERIC)"));
}
break;
case PANEL_VECTOR:
Ekey = PANEL_VECTOR;
m_view_list.findText(tr("Vector Display(Vector)"));
break;
case PANEL_MARK:
{
switch (Ekey)
{
case PANEL_BAR:
m_view_list.findText(tr("Bar Display Mark (MARK)"));
break;
case PANEL_WAVE:
m_view_list.findText(tr("Waveform Display Mark(MARK)"));
break;
default:
break;
}
}
break;
case PANEL_ITEM:
{
switch (Ekey)
{
case PANEL_BAR:
m_view_list.findText(tr("Bar Display Item (ITEM)"));
break;
case PANEL_WAVE:
m_view_list.findText(tr("Waveform Display Item(ITEM)"));
break;
case PANEL_NUMERIC:
m_view_list.findText(tr("Numetic Display Item(ITEM)"));
break;
case PANEL_VECTOR:
m_view_list.findText(tr("Vector Display Item(ITEM)"));
break;
default:
break;
}
}
break;
case PANEL_FORM:
{
switch (Ekey)
{
case PANEL_BAR:
m_view_list.findText(tr("Bar Display Format(FORM)"));
break;
case PANEL_WAVE:
m_view_list.findText(tr("Waveform Display Format(FORM)"));
break;
case PANEL_NUMERIC:
m_view_list.findText(tr("Numetic Display Format(FORM)"));
break;
case PANEL_VECTOR:
m_view_list.findText(tr("Vector Display Format(FORM)"));
break;
default:
break;
}
}
break;
case PANEL_USER:
m_view_list.findText(tr("Numetic Data Display(NUMERIC)"));
break;
case PANEL_TAB:
if (m_view_contents.hasFocus())
{
m_view_contents.setFocusPolicy(Qt::NoFocus);
m_view_list.setFocus();
p_curWebView = &m_view_list;
repaint();
}
else if (m_view_list.hasFocus())
{
m_view_list.setFocusPolicy(Qt::NoFocus);
m_view_contents.setFocus();
p_curWebView = &m_view_contents;
repaint();
}
break;
case PANEL_VOLTRANGE_CLOCKWISE:
case PANEL_VOLTRANGE_ANTICLOCKWISE:
m_view_list.findText(tr("Voltage Range(RANGE UP/DOWN (V))"));
break;
case PANEL_VOLTRANGE_AUTO:
m_view_list.findText(tr("Auto Voltage Range(AUTO (V))"));
break;
case PANEL_CURRRANGE_CLOCKWISE:
case PANEL_CURRRANGE_ANTICLOCKWISE:
m_view_list.findText(tr("Current Range (RANGE UP/DOWN (A))"));
break;
case PANEL_CURRANGE_AUTO:
m_view_list.findText(tr("Auto Current Range(AUTO (A))"));
break;
case PANEL_EXT:
m_view_list.findText(tr("External Current Sensor Range (EXT SENSOR; option)"));
break;
case PANEL_ACCURATE:
m_view_list.findText(tr("Line Filter(LINE FILTER)"));
break;
case PANEL_UTILITY:
m_view_list.findText(tr("Utility (UTILITY)"));
break;
case PANEL_MEASURRE:
m_view_list.findText(tr("Computation(MEASURE)"));
break;
case PANEL_OTHERS:
m_view_list.findText(tr("Combination Display (COMBINE)"));
break;
case PANEL_SOURCE:
m_view_list.findText(tr("Measurement period(SYNC SOURCE)"));
break;
case PANEL_FILE:
m_view_list.findText(tr("File(File)"));
break;
case PANEL_INFO:
m_view_list.findText(tr(""));
break;
case PANEL_IMAGESAVE:
m_view_list.findText(tr("Screen capture function (Image )"));
break;
case PANEL_HOLD:
m_view_list.findText(tr("Holding Measured Values(HOLD)"));
break;
case PANEL_SINGLE:
m_view_list.findText(tr("Single Measurement(SINGLE)"));
break;
case PANEL_STROE:
m_view_list.findText(tr("Data Storage"));
break;
case PANEL_INTEG:
m_view_list.findText(tr("Integrated Power(Watt hours)"));
break;
case PANEL_LOCALLOCK:
m_view_list.findText(tr("Touch Unlock"));
break;
default:
return true;
}
openUrl();
}
}
return QWidget::event(event);
}
// QWebView page() 接口源码
QWebPage *QWebView::page() const
{
if (!d->page)
{
QWebView *that = const_cast<QWebView *>(this);
that->setPage(new QWebPage(that));
}
return d->page;
}
// QWebView findText() 接口源码
bool QWebView::findText(const QString &subString, QWebPage::FindFlags options)
{
if (d->page)
{
return d->page->findText(subString, options);
}
return false;
}
// QWebPage findText() 接口源码
bool QWebPage::findText(const QString &subString, FindFlags options)
{
return d->findText(subString, static_cast<QWebPageAdapter::FindFlag>(options.operator int()));
}
// QWebPagePrivate findText() 接口源码
bool QWebPageAdapter::findText(const QString& subString, FindFlag options)
{
::WebCore::FindOptions webCoreFindOptions = ; if (!(options & FindCaseSensitively))
{
webCoreFindOptions |= WebCore::CaseInsensitive;
} if (options & FindBackward)
{
webCoreFindOptions |= WebCore::Backwards;
} if (options & FindWrapsAroundDocument)
{
webCoreFindOptions |= WebCore::WrapAround;
} if (options & FindAtWordBeginningsOnly)
{
webCoreFindOptions |= WebCore::AtWordStarts;
} if (options & TreatMedialCapitalAsWordBeginning)
{
webCoreFindOptions |= WebCore::TreatMedialCapitalAsWordStart;
} if (options & FindBeginsInSelection)
{
webCoreFindOptions |= WebCore::StartInSelection;
} if (options & HighlightAllOccurrences)
{
if (subString.isEmpty())
{
page->unmarkAllTextMatches();
return true;
} return page->markAllMatchesForText(subString, webCoreFindOptions, /*shouldHighlight*/ true, /*limit*/ );
} if (subString.isEmpty())
{
page->mainFrame()->selection()->clear();
Frame* frame = page->mainFrame()->tree()->firstChild();
while (frame)
{
frame->selection()->clear();
frame = frame->tree()->traverseNextWithWrap(false);
}
} return page->findString(subString, webCoreFindOptions);
}
// Page findString() 接口源码
bool Page::findString(const String& target, FindOptions options)
{
if (target.isEmpty() || !mainFrame())
{
return false;
} bool shouldWrap = options & WrapAround; Frame* frame = focusController()->focusedOrMainFrame();
Frame* startFrame = frame;
do
{
if (frame->editor().findString(target, (options & ~WrapAround) | StartInSelection))
{
if (frame != startFrame)
{
startFrame->selection()->clear();
} focusController()->setFocusedFrame(frame);
return true;
}
frame = incrementFrame(frame, !(options & Backwards), shouldWrap);
} while (frame && frame != startFrame); // Search contents of startFrame, on the other side of the selection that we did earlier.
// We cheat a bit and just research with wrap on
if (shouldWrap && !startFrame->selection()->isNone())
{
bool found = startFrame->editor().findString(target, options | WrapAround | StartInSelection);
focusController()->setFocusedFrame(frame);
return found;
} return false;
}
// Page mainFrame() 成员方法源码
Frame* mainFrame() const { return m_mainFrame.get(); }
// Editor findString() 成员方法源码
bool Editor::findString(const String& target, FindOptions options)
{
VisibleSelection selection = m_frame->selection()->selection();
RefPtr<Range> resultRange = rangeOfString(target, selection.firstRange().get(), options);
if (!resultRange)
{
return false;
} m_frame->selection()->setSelection(VisibleSelection(resultRange.get(), DOWNSTREAM));
m_frame->selection()->revealSelection();
return true;
}

2.问题原因

tr翻译后导致字符串查找失败。

如上实例,因为tr“包”起来的字符串被翻译为不同语言,但是每次与之比较匹配的字符串始终为英语版本。

所以,总出现匹配失败的异常,导致调试排除浪费很长时间,在此特别备录。

Good Good Study, Day Day Up.

顺序 选择  循环 总结