From b922d88b0f373c53598f0459586397fe306138f8 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Thu, 10 Aug 2017 10:43:49 -0400 Subject: [PATCH] Python-i18n: add a gettext_path for python job modules --- CMakeModules/CalamaresAddTranslations.cmake | 5 ++- src/libcalamares/GlobalStorage.cpp | 26 ++++++++++++++ src/libcalamares/GlobalStorage.h | 5 +++ src/libcalamares/PythonJob.cpp | 39 ++++++++++++++++----- src/libcalamares/PythonJobApi.cpp | 12 ++++++- src/libcalamares/PythonJobApi.h | 6 ++-- 6 files changed, 80 insertions(+), 13 deletions(-) diff --git a/CMakeModules/CalamaresAddTranslations.cmake b/CMakeModules/CalamaresAddTranslations.cmake index 97d4fadf1..cddab6b5e 100644 --- a/CMakeModules/CalamaresAddTranslations.cmake +++ b/CMakeModules/CalamaresAddTranslations.cmake @@ -57,10 +57,13 @@ macro(add_calamares_python_translations language) if( lang STREQUAL "en" ) message( STATUS "Skipping Python translations for en_US" ) else() - list( APPEND TS_FILES "${CMAKE_SOURCE_DIR}/lang/python_${lang}.po;${CMAKE_SOURCE_DIR}/lang/python_${lang}.mo" ) + list( APPEND TS_FILES "${CMAKE_SOURCE_DIR}/lang/python/${lang}/LC_MESSAGES/python.po;${CMAKE_SOURCE_DIR}/lang/python/${lang}/LC_MESSAGES/python.mo" ) endif() endforeach() + file( MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/_lang ) + file( COPY ${CMAKE_SOURCE_DIR}/lang/python DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/_lang ) + install( FILES ${TS_FILES} DESTINATION ${CMAKE_INSTALL_DATADIR}/calamares/lang/ diff --git a/src/libcalamares/GlobalStorage.cpp b/src/libcalamares/GlobalStorage.cpp index c872482be..32ae02191 100644 --- a/src/libcalamares/GlobalStorage.cpp +++ b/src/libcalamares/GlobalStorage.cpp @@ -125,6 +125,32 @@ GlobalStoragePythonWrapper::insert( const std::string& key, CalamaresPython::variantFromPyObject( value ) ); } +bp::list +GlobalStoragePythonWrapper::gettext_languages() const +{ + bp::list pyList; + QVariant localeConf_ = m_gs->value( "localeConf" ); + if ( localeConf_.canConvert< QVariantMap >() ) + { + QVariant lang_ = localeConf_.value< QVariantMap >()[ "LANG" ]; + if ( lang_.canConvert< QString >() ) + { + QString lang = lang_.value< QString >(); + pyList.append( lang.toStdString() ); + if ( lang.indexOf( '.' ) > 0) + { + lang.truncate( lang.indexOf( '.' ) ); + pyList.append( lang.toStdString() ); + } + if ( lang.indexOf( '_' ) > 0) + { + lang.truncate( lang.indexOf( '_' ) ); + pyList.append( lang.toStdString() ); + } + } + } + return pyList; +} bp::list GlobalStoragePythonWrapper::keys() const diff --git a/src/libcalamares/GlobalStorage.h b/src/libcalamares/GlobalStorage.h index 012a516a1..92def182e 100644 --- a/src/libcalamares/GlobalStorage.h +++ b/src/libcalamares/GlobalStorage.h @@ -86,6 +86,11 @@ public: boost::python::list keys() const; int remove( const std::string& key ); boost::python::api::object value( const std::string& key ) const; + + // Special case to simplify Python code, gets localeConf["LANG"] + // from the global store, in list form with language variants + // expanded (e.g [ "en_GB.UTF-8", "en_GB", "en" ] ). + boost::python::list gettext_languages() const; private: Calamares::GlobalStorage* m_gs; }; diff --git a/src/libcalamares/PythonJob.cpp b/src/libcalamares/PythonJob.cpp index 1955b64fc..f6ae92384 100644 --- a/src/libcalamares/PythonJob.cpp +++ b/src/libcalamares/PythonJob.cpp @@ -70,6 +70,7 @@ BOOST_PYTHON_MODULE( libcalamares ) .def_readonly( "module_name", &CalamaresPython::PythonJobInterface::moduleName ) .def_readonly( "pretty_name", &CalamaresPython::PythonJobInterface::prettyName ) .def_readonly( "working_path", &CalamaresPython::PythonJobInterface::workingPath ) + .def_readonly( "gettext_path", &CalamaresPython::PythonJobInterface::gettextPath ) .def_readonly( "configuration", &CalamaresPython::PythonJobInterface::configuration ) .def( "setprogress", @@ -85,7 +86,8 @@ BOOST_PYTHON_MODULE( libcalamares ) .def( "insert", &CalamaresPython::GlobalStoragePythonWrapper::insert ) .def( "keys", &CalamaresPython::GlobalStoragePythonWrapper::keys ) .def( "remove", &CalamaresPython::GlobalStoragePythonWrapper::remove ) - .def( "value", &CalamaresPython::GlobalStoragePythonWrapper::value ); + .def( "value", &CalamaresPython::GlobalStoragePythonWrapper::value ) + .def( "gettext_languages", &CalamaresPython::GlobalStoragePythonWrapper::gettext_languages ); // libcalamares.utils submodule starts here bp::object utilsModule( bp::handle<>( bp::borrowed( PyImport_AddModule( "libcalamares.utils" ) ) ) ); @@ -297,16 +299,35 @@ PythonJob::exec() scriptNamespace ); bp::object entryPoint = scriptNamespace[ "run" ]; - bp::extract< std::string > entryPoint_doc_attr(entryPoint.attr( "__doc__" ) ); + bp::object prettyNameFunc = scriptNamespace[ "pretty_name" ]; - if ( entryPoint_doc_attr.check() ) + if ( !prettyNameFunc.is_none() ) { - m_description = QString::fromStdString( entryPoint_doc_attr() ).trimmed(); - auto i_newline = m_description.indexOf('\n'); - if ( i_newline > 0 ) - m_description.truncate( i_newline ); - cDebug() << "Job" << prettyName() << "->" << m_description; - emit progress( 0 ); + bp::extract< std::string > prettyNameResult( prettyNameFunc() ); + if ( prettyNameResult.check() ) + { + m_description = QString::fromStdString( prettyNameResult() ).trimmed(); + } + if ( !m_description.isEmpty() ) + { + cDebug() << "Job" << prettyName() << "-pretty_name->" << m_description; + emit progress( 0 ); + } + } + + if ( m_description.isEmpty() ) + { + bp::extract< std::string > entryPoint_doc_attr(entryPoint.attr( "__doc__" ) ); + + if ( entryPoint_doc_attr.check() ) + { + m_description = QString::fromStdString( entryPoint_doc_attr() ).trimmed(); + auto i_newline = m_description.indexOf('\n'); + if ( i_newline > 0 ) + m_description.truncate( i_newline ); + cDebug() << "Job" << prettyName() << "->" << m_description; + emit progress( 0 ); + } } bp::object runResult = entryPoint(); diff --git a/src/libcalamares/PythonJobApi.cpp b/src/libcalamares/PythonJobApi.cpp index 9e2161c92..10b66b39f 100644 --- a/src/libcalamares/PythonJobApi.cpp +++ b/src/libcalamares/PythonJobApi.cpp @@ -1,6 +1,7 @@ /* === This file is part of Calamares - === * * Copyright 2014-2016, Teo Mrnjavac + * Copyright 2017, Adriaan de Groot * * Calamares is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -177,10 +178,19 @@ debug( const std::string& s ) PythonJobInterface::PythonJobInterface( Calamares::PythonJob* parent ) : m_parent( parent ) { - moduleName = QDir( m_parent->m_workingPath ).dirName().toStdString(); + auto moduleDir = QDir( m_parent->m_workingPath ); + moduleName = moduleDir.dirName().toStdString(); prettyName = m_parent->prettyName().toStdString(); workingPath = m_parent->m_workingPath.toStdString(); configuration = CalamaresPython::variantMapToPyDict( m_parent->m_configurationMap ); + + if (moduleDir.cd("../_lang/python")) + gettextPath = moduleDir.absolutePath().toStdString(); + else + { + debug( "No _lang/ directory for translations." ); + gettextPath = std::string(); + } } diff --git a/src/libcalamares/PythonJobApi.h b/src/libcalamares/PythonJobApi.h index 185b46569..a3443ad76 100644 --- a/src/libcalamares/PythonJobApi.h +++ b/src/libcalamares/PythonJobApi.h @@ -1,6 +1,7 @@ /* === This file is part of Calamares - === * * Copyright 2014-2016, Teo Mrnjavac + * Copyright 2017, Adriaan de Groot * * Calamares is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -60,10 +61,10 @@ std::string check_target_env_output( const boost::python::list& args, std::string obscure( const std::string& string ); -inline int _handle_check_target_env_call_error( int ec, const QString& cmd ); - void debug( const std::string& s ); +inline int _handle_check_target_env_call_error( int ec, const QString& cmd ); + class PythonJobInterface { public: @@ -72,6 +73,7 @@ public: std::string moduleName; std::string prettyName; std::string workingPath; + std::string gettextPath; boost::python::dict configuration;