Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add multi language support. #276

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions glogg.pro
Original file line number Diff line number Diff line change
Expand Up @@ -318,3 +318,6 @@ else {
perf {
QMAKE_CXXFLAGS += -DGLOGG_PERF_MEASURE_FPS
}

TRANSLATIONS += translations/glogg_zh_CN.ts

4 changes: 2 additions & 2 deletions src/abstractlogview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -360,10 +360,10 @@ void AbstractLogView::mousePressEvent( QMouseEvent* mouseEvent )
{
// Prepare the popup depending on selection type
if ( selection_.isSingleLine() ) {
copyAction_->setText( "&Copy this line" );
copyAction_->setText( tr("&Copy this line") );
}
else {
copyAction_->setText( "&Copy" );
copyAction_->setText( tr("&Copy") );
copyAction_->setStatusTip( tr("Copy the selection") );
}

Expand Down
6 changes: 6 additions & 0 deletions src/configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ Configuration::Configuration()
mainFont_ = QFont("monaco", 10);
mainFont_.setStyleHint( QFont::Courier, QFont::PreferOutline );

languageLocale_ = "en_US";

mainRegexpType_ = ExtendedRegexp;
quickfindRegexpType_ = FixedString;
quickfindIncremental_ = true;
Expand Down Expand Up @@ -95,6 +97,9 @@ void Configuration::retrieveFromStorage( QSettings& settings )
if ( settings.contains( "session.loadLast" ) )
loadLastSession_ = settings.value( "session.loadLast" ).toBool();

if ( settings.contains( "appearance.language" ) )
languageLocale_ = settings.value( "appearance.language" ).toString();

// View settings
if ( settings.contains( "view.overviewVisible" ) )
overviewVisible_ = settings.value( "view.overviewVisible" ).toBool();
Expand Down Expand Up @@ -130,6 +135,7 @@ void Configuration::saveToStorage( QSettings& settings ) const
settings.setValue( "polling.enabled", pollingEnabled_ );
settings.setValue( "polling.intervalMs", pollIntervalMs_ );
settings.setValue( "session.loadLast", loadLastSession_);
settings.setValue( "appearance.language", languageLocale_);

settings.setValue( "view.overviewVisible", overviewVisible_ );
settings.setValue( "view.lineNumbersVisibleInMain", lineNumbersVisibleInMain_ );
Expand Down
6 changes: 6 additions & 0 deletions src/configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ class Configuration : public Persistable {
QFont mainFont() const;
void setMainFont( QFont newFont );

QString languageLocale() const
{return languageLocale_;}
void setLanguageLocale(QString locale)
{languageLocale_ = locale;}

// Accesses the regexp types
SearchRegexpType mainRegexpType() const
{ return mainRegexpType_; }
Expand Down Expand Up @@ -106,6 +111,7 @@ class Configuration : public Persistable {
bool pollingEnabled_;
uint32_t pollIntervalMs_;
bool loadLastSession_;
QString languageLocale_;

// View settings
bool overviewVisible_;
Expand Down
4 changes: 2 additions & 2 deletions src/crawlerwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -693,8 +693,8 @@ void CrawlerWidget::setup()
searchInfoLine->setLineWidth( 1 );
searchInfoLineDefaultPalette = searchInfoLine->palette();

ignoreCaseCheck = new QCheckBox( "Ignore &case" );
searchRefreshCheck = new QCheckBox( "Auto-&refresh" );
ignoreCaseCheck = new QCheckBox(tr("Ignore &case"));
searchRefreshCheck = new QCheckBox(tr("Auto-&refresh"));

// Construct the Search line
searchLabel = new QLabel(tr("&Text: "));
Expand Down
41 changes: 39 additions & 2 deletions src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,15 @@ MainWindow::MainWindow( std::unique_ptr<Session> session,
,versionChecker_()
#endif
{
readSettings();

//setup language
lastLanguageLocale_ = "";
updateLanguage();

createActions();
updateRecentFileActions();

createMenus();
createToolBars();
// createStatusBar();
Expand All @@ -86,7 +94,7 @@ MainWindow::MainWindow( std::unique_ptr<Session> session,

setWindowIcon( mainIcon_ );

readSettings();


// Connect the signals to the mux (they will be forwarded to the
// "current" crawlerwidget
Expand Down Expand Up @@ -527,8 +535,10 @@ void MainWindow::options()
{
OptionsDialog dialog(this);
signalMux_.connect(&dialog, SIGNAL( optionsChanged() ), SLOT( applyConfiguration() ));
connect(&dialog, SIGNAL( optionsChanged() ),this, SLOT( updateLanguage() ));
dialog.exec();
signalMux_.disconnect(&dialog, SIGNAL( optionsChanged() ), SLOT( applyConfiguration() ));
disconnect(&dialog,SIGNAL( optionsChanged() ),this, SLOT( updateLanguage() ));
}

// Opens the 'About' dialog box.
Expand Down Expand Up @@ -967,6 +977,34 @@ void MainWindow::updateInfoLine()
}
}

void MainWindow::loadLanguage(QString locale)
{
// remove the old translator
qApp->removeTranslator(&translator_);
qApp->removeTranslator(&translatorQt_);

// load the new translator
QString path = QApplication::applicationDirPath();
path.append("/translations/");

if(translatorQt_.load(QLocale(locale),"qt","_",path,".qm"))
qApp->installTranslator(&translatorQt_);
if(translator_.load(QLocale(locale),"glogg","_",path,".qm"))
qApp->installTranslator(&translator_);
}

void MainWindow::updateLanguage()
{
std::shared_ptr<Configuration> config =
Persistent<Configuration>( "settings" );
QString locale = config->languageLocale();
if(lastLanguageLocale_ == locale)
return;

lastLanguageLocale_ = locale;
loadLanguage(locale);
}

// Write settings to permanent storage
void MainWindow::writeSettings()
{
Expand Down Expand Up @@ -1002,7 +1040,6 @@ void MainWindow::readSettings()

// History of recent files
GetPersistentInfo().retrieve( QString( "recentFiles" ) );
updateRecentFileActions();

// GetPersistentInfo().retrieve( QString( "settings" ) );
GetPersistentInfo().retrieve( QString( "filterSet" ) );
Expand Down
9 changes: 9 additions & 0 deletions src/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include <memory>
#include <QMainWindow>
#include <QTranslator>

#include "session.h"
#include "crawlerwidget.h"
Expand Down Expand Up @@ -126,6 +127,9 @@ class MainWindow : public QMainWindow
// Notify the user a new version is available
void newVersionNotification( const QString& new_version );

// Update UI language
void updateLanguage();

signals:
// Is emitted when new settings must be used
void optionsChanged();
Expand Down Expand Up @@ -154,12 +158,17 @@ class MainWindow : public QMainWindow
void displayQuickFindBar( QuickFindMux::QFDirection direction );
void updateMenuBarFromDocument( const CrawlerWidget* crawler );
void updateInfoLine();
void loadLanguage(QString locale);

std::unique_ptr<Session> session_;
std::shared_ptr<ExternalCommunicator> externalCommunicator_;
std::shared_ptr<RecentFiles> recentFiles_;
QString loadingFileName;

QTranslator translator_;
QTranslator translatorQt_;
QString lastLanguageLocale_;

// Encoding
struct EncodingList {
const char* name;
Expand Down
40 changes: 40 additions & 0 deletions src/optionsdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ OptionsDialog::OptionsDialog( QWidget* parent ) : QDialog(parent)
connect(pollingCheckBox, SIGNAL( toggled( bool ) ),
this, SLOT( onPollingChanged() ) );

locales_ = getLanguagesList();
setupLanguagesList();

updateDialogFromConfig();

setupIncremental();
Expand Down Expand Up @@ -111,6 +114,37 @@ void OptionsDialog::setupPolling()
pollIntervalLineEdit->setEnabled( pollingCheckBox->isChecked() );
}

QStringList OptionsDialog::getLanguagesList()
{
QString m_langPath = QApplication::applicationDirPath();
m_langPath.append("/translations");
QDir dir(m_langPath);
QStringList fileNames = dir.entryList(QStringList("glogg_*.qm"));

QStringList locales;
for (int i = 0; i < fileNames.size(); ++i)
{
// get locale extracted by filename
QString locale;
locale = fileNames[i];
locale.truncate(locale.lastIndexOf('.'));
locale.remove(0, locale.indexOf('_') + 1);
locales << locale;
}
locales << "en_US"; //The "C" locale is identical in behavior to English/UnitedStates.
return locales;
}

void OptionsDialog::setupLanguagesList()
{
QStringList langNames;
for (int i = 0; i < locales_.size(); ++i)
langNames << QLocale(locales_[i]).nativeLanguageName();
// langNames << QLocale::languageToString(QLocale(locales_[i]).language());

selectLanguagesComboBox->addItems(langNames);
}

// Convert a regexp type to its index in the list
int OptionsDialog::getRegexpIndex( SearchRegexpType syntax ) const
{
Expand Down Expand Up @@ -176,6 +210,9 @@ void OptionsDialog::updateDialogFromConfig()

// Last session
loadLastSessionCheckBox->setChecked( config->loadLastSession() );

// Language
selectLanguagesComboBox->setCurrentIndex(locales_.indexOf( config->languageLocale() ));
}

//
Expand Down Expand Up @@ -224,6 +261,9 @@ void OptionsDialog::updateConfigFromDialog()
config->setPollIntervalMs( poll_interval );

config->setLoadLastSession( loadLastSessionCheckBox->isChecked() );

config->setLanguageLocale( locales_[selectLanguagesComboBox->currentIndex()] );

emit optionsChanged();
}

Expand Down
4 changes: 4 additions & 0 deletions src/optionsdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,16 @@ class OptionsDialog : public QDialog, public Ui::OptionsDialog
void setupIncremental();
void setupPolling();

QStringList getLanguagesList();
void setupLanguagesList();

int getRegexpIndex( SearchRegexpType syntax ) const;
SearchRegexpType getRegexpTypeFromIndex( int index ) const;

void updateDialogFromConfig();

QValidator* polling_interval_validator_;
QStringList locales_;
};

#endif
Loading