Ejemplo n.º 1
0
    def _get_translation_function_parameters(self, value: str):
        placeholders = utils.get_placeholders(value)

        if not placeholders:
            return ''

        return ', '.join('{}: CustomStringConvertible'.format(p.variableName) for p in placeholders)
Ejemplo n.º 2
0
    def generate_namespace_supporting_files(
            self, arguments: NamespaceSupportingFilesArguments):
        projectNamespace = self._sanitize_namespace_name(
            arguments.namespace_name)
        outputDir = self._get_supporting_files_dir(
            arguments.project_output_dir)
        outputFile = os.path.join(
            outputDir, 'String+{}{}.swift'.format(
                arguments.project_short_identifier.capitalize(),
                projectNamespace))

        translationFunctions = [
            TranslationFunction(
                name=utils.camelize(key['key']),
                localisedKey=key['key'].strip(),
                localisedKeyValue=self._sanitize_localised_key_value(
                    key['value']),
                localisedKeyValueContainsHTML=key['contains_HTML'],
                localisedKeyComment=utils.to_comment_lines(key['comment']),
                parameters=self._get_translation_function_parameters(
                    key['value']),
                placeholders=utils.get_placeholders(key['value']))
            for key in arguments.localised_keys
        ]

        with open(outputFile, 'w') as file:
            template = arguments.environment.get_template(
                'String+Namespace.swift')
            file.write(
                template.render(
                    projectName=arguments.project_name,
                    projectShortIdentifier=arguments.project_short_identifier,
                    projectNamespace=projectNamespace,
                    translationFunctions=translationFunctions))
Ejemplo n.º 3
0
    def run(self, output_dir, db, project, project_version, project_namespaces,
            project_languages, default_language):
        json_dir = os.path.join(output_dir, 'json')
        print('  Creating json output directory')
        utils.make_dir(json_dir)

        for language in project_languages:
            language_file = os.path.join(
                json_dir, '{}.json'.format(language['slug'].strip().lower()))

            language_namespaces = {}

            for project_namespace in project_namespaces:
                translated_keys = db.get_translated_keys(
                    project_namespace['id'], language['id'])
                count_translated_keys = len(translated_keys)

                skip_text = ''

                if count_translated_keys == 0:
                    skip_text = ', skipping it'

                print('      Found {} keys for namespace {} with locale {}{}'.
                      format(len(translated_keys), project_namespace['name'],
                             language['slug'], skip_text))

                if count_translated_keys > 0:
                    current_namespace = {}
                    for translated_key in translated_keys:
                        value = translated_key['value'].strip()
                        placeholders = utils.get_placeholders(
                            translated_key['value'])

                        # Replace placeholders format from ${name} to {{name}}
                        if placeholders:
                            for placeholder in placeholders:
                                value = value.replace(
                                    placeholder.placeholder,
                                    '{{' + placeholder.variableName + '}}')

                        current_namespace[utils.filter_chars_numbers(
                            translated_key['key'])] = value
                    language_namespaces[utils.filter_chars_numbers(
                        project_namespace['name'])] = current_namespace

            with open(language_file, 'w') as json_file:
                json.dump(language_namespaces, json_file)
Ejemplo n.º 4
0
 def test_valid_placeholder_2(self):
     self.assertEqual([LocaliserPlaceholder(placeholder='${nAMe}', variableName='name')],
                      utils.get_placeholders('hi nothing at all for ${nAMe}'))
Ejemplo n.º 5
0
 def test_invalid_placeholder_5(self):
     self.assertEqual([], utils.get_placeholders('hi nothing at all for ${45name}'))
Ejemplo n.º 6
0
 def test_invalid_placeholder_3(self):
     self.assertEqual([], utils.get_placeholders('hi nothing at all for ${__123}'))
Ejemplo n.º 7
0
 def test_no_placeholders(self):
     self.assertEqual([], utils.get_placeholders('hi nothing at all'))