Skip to content

Commit

Permalink
Merge pull request #7694 from signedav/trans_extends
Browse files Browse the repository at this point in the history
Improvements of translation-featurs of .qgs project data
  • Loading branch information
m-kuhn committed Aug 24, 2018
2 parents 3b29e59 + 5ef0799 commit 04cdc9b
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 12 deletions.
6 changes: 6 additions & 0 deletions src/app/qgsprojectproperties.cpp
Expand Up @@ -2218,4 +2218,10 @@ void QgsProjectProperties::onGenerateTsFileButton() const
{
QString l = cbtsLocale->currentData().toString();
QgsProject::instance()->generateTsFile( l );
QMessageBox::information( nullptr, tr( "General TS file generated" ), tr( "TS file generated with source language %1.\n"
"- open it with Qt Linguist\n"
"- translate strings\n"
"- save it with the postfix of the target language (eg. de)\n"
"- release to get qm file including postfix (eg. aproject_de.qm)\n"
"When you open it again in QGIS having set the target language (de), the project will be translated and saved with postfix (eg. aproject_de.qgs)." ).arg( l ) ) ;
}
22 changes: 18 additions & 4 deletions src/core/qgsproject.cpp
Expand Up @@ -479,10 +479,18 @@ void QgsProject::registerTranslatableObjects( QgsTranslationContext *translation
const QgsFields fields = vlayer->fields();
for ( const QgsField &field : fields )
{
QString fieldName;
if ( field.alias().isEmpty() )
translationContext->registerTranslation( QStringLiteral( "project:layers:%1:fieldaliases" ).arg( vlayer->id() ), field.name() );
fieldName = field.name();
else
translationContext->registerTranslation( QStringLiteral( "project:layers:%1:fieldaliases" ).arg( vlayer->id() ), field.alias() );
fieldName = field.alias();

translationContext->registerTranslation( QStringLiteral( "project:layers:%1:fieldaliases" ).arg( vlayer->id() ), fieldName );

if ( field.editorWidgetSetup().type() == QStringLiteral( "ValueRelation" ) )
{
translationContext->registerTranslation( QStringLiteral( "project:layers:%1:fields:%2:valuerelationvalue" ).arg( vlayer->id(), field.name() ), field.editorWidgetSetup().config().value( QStringLiteral( "Value" ) ).toString() );
}
}

//register formcontainers
Expand Down Expand Up @@ -2820,7 +2828,7 @@ void QgsProject::generateTsFile( const QString &locale )
{
QgsTranslationContext translationContext;
translationContext.setProject( this );
translationContext.setFileName( QStringLiteral( "%1/%2_%3.ts" ).arg( absolutePath(), baseName(), locale ) );
translationContext.setFileName( QStringLiteral( "%1/%2.ts" ).arg( absolutePath(), baseName() ) );

emit QgsApplication::instance()->collectTranslatableObjects( &translationContext );

Expand All @@ -2834,5 +2842,11 @@ QString QgsProject::translate( const QString &context, const QString &sourceText
return sourceText;
}

return mTranslator->translate( context.toUtf8(), sourceText.toUtf8(), disambiguation, n );
QString result = mTranslator->translate( context.toUtf8(), sourceText.toUtf8(), disambiguation, n );

if ( result.isEmpty() )
{
return sourceText;
}
return result;
}
8 changes: 2 additions & 6 deletions src/core/qgstranslationcontext.cpp
Expand Up @@ -52,14 +52,11 @@ void QgsTranslationContext::registerTranslation( const QString &context, const Q

void QgsTranslationContext::writeTsFile( const QString &locale )
{
QgsSettings settings;

//write xml
QDomDocument doc( QStringLiteral( "TS" ) );

QDomElement tsElement = doc.createElement( QStringLiteral( "TS" ) );
tsElement.setAttribute( QStringLiteral( "language" ), locale );
tsElement.setAttribute( QStringLiteral( "sourcelanguage" ), settings.value( QStringLiteral( "locale/userLocale" ), "" ).toString() );
tsElement.setAttribute( QStringLiteral( "sourcelanguage" ), locale );
doc.appendChild( tsElement );

for ( const TranslatableObject &translatableObject : mTranslatableObjects )
Expand All @@ -81,8 +78,7 @@ void QgsTranslationContext::writeTsFile( const QString &locale )
messageElement.appendChild( sourceElement );

QDomElement translationElement = doc.createElement( QStringLiteral( "translation" ) );
QDomText translationText = doc.createTextNode( translatableObject.source );
translationElement.appendChild( translationText );
translationElement.setAttribute( QStringLiteral( "type" ), QStringLiteral( "unfinished" ) );
messageElement.appendChild( translationElement );
}

Expand Down
4 changes: 4 additions & 0 deletions src/core/qgsvectorlayer.cpp
Expand Up @@ -2035,6 +2035,10 @@ bool QgsVectorLayer::readSymbology( const QDomNode &layerNode, QString &errorMes
const QDomElement cfgElem = fieldConfigElement.elementsByTagName( QStringLiteral( "config" ) ).at( 0 ).toElement();
const QDomElement optionsElem = cfgElem.childNodes().at( 0 ).toElement();
QVariantMap optionsMap = QgsXmlUtils::readVariant( optionsElem ).toMap();
if ( widgetType == QStringLiteral( "ValueRelation" ) )
{
optionsMap[ QStringLiteral( "Value" ) ] = context.projectTranslator()->translate( QStringLiteral( "project:layers:%1:fields:%2:valuerelationvalue" ).arg( layerNode.namedItem( QStringLiteral( "id" ) ).toElement().text(), fieldName ), optionsMap[ QStringLiteral( "Value" ) ].toString() );
}
QgsEditorWidgetSetup setup = QgsEditorWidgetSetup( widgetType, optionsMap );
mFieldWidgetSetups[fieldName] = setup;
}
Expand Down
8 changes: 8 additions & 0 deletions src/ui/qgsprojectpropertiesbase.ui
Expand Up @@ -806,6 +806,13 @@
<string>Generate Project Translation File</string>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_4">
<item>
<widget class="QLabel" name="sourceLanguageLabel">
<property name="text">
<string>Source language</string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="cbtsLocale"/>
</item>
Expand Down Expand Up @@ -2950,6 +2957,7 @@
<include location="../../images/images.qrc"/>
<include location="../../images/images.qrc"/>
<include location="../../images/images.qrc"/>
<include location="../../images/images.qrc"/>
</resources>
<connections>
<connection>
Expand Down
4 changes: 2 additions & 2 deletions tests/src/core/testqgstranslateproject.cpp
Expand Up @@ -71,7 +71,7 @@ void TestQgsTranslateProject::cleanupTestCase()

//delete created ts file
QString tsFileName( TEST_DATA_DIR );
tsFileName = tsFileName + "/project_translation/points_translation_de.ts";
tsFileName = tsFileName + "/project_translation/points_translation.ts";
QFile tsFile( tsFileName );
tsFile.remove();
}
Expand Down Expand Up @@ -99,7 +99,7 @@ void TestQgsTranslateProject::createTsFile()

//check if ts file is created
QString tsFileName( TEST_DATA_DIR );
tsFileName = tsFileName + "/project_translation/points_translation_de.ts";
tsFileName = tsFileName + "/project_translation/points_translation.ts";
QFile tsFile( tsFileName );
QVERIFY( tsFile.exists() );

Expand Down

0 comments on commit 04cdc9b

Please sign in to comment.