def testAllDynamicTranslatons(self): """Test all the phrases defined in dynamic_translations translate.""" myParentPath = os.path.join(__file__, os.path.pardir, os.path.pardir) myDirPath = os.path.abspath(myParentPath) myFilePath = os.path.join(myDirPath, 'safe', 'common', 'dynamic_translations.py') myFile = file(myFilePath, 'rt') myFailureList = [] os.environ['LANG'] = 'id' myLineCount = 0 # exception_words is a list of words that has the same form in both # English and Indonesian. For example hotel, bank exception_words = ['hotel', 'bank'] for myLine in myFile.readlines(): myLineCount += 1 if 'tr(' in myLine: myMatch = re.search(r'\(\'(.*)\'\)', myLine, re.M | re.I) if myMatch: myGroup = myMatch.group() myCleanedLine = myGroup[2:-2] if myCleanedLine in exception_words: continue myTranslation = safeTr(myCleanedLine) print myTranslation, myCleanedLine if myCleanedLine == myTranslation: myFailureList.append(myCleanedLine) myMessage = ('Translations not found for:\n %s\n%i ' 'of %i untranslated\n' % (str(myFailureList).replace( ',', '\n'), len(myFailureList), myLineCount)) myMessage += ('If you think the Indonesian word for the failed ' 'translations is the same form in English, i.' 'e. "hotel", you can add it in exception_words in ' 'safe_qgis/test_safe_translations.py') assert len(myFailureList) == 0, myMessage
def _addFormItem(self, theParameterKey, theParameterValue): """Add a new form element dynamically from a key value pair. Args: * theParameterKey: str Mandatory string referencing the key in the function configurable parameters dictionary. * theParameterValue: object Mandatory representing the value referenced by the key. Returns: None Raises: None """ myLabel = QtGui.QLabel(self.formLayoutWidget) myLabel.setObjectName(_fromUtf8(theParameterKey + "Label")) myKey = theParameterKey myKey = myKey.replace('_', ' ') myKey = myKey.capitalize() myLabel.setText(safeTr(myKey)) myLabel.setToolTip(str(type(theParameterValue))) self.editableImpactFunctionsFormLayout.setWidget(self.formItemCounters, QtGui.QFormLayout.LabelRole, myLabel) myLineEdit = QtGui.QLineEdit(self.formLayoutWidget) myLineEdit.setText(str(theParameterValue)) myLineEdit.setObjectName(_fromUtf8(theParameterKey + 'LineEdit')) myLineEdit.setCursorPosition(0) self.editableImpactFunctionsFormLayout.setWidget(self.formItemCounters, QtGui.QFormLayout.FieldRole, myLineEdit) self.formItemCounters += 1
def testImpactSummaryWords(self): """Test specific words from impact summary info shown in doc see #348. """ os.environ['LANG'] = 'id' myPhraseList = [] myMessage = 'Specific words checked for translation:\n' for myPhrase in myPhraseList: if myPhrase == safeTr(myPhrase): myMessage += 'FAIL: %s' % myPhrase else: myMessage += 'PASS: %s' % myPhrase self.assertNotIn('FAIL', myMessage, myMessage)
def buildWidget(self, theFormLayout, theName, theValue): """Create a new form element dynamically based from theValue type. The element will be inserted to theFormLayout. Args: * theFormLayout: QFormLayout Mandatory a layout instance * theName: str Mandatory string referencing the key in the function configurable parameters dictionary. * theValue: object Mandatory representing the value referenced by the key. Returns: a function that return the value of widget Raises: None """ # create label if isinstance(theName, str): myLabel = QLabel() myLabel.setObjectName(_fromUtf8(theName + "Label")) myLabelText = theName.replace('_', ' ').capitalize() myLabel.setText(safeTr(myLabelText)) myLabel.setToolTip(str(type(theValue))) else: myLabel = theName # create widget based on the type of theValue variable if isinstance(theValue, list): myWidget = QLineEdit() myValue = ', '.join([str(x) for x in theValue]) # NOTE: we assume that all element in list have same type myType = type(theValue[0]) myFunc = lambda x: [myType(y) for y in str(x).split(',')] elif isinstance(theValue, dict): myWidget = QLineEdit() myValue = str(theValue) myFunc = lambda x: ast.literal_eval(str(x)) else: myWidget = QLineEdit() myValue = str(theValue) myFunc = type(theValue) myWidget.setText(myValue) theFormLayout.addRow(myLabel, myWidget) return self.bind(myWidget, 'text', myFunc)
def test_ImpactFunctionI18n(self): """Library translations are working.""" # Import this late so that i18n setup is already in place # Test indonesian too myParent = QWidget() myCanvas = QgsMapCanvas(myParent) myIface = QgisInterface(myCanvas) myPlugin = Plugin(myIface) myPlugin.setupI18n('id') # indonesian myExpectedString = 'Letusan gunung berapi' myTranslation = safeTr('A volcano eruption') myMessage = '\nTranslated: %s\nGot: %s\nExpected: %s' % \ ('A volcano eruption', myTranslation, myExpectedString) assert myTranslation == myExpectedString, myMessage
def testDynamicTranslation(self): """Test for dynamic translations for a string """ # English func_title = 'Be affected' expected_title = 'Be affected' assert func_title == expected_title # Indonesia os.environ['LANG'] = 'id' func_title = 'Be affected' real_title = safeTr(func_title) expected_title = 'Terkena dampak' msg = 'expected %s but got %s' % (expected_title, real_title) assert expected_title == real_title, msg
def testAllDynamicTranslatons(self): """Test all the phrases defined in dynamic_translations translate.""" myParentPath = os.path.join(__file__, os.path.pardir, os.path.pardir) myDirPath = os.path.abspath(myParentPath) myFilePath = os.path.join(myDirPath, 'safe', 'common', 'dynamic_translations.py') myFile = file(myFilePath, 'rt') myFailureList = [] os.environ['LANG'] = 'id' myLineCount = 0 # exception_words is a list of words that has the same form in both # English and Indonesian. For example hotel, bank exception_words = ['hotel', 'bank'] for myLine in myFile.readlines(): myLineCount += 1 if 'tr(' in myLine: myMatch = re.search(r'\(\'(.*)\'\)', myLine, re.M | re.I) if myMatch: myGroup = myMatch.group() myCleanedLine = myGroup[2:-2] if myCleanedLine in exception_words: continue myTranslation = safeTr(myCleanedLine) print myTranslation, myCleanedLine if myCleanedLine == myTranslation: myFailureList.append(myCleanedLine) myMessage = ('Translations not found for:\n %s\n%i ' 'of %i untranslated\n' % ( str(myFailureList).replace(',', '\n'), len(myFailureList), myLineCount)) myMessage += ('If you think the Indonesian word for the failed ' 'translations is the same form in English, i.' 'e. "hotel", you can add it in exception_words in ' 'safe_qgis/test_safe_translations.py') assert len(myFailureList) == 0, myMessage