From 9ac87132aff8699fa3e09f259b284c05117c9a78 Mon Sep 17 00:00:00 2001 From: jean-pierre charras Date: Thu, 12 May 2022 13:05:06 +0200 Subject: [PATCH] Fix a few issues related to field names and their translation. - Do not use translated field names outside strings displayed in dialogs. - fix code that does not work well with default locale. - fix some (not all) I18n issues in DIALOG_UPDATE_SYMBOL_FIELDS. (cherry picked from commit 20d1d0705e22429e40314a4219342238e133c035) --- common/template_fieldnames.cpp | 49 ++++--------------- eeschema/dialogs/dialog_change_symbols.cpp | 2 +- .../dialogs/dialog_symbol_fields_table.cpp | 4 ++ eeschema/dialogs/dialog_symbol_properties.cpp | 2 +- .../dialogs/dialog_update_symbol_fields.cpp | 2 +- eeschema/fields_grid_table.cpp | 2 +- eeschema/lib_field.cpp | 9 ++-- eeschema/lib_field.h | 2 +- eeschema/sch_field.h | 2 +- eeschema/sch_sheet.cpp | 21 ++------ include/template_fieldnames.h | 10 ++-- 11 files changed, 34 insertions(+), 71 deletions(-) diff --git a/common/template_fieldnames.cpp b/common/template_fieldnames.cpp index 5c7c59bf1d..c4cee8eaa9 100644 --- a/common/template_fieldnames.cpp +++ b/common/template_fieldnames.cpp @@ -2,7 +2,7 @@ * This program source code file is part of KiCad, a free EDA CAD application. * * Copyright (C) 2010 SoftPLC Corporation, Dick Hollenbeck - * Copyright (C) 2015-2020 KiCad Developers, see AUTHORS.TXT for contributors. + * Copyright (C) 2015-2022 KiCad Developers, see AUTHORS.TXT for contributors. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -35,18 +35,9 @@ using namespace TFIELD_T; #define FOOTPRINT_CANONICAL "Footprint" #define DATASHEET_CANONICAL "Datasheet" -static std::mutex s_defaultFieldMutex; - -const wxString TEMPLATE_FIELDNAME::GetDefaultFieldName( int aFieldNdx, bool aTranslate ) +const wxString TEMPLATE_FIELDNAME::GetDefaultFieldName( int aFieldNdx, bool aTranslateForHI ) { - static void* locale = nullptr; - static wxString referenceDefault; - static wxString valueDefault; - static wxString footprintDefault; - static wxString datasheetDefault; - static wxString fieldDefault; - - if( !aTranslate ) + if( !aTranslateForHI ) { switch( aFieldNdx ) { @@ -54,42 +45,20 @@ const wxString TEMPLATE_FIELDNAME::GetDefaultFieldName( int aFieldNdx, bool aTra case VALUE_FIELD: return VALUE_CANONICAL; // The symbol value case FOOTPRINT_FIELD: return FOOTPRINT_CANONICAL; // The footprint for use with Pcbnew case DATASHEET_FIELD: return DATASHEET_CANONICAL; // Link to a datasheet for symbol + default: return wxString::Format( wxT( "Field%d" ), aFieldNdx ); } } - // Mutex protection is needed so that multiple loader threads don't write to the static - // variables at once - std::lock_guard lock( s_defaultFieldMutex ); - - // Fetching translations can take a surprising amount of time when loading libraries, - // so only do it when necessary. - if( Pgm().GetLocale() != locale ) - { - referenceDefault = _( REFERENCE_CANONICAL ); - valueDefault = _( VALUE_CANONICAL ); - footprintDefault = _( FOOTPRINT_CANONICAL ); - datasheetDefault = _( DATASHEET_CANONICAL ); - fieldDefault = _( "Field%d" ); - locale = Pgm().GetLocale(); - } - - // Fixed values for the mandatory fields switch( aFieldNdx ) { - case REFERENCE_FIELD: return referenceDefault; // The symbol reference, R1, C1, etc. - case VALUE_FIELD: return valueDefault; // The symbol value - case FOOTPRINT_FIELD: return footprintDefault; // The footprint for use with Pcbnew - case DATASHEET_FIELD: return datasheetDefault; // Link to a datasheet for symbol - default: return wxString::Format( fieldDefault, aFieldNdx ); + case REFERENCE_FIELD: return _( REFERENCE_CANONICAL ); // The symbol reference, R1, C1, etc. + case VALUE_FIELD: return _( VALUE_CANONICAL ); // The symbol value + case FOOTPRINT_FIELD: return _( FOOTPRINT_CANONICAL ); // The footprint for use with Pcbnew + case DATASHEET_FIELD: return _( DATASHEET_CANONICAL ); // Link to a datasheet for symbol + default: return wxString::Format( _( "Field%d" ), aFieldNdx ); } - } -#undef REFERENCE_CANONICAL -#undef VALUE_CANONICAL -#undef FOOTPRINT_CANONICAL -#undef DATASHEET_CANONICAL - void TEMPLATE_FIELDNAME::Format( OUTPUTFORMATTER* out, int nestLevel ) const { diff --git a/eeschema/dialogs/dialog_change_symbols.cpp b/eeschema/dialogs/dialog_change_symbols.cpp index 2b13002fe4..b845f4ce66 100644 --- a/eeschema/dialogs/dialog_change_symbols.cpp +++ b/eeschema/dialogs/dialog_change_symbols.cpp @@ -100,7 +100,7 @@ DIALOG_CHANGE_SYMBOLS::DIALOG_CHANGE_SYMBOLS( SCH_EDIT_FRAME* aParent, SCH_SYMBO for( int i = 0; i < MANDATORY_FIELDS; ++i ) { - m_fieldsBox->Append( TEMPLATE_FIELDNAME::GetDefaultFieldName( i ) ); + m_fieldsBox->Append( TEMPLATE_FIELDNAME::GetDefaultFieldName( i, DO_TRANSLATE ) ); if( i == REFERENCE_FIELD ) m_fieldsBox->Check( i, g_selectRefDes ); diff --git a/eeschema/dialogs/dialog_symbol_fields_table.cpp b/eeschema/dialogs/dialog_symbol_fields_table.cpp index 3a7d1dae41..d0784cc19d 100644 --- a/eeschema/dialogs/dialog_symbol_fields_table.cpp +++ b/eeschema/dialogs/dialog_symbol_fields_table.cpp @@ -233,6 +233,10 @@ public: if( aCol == QUANTITY_COLUMN ) return _( "Qty" ); else if( aCol < MANDATORY_FIELDS ) + // FIX ME: the column label should be displayed translated. + // but when translated, and the DATASHEET column is shown, a new field + // with the translated DATASHEET field name is added when saving fields + // return TEMPLATE_FIELDNAME::GetDefaultFieldName( aCol DO_TRANSLATE ); return TEMPLATE_FIELDNAME::GetDefaultFieldName( aCol ); else return m_fieldNames[ aCol ]; diff --git a/eeschema/dialogs/dialog_symbol_properties.cpp b/eeschema/dialogs/dialog_symbol_properties.cpp index 7f548e5936..aa36b46c8c 100644 --- a/eeschema/dialogs/dialog_symbol_properties.cpp +++ b/eeschema/dialogs/dialog_symbol_properties.cpp @@ -867,7 +867,7 @@ void DIALOG_SYMBOL_PROPERTIES::OnAddField( wxCommandEvent& event ) SCHEMATIC_SETTINGS& settings = m_symbol->Schematic()->Settings(); int fieldID = m_fields->size(); SCH_FIELD newField( wxPoint( 0, 0 ), fieldID, m_symbol, - TEMPLATE_FIELDNAME::GetDefaultFieldName( fieldID ) ); + TEMPLATE_FIELDNAME::GetDefaultFieldName( fieldID, DO_TRANSLATE ) ); newField.SetTextAngle( m_fields->at( REFERENCE_FIELD ).GetTextAngle() ); newField.SetTextSize( wxSize( settings.m_DefaultTextSize, settings.m_DefaultTextSize ) ); diff --git a/eeschema/dialogs/dialog_update_symbol_fields.cpp b/eeschema/dialogs/dialog_update_symbol_fields.cpp index 853c1f6e20..e041620053 100644 --- a/eeschema/dialogs/dialog_update_symbol_fields.cpp +++ b/eeschema/dialogs/dialog_update_symbol_fields.cpp @@ -47,7 +47,7 @@ DIALOG_UPDATE_SYMBOL_FIELDS::DIALOG_UPDATE_SYMBOL_FIELDS( SYMBOL_EDIT_FRAME* aPa for( int i = 0; i < MANDATORY_FIELDS; ++i ) { - m_fieldsBox->Append( TEMPLATE_FIELDNAME::GetDefaultFieldName( i ) ); + m_fieldsBox->Append( TEMPLATE_FIELDNAME::GetDefaultFieldName( i, DO_TRANSLATE ) ); m_fieldsBox->Check( i, true ); } diff --git a/eeschema/fields_grid_table.cpp b/eeschema/fields_grid_table.cpp index 86a1525987..9cf22d7228 100644 --- a/eeschema/fields_grid_table.cpp +++ b/eeschema/fields_grid_table.cpp @@ -389,7 +389,7 @@ wxString FIELDS_GRID_TABLE::GetValue( int aRow, int aCol ) if( aRow < m_mandatoryFieldCount ) { if( m_parentType == SCH_SYMBOL_T ) - return TEMPLATE_FIELDNAME::GetDefaultFieldName( aRow ); + return TEMPLATE_FIELDNAME::GetDefaultFieldName( aRow, DO_TRANSLATE ); else if( m_parentType == SCH_SHEET_T ) return SCH_SHEET::GetDefaultFieldName( aRow ); } diff --git a/eeschema/lib_field.cpp b/eeschema/lib_field.cpp index 3205b0ea8e..3c0dcfa60a 100644 --- a/eeschema/lib_field.cpp +++ b/eeschema/lib_field.cpp @@ -403,10 +403,11 @@ wxString LIB_FIELD::GetCanonicalName() const { switch( m_id ) { - case REFERENCE_FIELD: return wxT( "Reference" ); - case VALUE_FIELD: return wxT( "Value" ); - case FOOTPRINT_FIELD: return wxT( "Footprint" ); - case DATASHEET_FIELD: return wxT( "Datasheet" ); + case REFERENCE_FIELD: + case VALUE_FIELD: + case FOOTPRINT_FIELD: + case DATASHEET_FIELD: + return TEMPLATE_FIELDNAME::GetDefaultFieldName( m_id ); } return m_name; diff --git a/eeschema/lib_field.h b/eeschema/lib_field.h index 58bd61179c..1bb29b8e6f 100644 --- a/eeschema/lib_field.h +++ b/eeschema/lib_field.h @@ -85,7 +85,7 @@ public: void Init( int aId ); /** - * Return the field name. + * Return the field name (not translated). * * The first four field IDs are reserved and therefore always return their respective * names. diff --git a/eeschema/sch_field.h b/eeschema/sch_field.h index 955ef2a11b..78e52464aa 100644 --- a/eeschema/sch_field.h +++ b/eeschema/sch_field.h @@ -94,7 +94,7 @@ public: void DoHypertextMenu( EDA_DRAW_FRAME* aFrame ) override; /** - * Return the field name. + * Return the field name (not translated).. * * @param aUseDefaultName When true return the default field name if the field name is * empty. Otherwise the default field name is returned. diff --git a/eeschema/sch_sheet.cpp b/eeschema/sch_sheet.cpp index 2879e48ca4..a76cf885a2 100644 --- a/eeschema/sch_sheet.cpp +++ b/eeschema/sch_sheet.cpp @@ -52,11 +52,6 @@ const wxString SCH_SHEET::GetDefaultFieldName( int aFieldNdx, bool aTranslated ) { - static void* locale = nullptr; - static wxString sheetnameDefault; - static wxString sheetfilenameDefault; - static wxString userFieldDefault; - if( !aTranslated ) { switch( aFieldNdx ) @@ -67,22 +62,12 @@ const wxString SCH_SHEET::GetDefaultFieldName( int aFieldNdx, bool aTranslated ) } } - // Fetching translations can take a surprising amount of time when loading libraries, - // so only do it when necessary. - if( Pgm().GetLocale() != locale ) - { - sheetnameDefault = _( SHEET_NAME_CANONICAL ); - sheetfilenameDefault = _( SHEET_FILE_CANONICAL ); - userFieldDefault = _( USER_FIELD_CANONICAL ); - locale = Pgm().GetLocale(); - } - // Fixed values for the mandatory fields switch( aFieldNdx ) { - case SHEETNAME: return sheetnameDefault; - case SHEETFILENAME: return sheetfilenameDefault; - default: return wxString::Format( userFieldDefault, aFieldNdx ); + case SHEETNAME: return _( SHEET_NAME_CANONICAL ); + case SHEETFILENAME: return _( SHEET_FILE_CANONICAL ); + default: return wxString::Format( _( USER_FIELD_CANONICAL ), aFieldNdx ); } } diff --git a/include/template_fieldnames.h b/include/template_fieldnames.h index efa16dd0ee..ea4d8171ba 100644 --- a/include/template_fieldnames.h +++ b/include/template_fieldnames.h @@ -2,7 +2,7 @@ * This program source code file is part of KiCad, a free EDA CAD application. * * Copyright (C) 2010 SoftPLC Corporation, Dick Hollenbeck - * Copyright (C) 2014-2020 KiCad Developers, see AUTHORS.TXT for contributors. + * Copyright (C) 2014-2022 KiCad Developers, see AUTHORS.TXT for contributors. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -50,6 +50,9 @@ enum MANDATORY_FIELD_T { MANDATORY_FIELDS }; +// A helper to call GetDefaultFieldName with or without translation. +// Translation should be used only to display field names in dialogs +#define DO_TRANSLATE true /** * Hold a name of a symbol's field, field value, and default visibility. @@ -103,9 +106,10 @@ struct TEMPLATE_FIELDNAME * These field names are not modifiable but template field names are. * * @param aFieldNdx The field number index, > 0. - * @param aTranslate If true, return the translated field name, else get the canonical name. + * @param aTranslateForHI If true, return the translated field name, + * else get the canonical name (defualt). Translation is intended only for dialogs */ - static const wxString GetDefaultFieldName( int aFieldNdx, bool aTranslate = true ); + static const wxString GetDefaultFieldName( int aFieldNdx, bool aTranslateForHI = false ); wxString m_Name; // The field name bool m_Visible; // Field defaults to being visible in schematic.