Skip to content

Commit

Permalink
Merge pull request #100 from AckeeCZ/fix/find-fallback-column
Browse files Browse the repository at this point in the history
Fix fallback plugin not finding default language column
  • Loading branch information
cermakjiri authored Dec 2, 2024
2 parents 0a0b08c + 9519d46 commit 3dff337
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
32 changes: 27 additions & 5 deletions packages/plugin-fallback/src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,39 @@
import type { GoogleSpreadsheetRow } from 'google-spreadsheet';
import type { GoogleSpreadsheetRow, GoogleSpreadsheetWorksheet } from 'google-spreadsheet';
import { Line } from '@lokse/core';

import fallbackPluginFactory from '..';
import type { PluginOptions } from '..';

export const createRow = (rowIndex: number, values: { [key: string]: any }) =>
({
export const createRow = (rowIndex: number, values: { [key: string]: any }) => {
const defaultRow = {
rowIndex,
...values,
get: (key: string) => values[key],
save: () => null,
delete: () => null,
}) as unknown as GoogleSpreadsheetRow;
};
return new Proxy<GoogleSpreadsheetRow>({} as unknown as GoogleSpreadsheetRow, {
get(_t: GoogleSpreadsheetRow, p: string | symbol, _r: any): any {
const key = p as keyof GoogleSpreadsheetRow;
if (key in defaultRow) {
return defaultRow[key as keyof typeof defaultRow];
}

if (key === '_worksheet') {
return new Proxy<GoogleSpreadsheetWorksheet>({} as unknown as GoogleSpreadsheetWorksheet, {
get(_t: GoogleSpreadsheetWorksheet, p: string | symbol, _r: any): any {
if (p === ('headerValues' satisfies keyof GoogleSpreadsheetWorksheet)) {
return Object.keys(values);
}

throw new Error(`GoogleSpreadsheetWorksheet::${key} not implemented`);
},
});
}

throw new Error(`GoogleSpreadsheetRow::${key} not implemented`);
},
});
};

describe('Fallback plugin', () => {
const logger = { warn: jest.fn(), log: jest.fn() };
Expand Down
3 changes: 2 additions & 1 deletion packages/plugin-fallback/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ export default function (options: PluginOptions, { languages }: GeneralPluginMet
return createPlugin({
async readTranslation(line, meta) {
if (line.key && !line.value) {
const defaultLanguageKey = Object.keys(meta.row).find(key => isDefaultLang(key)) ?? NOT_FOUND_KEY;
const defaultLanguageKey =
meta.row._worksheet.headerValues.find(key => isDefaultLang(key)) ?? NOT_FOUND_KEY;

const fallbackLanguageValue = meta.row.get(defaultLanguageKey) ?? '';

Expand Down

0 comments on commit 3dff337

Please sign in to comment.