First pass at setting up translation framework.

This commit is contained in:
Jim Evins
2017-03-25 23:14:49 -04:00
parent e3e3441cdb
commit ca3d4a5d5c
19 changed files with 3358 additions and 49 deletions
+2
View File
@@ -34,6 +34,7 @@ find_package(Qt5Widgets 5.4 REQUIRED)
find_package(Qt5PrintSupport 5.4 REQUIRED)
find_package(Qt5Xml 5.4 REQUIRED)
find_package(Qt5Svg 5.4 REQUIRED)
find_package(Qt5LinguistTools)
if (MINGW)
# Locate Qt directories
@@ -49,6 +50,7 @@ find_package(ZLIB 1.2 REQUIRED)
#=======================================
add_subdirectory (glabels)
add_subdirectory (templates)
add_subdirectory (translations)
add_subdirectory (data)
+2 -2
View File
@@ -25,7 +25,7 @@ It is still missing several features to bring it in parity with glabels-3.4. Th
- Compatability with older glabels files
- Batch mode
- Internationalization
- Product template designer
- Custom product templates designer
- Online manual
## Build Instructions
@@ -48,7 +48,7 @@ These include
* Help is needed writing online documentation.
* Help is needed setting up internationalization and writing translations.
* Help is needed writing translations.
* Suggestions.
+2
View File
@@ -38,7 +38,9 @@ namespace glabels
///
class BarcodeBackends : public QObject
{
Q_OBJECT
/////////////////////////////////
// Life Cycle
/////////////////////////////////
+1
View File
@@ -101,6 +101,7 @@ set (glabels_sources
set (glabels_qobject_headers
AboutDialog.h
BarcodeBackends.h
BarcodeMenu.h
BarcodeMenuButton.h
BarcodeMenuItem.h
+1
View File
@@ -28,6 +28,7 @@ namespace glabels
namespace Config
{
const QString PROJECT_SOURCE_DIR = "@glabels_SOURCE_DIR@";
const QString PROJECT_BUILD_DIR = "@glabels_BINARY_DIR@";
}
}
+5 -28
View File
@@ -21,12 +21,12 @@
#include "Db.h"
#include <QApplication>
#include <QtDebug>
#include <QtGlobal>
#include "Config.h"
#include "StrUtil.h"
#include "FileUtil.h"
#include "XmlCategoryParser.h"
#include "XmlPaperParser.h"
#include "XmlTemplateParser.h"
@@ -603,32 +603,9 @@ namespace glabels
}
QDir Db::systemTemplatesDir()
{
QDir dir;
// First, try finding templates directory relative to application path
dir.cd( QApplication::applicationDirPath() );
if ( (dir.dirName() == "bin") &&
dir.cdUp() && dir.cd( "share" ) && dir.cd( "glabels-qt" ) && dir.cd( "templates" ) )
{
return dir;
}
// Next, try running out of the source directory.
if ( dir.cd( Config::PROJECT_SOURCE_DIR ) && dir.cd( "templates" ) )
{
return dir;
}
qFatal( "Cannot find template directory!" );
return QDir("/");
}
void Db::readPapers()
{
readPapersFromDir( systemTemplatesDir() );
readPapersFromDir( FileUtil::systemTemplatesDir() );
}
@@ -648,7 +625,7 @@ namespace glabels
void Db::readCategories()
{
readCategoriesFromDir( systemTemplatesDir() );
readCategoriesFromDir( FileUtil::systemTemplatesDir() );
}
@@ -668,7 +645,7 @@ namespace glabels
void Db::readVendors()
{
readVendorsFromDir( systemTemplatesDir() );
readVendorsFromDir( FileUtil::systemTemplatesDir() );
}
@@ -688,7 +665,7 @@ namespace glabels
void Db::readTemplates()
{
readTemplatesFromDir( systemTemplatesDir() );
readTemplatesFromDir( FileUtil::systemTemplatesDir() );
// TODO: Read user directories
+54 -9
View File
@@ -20,23 +20,68 @@
#include "FileUtil.h"
#include <QApplication>
#include "Config.h"
namespace glabels
{
namespace FileUtil
QString FileUtil::addExtension( const QString& rawFilename, const QString& extension )
{
QString addExtension( const QString& rawFilename, const QString& extension )
if ( rawFilename.endsWith( extension ) )
{
if ( rawFilename.endsWith( extension ) )
{
return rawFilename;
}
return rawFilename + extension;
return rawFilename;
}
return rawFilename + extension;
}
QDir FileUtil::systemTemplatesDir()
{
QDir dir;
// First, try finding templates directory relative to application path
dir.cd( QApplication::applicationDirPath() );
if ( (dir.dirName() == "bin") &&
dir.cdUp() && dir.cd( "share" ) && dir.cd( "glabels-qt" ) && dir.cd( "templates" ) )
{
return dir;
}
// Next, try running out of the source directory.
if ( dir.cd( Config::PROJECT_SOURCE_DIR ) && dir.cd( "templates" ) )
{
return dir;
}
qFatal( "Cannot locate system template directory!" );
return QDir("/");
}
QDir FileUtil::translationsDir()
{
QDir dir;
// First, try finding translations directory relative to application path
dir.cd( QApplication::applicationDirPath() );
if ( (dir.dirName() == "bin") &&
dir.cdUp() && dir.cd( "share" ) && dir.cd( "glabels-qt" ) && dir.cd( "translations" ) )
{
return dir;
}
// Next, try running out of the source directory.
if ( dir.cd( Config::PROJECT_BUILD_DIR ) && dir.cd( "translations" ) )
{
return dir;
}
qFatal( "Cannot locate system template directory!" );
return QDir("/");
}
}
+5
View File
@@ -23,6 +23,7 @@
#include <QString>
#include <QDir>
namespace glabels
@@ -33,6 +34,10 @@ namespace glabels
QString addExtension( const QString& rawFilename, const QString& extension );
QDir systemTemplatesDir();
QDir userTemplatesDir();
QDir translationsDir();
}
}
+2
View File
@@ -30,6 +30,8 @@ namespace glabels
class FrameCd : public Frame
{
Q_DECLARE_TR_FUNCTIONS(FrameCd)
public:
FrameCd( const Distance& r1,
const Distance& r2,
+1
View File
@@ -30,6 +30,7 @@ namespace glabels
class FrameEllipse : public Frame
{
Q_DECLARE_TR_FUNCTIONS(FrameEllipse)
public:
FrameEllipse( const Distance& w,
+2
View File
@@ -30,6 +30,8 @@ namespace glabels
class FrameRect : public Frame
{
Q_DECLARE_TR_FUNCTIONS(FrameRect)
public:
FrameRect( const Distance& w,
const Distance& h,
+1
View File
@@ -30,6 +30,7 @@ namespace glabels
class FrameRound : public Frame
{
Q_DECLARE_TR_FUNCTIONS(FrameRound)
public:
FrameRound( const Distance& r,
+2 -2
View File
@@ -22,6 +22,7 @@
#include <QTextStream>
#include <QCoreApplication>
#include <QtDebug>
@@ -192,7 +193,6 @@ namespace glabels
{
init();
// TODO: are translations done in a compatable way, so that we can use "_name" attributes?
QString i18nString = node.attribute( QString("_").append(name), "" );
if ( i18nString == "" )
@@ -200,7 +200,7 @@ namespace glabels
return node.attribute( name, default_value );
}
return i18nString;
return QCoreApplication::translate( "XmlStrings", i18nString.toUtf8().constData() );
}
+32 -8
View File
@@ -20,7 +20,11 @@
#include <QApplication>
#include <QLocale>
#include <QTranslator>
#include <QtDebug>
#include "FileUtil.h"
#include "Db.h"
#include "MainWindow.h"
#include "Settings.h"
@@ -36,19 +40,39 @@ int main( int argc, char **argv )
QCoreApplication::setOrganizationDomain( "glabels.org" );
QCoreApplication::setApplicationName( "glabels-qt" );
//
// Setup translators
//
QLocale locale = QLocale::system();
QString translationsDir = glabels::FileUtil::translationsDir().canonicalPath();
QTranslator glabelsTranslator;
if ( glabelsTranslator.load( locale, "glabels", "_", translationsDir ) )
{
app.installTranslator(&glabelsTranslator);
}
QTranslator templatesTranslator;
if ( templatesTranslator.load( locale, "templates", "_", translationsDir ) )
{
app.installTranslator(&templatesTranslator);
}
//
// Initialize subsystems
//
glabels::Settings::init();
glabels::Db::init();
glabels::merge::Factory::init();
////// TEMPORARY TESTING ////////
#if 0
glabels::Db::printKnownPapers();
glabels::Db::printKnownCategories();
glabels::Db::printKnownVendors();
glabels::Db::printKnownTemplates();
#endif
/////////////////////////////////
/// @TODO open file(s) from command line if present, otherwise start wizard
//
// Launch main window
//
glabels::MainWindow mainWindow;
mainWindow.show();
+4
View File
@@ -49,6 +49,10 @@ set (other_db_files
vendors.xml
)
# Export variables to parent scope
set (template_files ${template_files} PARENT_SCOPE)
set (other_db_files ${other_db_files} PARENT_SCOPE)
#=======================================
# Install
#=======================================
+97
View File
@@ -0,0 +1,97 @@
#=======================================
# Translation files
#=======================================
# without this a "make clean" would delete the .ts files
set_directory_properties (PROPERTIES CLEAN_NO_CUSTOM 1)
#
# gLabels Translation Files
#
set (glabels_ts_files
glabels_C.ts
)
#
# Template Translation Files
#
set (templates_ts_files
templates_C.ts
)
qt5_create_translation (glabels_qm_files
${CMAKE_SOURCE_DIR}/glabels ${glabels_ts_files}
)
qt5_create_translation (templates_qm_files
${CMAKE_CURRENT_BINARY_DIR}/template-strings.h ${templates_ts_files}
OPTIONS -locations none
)
add_custom_target (update_translations DEPENDS ${glabels_qm_files} ${templates_qm_files})
# Add updating translations as a dependency for glabels-qt
add_dependencies (glabels-qt update_translations)
#=======================================
# Compilation
#=======================================
add_compile_options (-std=c++11 -g)
if (NOT WIN32)
add_compile_options (-fPIC)
endif ()
#=======================================
# XmlStrings utility
#=======================================
set (XmlStrings_sources
XmlStrings.cpp
)
add_executable (XmlStrings WIN32
${XmlStrings_sources}
)
target_link_libraries (XmlStrings
${Qt5Xml_LIBRARIES}
${Qt5Svg_LIBRARIES}
)
include_directories (
${Qt5Xml_INCLUDE_DIRS}
${Qt5Svg_INCLUDE_DIRS}
)
#=======================================
# Extract translatable strings from XML
# template files.
#=======================================
# Use absolute locations of XML files
string (REGEX REPLACE "([^;]+)" "${CMAKE_SOURCE_DIR}/templates/\\1"
xml_files "${template_files};${other_db_files}"
)
add_custom_command (
OUTPUT template-strings.h
COMMAND XmlStrings ${xml_files} > template-strings.h
COMMENT "Extracting template strings."
DEPENDS XmlStrings ${xml_files}
)
set_source_files_properties (template-strings.h PROPERTIES GENERATED TRUE)
add_custom_target (template-strings DEPENDS template-strings.h)
#=======================================
# Subdirectories
#=======================================
#=======================================
# Install
#=======================================
install (FILES ${glabels_qm_files} DESTINATION share/glabels-qt/translations)
install (FILES ${templates_qm_files} DESTINATION share/glabels-qt/translations)
+108
View File
@@ -0,0 +1,108 @@
/* XmlStrings.cpp
*
* Copyright (C) 2013-2016 Jim Evins <evins@snaught.com>
*
* This file is part of gLabels-qt.
*
* gLabels-qt is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* gLabels-qt is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with gLabels-qt. If not, see <http://www.gnu.org/licenses/>.
*/
#include <QApplication>
#include <QStringList>
#include <QFile>
#include <QDomDocument>
#include <QDomNode>
#include <QDomAttr>
#include <QTextStream>
#include <QDebug>
namespace
{
QStringList stringList;
}
void parseElement( const QDomElement& node )
{
// Examine each attribute for translatable strings
QDomNamedNodeMap attrNodes = node.attributes();
for ( int i = 0; i < attrNodes.count(); i++ )
{
QDomAttr attr = attrNodes.item(i).toAttr();
if ( attr.name().at(0) == '_' )
{
if ( !stringList.contains( attr.value() ) )
{
stringList.append( attr.value() );
}
}
}
// Recurse over children
for ( QDomNode child = node.firstChild(); !child.isNull(); child = child.nextSibling() )
{
parseElement( child.toElement() );
}
}
void parseFile( const QString& filename )
{
QFile file( filename );
if ( file.open( QFile::ReadOnly|QFile::Text ) )
{
QDomDocument doc;
if ( doc.setContent( &file, false ) )
{
QDomElement root = doc.documentElement();
parseElement( root );
}
}
}
int main( int argc, char *argv[] )
{
QCoreApplication app( argc, argv );
QStringList filenameList = app.arguments();
filenameList.removeFirst(); // Remove 0th argument, which is the command name
foreach ( QString filename, filenameList )
{
parseFile( filename );
}
stringList.sort();
QTextStream out( stdout );
out << "// Automatically generated with " << app.arguments().at(0) << endl;
out << "//" << endl;
out << "// Sources:" << endl;
foreach ( QString filename, filenameList )
{
out << "// " << filename << endl;
}
out << "//" << endl;
foreach ( QString string, stringList )
{
out << "QT_TRANSLATE_NOOP( \"XmlStrings\", \"" << string << "\" );" << endl;
}
}
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff