Skip to content

Commit

Permalink
Fix getLocalizationTable
Browse files Browse the repository at this point in the history
  • Loading branch information
robincodex committed Mar 29, 2024
1 parent 15f7ca8 commit 803bd1f
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 11 deletions.
2 changes: 1 addition & 1 deletion __tests__/__snapshots__/localize_macro_test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`localize_macro getLocalizationList 1`] = `
exports[`localize_macro getLocalizationTable 1`] = `
""use strict";
const token_a = "#test_token_a";
Expand Down
6 changes: 3 additions & 3 deletions __tests__/localize_macro_test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { parseMacros } from './utils';
import { describe, expect, test } from '@jest/globals';
import { getLocalizationList } from '../packages/panorama-all-in-jsx/src/localize.macro';
import { getLocalizationTable } from '../packages/panorama-all-in-jsx/src/localize.macro';

describe('localize_macro', function () {
test('getLocalizationList', function () {
test('getLocalizationTable', function () {
const result = parseMacros(
`
import localize from '../packages/panorama-all-in-jsx/src/localize.macro';
Expand All @@ -21,7 +21,7 @@ describe('localize_macro', function () {
);
expect(result).toMatchSnapshot();

expect(getLocalizationList()).toStrictEqual({
expect(getLocalizationTable()).toStrictEqual({
test_token_a: {
english: 'this is a',
schinese: '这是a',
Expand Down
2 changes: 1 addition & 1 deletion packages/panorama-all-in-jsx/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "solid-panorama-all-in-jsx",
"version": "0.3.7",
"version": "0.3.8",
"description": "",
"engines": {
"node": ">=16.0.0"
Expand Down
20 changes: 15 additions & 5 deletions packages/panorama-all-in-jsx/src/localize.macro.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import { createMacro } from 'babel-plugin-macros';
import { createHash } from 'crypto';

let localizationList: Record<string, Record<string, string>> = {};
type LocalizationTable = Record<string, Record<string, string>>;

let fileLocalization: Record<string, LocalizationTable> = {};

export default createMacro(
function ({ references, state, babel, config }) {
localizationList = {};
if (!state.filename) {
throw new Error('state.filename is not defined');
}
const localizationTable: LocalizationTable = {};
fileLocalization[state.filename] = localizationTable;
const argv = config ? config['localize_language_argv'] : ['english'];

for (const path of references.default) {
Expand Down Expand Up @@ -64,7 +70,7 @@ export default createMacro(
hash.update(token);
token = `token_${hash.digest('hex').slice(0, 16)}`;
}
localizationList[token] = localizationData;
localizationTable[token] = localizationData;

// replace localize call with token
path.parentPath.replaceWith(
Expand All @@ -76,6 +82,10 @@ export default createMacro(
{ configName: 'panorama-all-in-jsx' }
);

export function getLocalizationList() {
return localizationList;
export function getLocalizationTable() {
const localizationTable: LocalizationTable = {};
for (const [_, table] of Object.entries(fileLocalization)) {
Object.assign(localizationTable, table);
}
return localizationTable;
}
2 changes: 1 addition & 1 deletion packages/panorama-all-in-jsx/types/localize.macro.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export interface LocalizationData {
ukrainian?: string;
vietnamese?: string;
}
export function getLocalizationList<T extends LocalizationData>(): Record<
export function getLocalizationTable<T extends LocalizationData>(): Record<
string,
T
>;

0 comments on commit 803bd1f

Please sign in to comment.