Exemple #1
0
 def _add_message(self, catalog, message):
     lineno, string, comments, context = message
     flags = set()
     if string in catalog:
         existing_message = catalog.get(string)
         flags = existing_message.flags
     return catalog.add(string, None, auto_comments=comments, context=context, flags=flags)
Exemple #2
0
 def _add_message(self, catalog, message):
     lineno, string, comments, context = message
     flags = set()
     if string in catalog:
         existing_message = catalog.get(string)
         flags = existing_message.flags
     return catalog.add(string, None, auto_comments=comments, context=context,
                        flags=flags)
Exemple #3
0
    def _generate_update_sheets_requests(self,
                                         sheet_ids_to_catalogs,
                                         source_lang,
                                         spreadsheet_id,
                                         prune=False):
        requests = []
        for sheet_id, catalog in sheet_ids_to_catalogs.iteritems():
            lang = str(catalog.locale)
            existing_values = self._download_sheet(spreadsheet_id, lang)
            for x in range(self.HEADER_ROW_COUNT):
                existing_values.pop(0)  # Remove header rows.
            for value in existing_values:
                if not value or self._is_empty_row(value):  # Skip empty rows.
                    continue
                if value not in catalog:
                    source = value[0]
                    translation = value[1] if len(value) > 1 else None
                    catalog.add(source,
                                translation,
                                auto_comments=[],
                                context=None,
                                flags=[])

            # Check for missing columns.
            num_missing_columns = 0
            for column in existing_values[0]:
                if column == None:
                    num_missing_columns += 1

            if num_missing_columns:
                requests.append({
                    'appendDimension': {
                        'sheetId': sheet_id,
                        'dimension': 'COLUMNS',
                        'length': num_missing_columns,
                    },
                })

                # Update the column headers.
                requests.append({
                    'updateCells': {
                        'fields': 'userEnteredValue',
                        'start': {
                            'sheetId': sheet_id,
                            'rowIndex': 0,
                            'columnIndex': 0,
                        },
                        'rows':
                        [self._create_header_row_data(source_lang, lang)],
                    },
                })

            # Perform a diff of the existing data to what the catalog provides
            # to make targeted changes to the spreadsheet and preserve meta
            # information--such as comments.
            existing_rows, new_rows, removed_rows = self._diff_data(
                existing_values, catalog)

            # Update the existing values in place.
            if len(existing_rows):
                row_data = []
                for value in existing_rows:
                    if not value:  # Skip empty rows.
                        continue
                    row_data.append(
                        self._create_catalog_row(value['source'],
                                                 value['translation'],
                                                 value['comments'],
                                                 value['locations']))
                # NOTE This is not (yet) smart enough to only update small sections
                # with the updated information. Hint: Use value['updated'].
                requests.append({
                    'updateCells': {
                        'fields': 'userEnteredValue',
                        'start': {
                            'sheetId': sheet_id,
                            # Skip header row.
                            'rowIndex': self.HEADER_ROW_COUNT,
                            'columnIndex': 0,
                        },
                        'rows': row_data,
                    },
                })

            # Append new values to end of sheet.
            if len(new_rows):
                row_data = []
                for value in new_rows:
                    row_data.append(
                        self._create_catalog_row(value['source'],
                                                 value['translation'],
                                                 value['comments'],
                                                 value['locations']))

                requests.append({
                    'appendCells': {
                        'sheetId': sheet_id,
                        'fields': 'userEnteredValue',
                        'rows': row_data,
                    },
                })

            # Remove obsolete rows if not included.
            if prune and len(removed_rows):
                for value in reversed(removed_rows):  # Start from the bottom.
                    # NOTE this is ineffecient since it does not combine ranges.
                    # ex: 1, 2, 3 are three requests instead of one request 1-3
                    requests.append({
                        'deleteDimension': {
                            'range': {
                                'sheetId': sheet_id,
                                'dimension': 'ROWS',
                                'startIndex': self.HEADER_ROW_COUNT + value,
                                'endIndex': self.HEADER_ROW_COUNT + value + 1,
                            },
                        },
                    })

            # Sort all rows.
            requests.append({
                'sortRange': {
                    'range': {
                        'sheetId': sheet_id,
                        'startColumnIndex': 0,
                        'startRowIndex': self.HEADER_ROW_COUNT,
                    },
                    'sortSpecs': [{
                        'dimensionIndex': 0,
                        'sortOrder': 'ASCENDING',
                    }],
                },
            })

            requests += self._generate_comments_column_requests(
                sheet_id, catalog)
        return requests
