Skip to content

Commit 04cdc9b

Browse files
authoredAug 24, 2018
Merge pull request #7694 from signedav/trans_extends
Improvements of translation-featurs of .qgs project data
2 parents 3b29e59 + 5ef0799 commit 04cdc9b

File tree

6 files changed

+40
-12
lines changed

6 files changed

+40
-12
lines changed
 

‎src/app/qgsprojectproperties.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2218,4 +2218,10 @@ void QgsProjectProperties::onGenerateTsFileButton() const
22182218
{
22192219
QString l = cbtsLocale->currentData().toString();
22202220
QgsProject::instance()->generateTsFile( l );
2221+
QMessageBox::information( nullptr, tr( "General TS file generated" ), tr( "TS file generated with source language %1.\n"
2222+
"- open it with Qt Linguist\n"
2223+
"- translate strings\n"
2224+
"- save it with the postfix of the target language (eg. de)\n"
2225+
"- release to get qm file including postfix (eg. aproject_de.qm)\n"
2226+
"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 ) ) ;
22212227
}

‎src/core/qgsproject.cpp

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -479,10 +479,18 @@ void QgsProject::registerTranslatableObjects( QgsTranslationContext *translation
479479
const QgsFields fields = vlayer->fields();
480480
for ( const QgsField &field : fields )
481481
{
482+
QString fieldName;
482483
if ( field.alias().isEmpty() )
483-
translationContext->registerTranslation( QStringLiteral( "project:layers:%1:fieldaliases" ).arg( vlayer->id() ), field.name() );
484+
fieldName = field.name();
484485
else
485-
translationContext->registerTranslation( QStringLiteral( "project:layers:%1:fieldaliases" ).arg( vlayer->id() ), field.alias() );
486+
fieldName = field.alias();
487+
488+
translationContext->registerTranslation( QStringLiteral( "project:layers:%1:fieldaliases" ).arg( vlayer->id() ), fieldName );
489+
490+
if ( field.editorWidgetSetup().type() == QStringLiteral( "ValueRelation" ) )
491+
{
492+
translationContext->registerTranslation( QStringLiteral( "project:layers:%1:fields:%2:valuerelationvalue" ).arg( vlayer->id(), field.name() ), field.editorWidgetSetup().config().value( QStringLiteral( "Value" ) ).toString() );
493+
}
486494
}
487495

488496
//register formcontainers
@@ -2820,7 +2828,7 @@ void QgsProject::generateTsFile( const QString &locale )
28202828
{
28212829
QgsTranslationContext translationContext;
28222830
translationContext.setProject( this );
2823-
translationContext.setFileName( QStringLiteral( "%1/%2_%3.ts" ).arg( absolutePath(), baseName(), locale ) );
2831+
translationContext.setFileName( QStringLiteral( "%1/%2.ts" ).arg( absolutePath(), baseName() ) );
28242832

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

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

2837-
return mTranslator->translate( context.toUtf8(), sourceText.toUtf8(), disambiguation, n );
2845+
QString result = mTranslator->translate( context.toUtf8(), sourceText.toUtf8(), disambiguation, n );
2846+
2847+
if ( result.isEmpty() )
2848+
{
2849+
return sourceText;
2850+
}
2851+
return result;
28382852
}

‎src/core/qgstranslationcontext.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,11 @@ void QgsTranslationContext::registerTranslation( const QString &context, const Q
5252

5353
void QgsTranslationContext::writeTsFile( const QString &locale )
5454
{
55-
QgsSettings settings;
56-
5755
//write xml
5856
QDomDocument doc( QStringLiteral( "TS" ) );
5957

6058
QDomElement tsElement = doc.createElement( QStringLiteral( "TS" ) );
61-
tsElement.setAttribute( QStringLiteral( "language" ), locale );
62-
tsElement.setAttribute( QStringLiteral( "sourcelanguage" ), settings.value( QStringLiteral( "locale/userLocale" ), "" ).toString() );
59+
tsElement.setAttribute( QStringLiteral( "sourcelanguage" ), locale );
6360
doc.appendChild( tsElement );
6461

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

8380
QDomElement translationElement = doc.createElement( QStringLiteral( "translation" ) );
84-
QDomText translationText = doc.createTextNode( translatableObject.source );
85-
translationElement.appendChild( translationText );
81+
translationElement.setAttribute( QStringLiteral( "type" ), QStringLiteral( "unfinished" ) );
8682
messageElement.appendChild( translationElement );
8783
}
8884

‎src/core/qgsvectorlayer.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2035,6 +2035,10 @@ bool QgsVectorLayer::readSymbology( const QDomNode &layerNode, QString &errorMes
20352035
const QDomElement cfgElem = fieldConfigElement.elementsByTagName( QStringLiteral( "config" ) ).at( 0 ).toElement();
20362036
const QDomElement optionsElem = cfgElem.childNodes().at( 0 ).toElement();
20372037
QVariantMap optionsMap = QgsXmlUtils::readVariant( optionsElem ).toMap();
2038+
if ( widgetType == QStringLiteral( "ValueRelation" ) )
2039+
{
2040+
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() );
2041+
}
20382042
QgsEditorWidgetSetup setup = QgsEditorWidgetSetup( widgetType, optionsMap );
20392043
mFieldWidgetSetups[fieldName] = setup;
20402044
}

‎src/ui/qgsprojectpropertiesbase.ui

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -806,6 +806,13 @@
806806
<string>Generate Project Translation File</string>
807807
</property>
808808
<layout class="QHBoxLayout" name="horizontalLayout_4">
809+
<item>
810+
<widget class="QLabel" name="sourceLanguageLabel">
811+
<property name="text">
812+
<string>Source language</string>
813+
</property>
814+
</widget>
815+
</item>
809816
<item>
810817
<widget class="QComboBox" name="cbtsLocale"/>
811818
</item>
@@ -2950,6 +2957,7 @@
29502957
<include location="../../images/images.qrc"/>
29512958
<include location="../../images/images.qrc"/>
29522959
<include location="../../images/images.qrc"/>
2960+
<include location="../../images/images.qrc"/>
29532961
</resources>
29542962
<connections>
29552963
<connection>

‎tests/src/core/testqgstranslateproject.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ void TestQgsTranslateProject::cleanupTestCase()
7171

7272
//delete created ts file
7373
QString tsFileName( TEST_DATA_DIR );
74-
tsFileName = tsFileName + "/project_translation/points_translation_de.ts";
74+
tsFileName = tsFileName + "/project_translation/points_translation.ts";
7575
QFile tsFile( tsFileName );
7676
tsFile.remove();
7777
}
@@ -99,7 +99,7 @@ void TestQgsTranslateProject::createTsFile()
9999

100100
//check if ts file is created
101101
QString tsFileName( TEST_DATA_DIR );
102-
tsFileName = tsFileName + "/project_translation/points_translation_de.ts";
102+
tsFileName = tsFileName + "/project_translation/points_translation.ts";
103103
QFile tsFile( tsFileName );
104104
QVERIFY( tsFile.exists() );
105105

0 commit comments

Comments
 (0)