Exemple #4
0
    def _generate_update_sheets_requests(self, sheet_ids_to_catalogs,
                                         source_lang, spreadsheet_id, prune=False):
        requests = []
        for sheet_id, catalog in sheet_ids_to_catalogs.iteritems():
            lang = str(catalog.locale)
            existing_values = self._download_sheet(spreadsheet_id, lang)
            for x in range(self.HEADER_ROW_COUNT):
                existing_values.pop(0)  # Remove header rows.
            for value in existing_values:
                if not value or self._is_empty_row(value):  # Skip empty rows.
                    continue
                if value not in catalog:
                    source = value[0]
                    translation = value[1] if len(value) > 1 else None
                    catalog.add(source, translation, auto_comments=[],
                                context=None, flags=[])

            # Check for missing columns.
            num_missing_columns = 0
            for column in existing_values[0]:
                if column == None:
                    num_missing_columns += 1

            if num_missing_columns:
                requests.append({
                    'appendDimension': {
                        'sheetId': sheet_id,
                        'dimension': 'COLUMNS',
                        'length': num_missing_columns,
                    },
                })

                # Update the column headers.
                requests.append({
                    'updateCells': {
                        'fields': 'userEnteredValue',
                        'start': {
                            'sheetId': sheet_id,
                            'rowIndex': 0,
                            'columnIndex': 0,
                        },
                        'rows': [
                            self._create_header_row_data(source_lang, lang)
                        ],
                    },
                })

            # Perform a diff of the existing data to what the catalog provides
            # to make targeted changes to the spreadsheet and preserve meta
            # information--such as comments.
            existing_rows, new_rows, removed_rows = self._diff_data(
                existing_values, catalog)

            # Update the existing values in place.
            if len(existing_rows):
                row_data = []
                for value in existing_rows:
                    if not value:  # Skip empty rows.
                        continue
                    row_data.append(self._create_catalog_row(
                        value['source'], value['translation'],
                        value['comments'], value['locations']))
                # NOTE This is not (yet) smart enough to only update small sections
                # with the updated information. Hint: Use value['updated'].
                requests.append({
                    'updateCells': {
                        'fields': 'userEnteredValue',
                        'start': {
                            'sheetId': sheet_id,
                            # Skip header row.
                            'rowIndex': self.HEADER_ROW_COUNT,
                            'columnIndex': 0,
                        },
                        'rows': row_data,
                    },
                })

            # Append new values to end of sheet.
            if len(new_rows):
                # Mark to update the sheet metadata after done.
                self.update_meta_after_upload = True
                row_data = []
                for value in new_rows:
                    row_data.append(self._create_catalog_row(
                        value['source'], value['translation'],
                        value['comments'], value['locations']))

                requests.append({
                    'appendCells': {
                        'sheetId': sheet_id,
                        'fields': 'userEnteredValue',
                        'rows': row_data,
                    },
                })

            # Remove obsolete rows if not included.
            if prune and len(removed_rows):
                for value in reversed(removed_rows):  # Start from the bottom.
                    # NOTE this is ineffecient since it does not combine ranges.
                    # ex: 1, 2, 3 are three requests instead of one request 1-3
                    requests.append({
                        'deleteDimension': {
                            'range': {
                                'sheetId': sheet_id,
                                'dimension': 'ROWS',
                                'startIndex': self.HEADER_ROW_COUNT + value,
                                'endIndex': self.HEADER_ROW_COUNT + value + 1,
                            },
                        },
                    })

            # Sort all rows.
            requests.append({
                'sortRange': {
                    'range': {
                        'sheetId': sheet_id,
                        'startColumnIndex': 0,
                        'startRowIndex': self.HEADER_ROW_COUNT,
                    },
                    'sortSpecs': [
                        {
                            'dimensionIndex': 0,
                            'sortOrder': 'ASCENDING',
                        }
                    ],
                },
            })

            requests += self._generate_comments_column_requests(
                sheet_id, catalog)
        return requests
for a in res:
    print(a)
print()
"""
#(filename, lineno, message, comments, context)
('main.py', 9, 'MSG000', [], None)
('sub.py', 1, 'MSG000', [], None)
('mypackage/mymodule.py', 1, 'Good Luck !', [], None)
"""

#翻訳データ(Catalog)を作成する
import babel.messages.catalog
catalog = babel.messages.catalog.Catalog(locale='ja', domain='hello')
c_list = list(babel.messages.extract.extract_from_dir(dirname='../../src/'))
for c_tpl in c_list:
    catalog.add(c_tpl[2], string='翻訳後テキスト', locations=[(c_t[0], c_t[1]) for c_t in c_list if c_tpl[2]==c_t[2]])

#poファイル書き出し
from babel._compat import BytesIO
buf = BytesIO()
import babel.messages.pofile
babel.messages.pofile.write_po(buf, catalog)
#babel.messages.pofile.write_po(buf, catalog, omit_header=True)
print(buf.getvalue().decode("utf8"))

"""
#: main.py:9 sub.py:1
msgid "MSG000"
msgstr "翻訳後テキスト"

#: mypackage/mymodule.py:1