Skip to content

Commit e7a977f

Browse files
committed
feat: added translation key as a comment value to be searchable
1 parent 685dbba commit e7a977f

File tree

3 files changed

+93
-42
lines changed

3 files changed

+93
-42
lines changed

src/BaseWriter.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,26 +161,37 @@ export class BaseWriter {
161161
key === this.rootKey
162162
? keyPrefix
163163
: [keyPrefix, key].filter(Boolean).join('.');
164+
const delimiter = this.options.translationFn ? ';' : ',';
164165

166+
let valueComment = '';
167+
let keyComment = '';
165168
let valueToSet: string;
166-
let comment = '';
167169

168170
if (typeof value !== 'string') {
169171
valueToSet = this.writeObjectAsStr(value, localeKey);
170172
} else {
171173
if (this.options.translationFn) {
172174
valueToSet = `(${this.buildFunctionParam(value)}) => string`;
175+
keyComment = `/* ${localeKey} */`;
173176
} else {
174177
valueToSet = `'${localeKey}'`;
175178
}
176179

177180
if (this.options.showTranslations) {
178-
comment = ` /* ${value} */`;
181+
valueComment = `/* ${value} */`;
179182
}
180183
}
181184

185+
if (keyComment) {
186+
writer.writeLine(keyComment);
187+
writer.writeLine(valueComment);
188+
valueComment = '';
189+
}
190+
182191
const keyToSet = /([^A-z0-9_$]|^[0-9])/.test(key) ? `'${key}'` : key;
183-
writer.writeLine(`${keyToSet}: ${valueToSet},${comment}`);
192+
writer.writeLine(
193+
`${keyToSet}: ${valueToSet}${delimiter} ${valueComment}`
194+
);
184195
});
185196
});
186197

tests/__snapshots__/generator.spec.ts.snap

Lines changed: 71 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,19 @@ export type ILocaleKeys = {
77
order: {
88
shippingLabel: {
99
customerDetailsCard: {
10-
address: (data: Record<'firstName' | 'lastName' | 'addressLine1' | 'addressLine2' | 'city' | 'state' | 'zipCode' | 'country', unknown>) => string, /* {{firstName}} {{lastName}}, {{addressLine1, suffix ', '}}{{addressLine2, suffix ', '}}{{city, concat ', '}}{{state, suffix ' '}}{{zipCode, suffix ', '}}{{country}} */
11-
},
10+
/* order.shippingLabel.customerDetailsCard.address */
11+
/* {{firstName}} {{lastName}}, {{addressLine1, suffix ', '}}{{addressLine2, suffix ', '}}{{city, concat ', '}}{{state, suffix ' '}}{{zipCode, suffix ', '}}{{country}} */
12+
address: (data: Record<'firstName' | 'lastName' | 'addressLine1' | 'addressLine2' | 'city' | 'state' | 'zipCode' | 'country', unknown>) => string;
13+
};
1214
labelDetailsCard: {
1315
shipFrom: {
14-
addressFormat: (data: Record<'addressLine1' | 'addressLine2' | 'city' | 'state' | 'zipCode' | 'country', unknown>) => string, /* {{addressLine1, suffix ', '}}{{addressLine2, suffix ', '}}{{city, suffix ', '}}{{state}} {{zipCode, suffix ', '}}{{country}} */
15-
},
16-
},
17-
},
18-
},
16+
/* order.shippingLabel.labelDetailsCard.shipFrom.addressFormat */
17+
/* {{addressLine1, suffix ', '}}{{addressLine2, suffix ', '}}{{city, suffix ', '}}{{state}} {{zipCode, suffix ', '}}{{country}} */
18+
addressFormat: (data: Record<'addressLine1' | 'addressLine2' | 'city' | 'state' | 'zipCode' | 'country', unknown>) => string;
19+
};
20+
};
21+
};
22+
};
1923
};
2024
const createProxyImpl = <R extends string>(
2125
t = (...[k]: unknown[]) => k as R,
@@ -45,16 +49,32 @@ exports[`generator data interpolation icu snapshot 1`] = `
4549
export type ILocaleKeys = {
4650
common: {
4751
people: {
48-
message: (data: Record<'numPersons', unknown>) => string, /* Hey, {numPersons, plural, =0 {no one} =1 {one person} other {# persons}} */
49-
messageComplex: (data: Record<'name' | 'numPersons' | 'productsAmount', unknown>) => string, /* Hey {name}, There are {numPersons, plural, =0 {no one} =1 {one person} other {# persons}} that want to change the {productsAmount, plural, =1 {price of 1 product} other {prices of # products}} */
50-
pluralMessage: (data: Record<'numPeople', unknown>) => string, /* {numPeople, plural, =0 {No one is} =1 {One person is} other {# people are}} interested */
51-
ordinalMessage: (data: Record<'position', unknown>) => string, /* {position, selectordinal, one {You're 1st} two {You're 2nd} few {You're 3rd} other {You're #th}} */
52-
dateMessage: (data: Record<'currentDate', unknown>) => string, /* Today is {currentDate, date, long} */
53-
timeMessage: (data: Record<'currentTime', unknown>) => string, /* The current time is {currentTime, time, short} */
54-
selectMessage: (data: Record<'gender', unknown>) => string, /* {gender, select, male {He is} female {She is} other {They are} } interested */
55-
numberMessage: (data: Record<'numApples', unknown>) => string, /* You have {numApples, number} apples */
56-
},
57-
},
52+
/* common.people.message */
53+
/* Hey, {numPersons, plural, =0 {no one} =1 {one person} other {# persons}} */
54+
message: (data: Record<'numPersons', unknown>) => string;
55+
/* common.people.messageComplex */
56+
/* Hey {name}, There are {numPersons, plural, =0 {no one} =1 {one person} other {# persons}} that want to change the {productsAmount, plural, =1 {price of 1 product} other {prices of # products}} */
57+
messageComplex: (data: Record<'name' | 'numPersons' | 'productsAmount', unknown>) => string;
58+
/* common.people.pluralMessage */
59+
/* {numPeople, plural, =0 {No one is} =1 {One person is} other {# people are}} interested */
60+
pluralMessage: (data: Record<'numPeople', unknown>) => string;
61+
/* common.people.ordinalMessage */
62+
/* {position, selectordinal, one {You're 1st} two {You're 2nd} few {You're 3rd} other {You're #th}} */
63+
ordinalMessage: (data: Record<'position', unknown>) => string;
64+
/* common.people.dateMessage */
65+
/* Today is {currentDate, date, long} */
66+
dateMessage: (data: Record<'currentDate', unknown>) => string;
67+
/* common.people.timeMessage */
68+
/* The current time is {currentTime, time, short} */
69+
timeMessage: (data: Record<'currentTime', unknown>) => string;
70+
/* common.people.selectMessage */
71+
/* {gender, select, male {He is} female {She is} other {They are} } interested */
72+
selectMessage: (data: Record<'gender', unknown>) => string;
73+
/* common.people.numberMessage */
74+
/* You have {numApples, number} apples */
75+
numberMessage: (data: Record<'numApples', unknown>) => string;
76+
};
77+
};
5878
};
5979
const createProxyImpl = <R extends string>(
6080
t = (...[k]: unknown[]) => k as R,
@@ -84,10 +104,14 @@ exports[`generator data interpolation icu with nested params snapshot 1`] = `
84104
export type ILocaleKeys = {
85105
common: {
86106
people: {
87-
message: (data: Record<'numPersons', unknown>) => string, /* Hey, {numPersons, plural, =0 {no one} =1 {one person} other {# persons}} */
88-
messageNestedParams: (data: Record<'name' | 'numPersons', unknown>) => string, /* Hey, {numPersons, plural, =0 {No one here.} one {{name}. You are the only person here.} other {{name} and # other persons are here.}} */
89-
},
90-
},
107+
/* common.people.message */
108+
/* Hey, {numPersons, plural, =0 {no one} =1 {one person} other {# persons}} */
109+
message: (data: Record<'numPersons', unknown>) => string;
110+
/* common.people.messageNestedParams */
111+
/* Hey, {numPersons, plural, =0 {No one here.} one {{name}. You are the only person here.} other {{name} and # other persons are here.}} */
112+
messageNestedParams: (data: Record<'name' | 'numPersons', unknown>) => string;
113+
};
114+
};
91115
};
92116
const createProxyImpl = <R extends string>(
93117
t = (...[k]: unknown[]) => k as R,
@@ -117,10 +141,14 @@ exports[`generator data interpolation single quote snapshot 1`] = `
117141
export type ILocaleKeys = {
118142
common: {
119143
loggedIn: {
120-
message: (data: Record<'username', unknown>) => string, /* Hey, {username}, you have successfully logged in! */
121-
},
122-
},
123-
readingWarning: (data: Record<'reader' | 'writer', unknown>) => string, /* {reader} reads message from {writer} */
144+
/* common.loggedIn.message */
145+
/* Hey, {username}, you have successfully logged in! */
146+
message: (data: Record<'username', unknown>) => string;
147+
};
148+
};
149+
/* readingWarning */
150+
/* {reader} reads message from {writer} */
151+
readingWarning: (data: Record<'reader' | 'writer', unknown>) => string;
124152
};
125153
const createProxyImpl = <R extends string>(
126154
t = (...[k]: unknown[]) => k as R,
@@ -149,13 +177,17 @@ exports[`generator flat data snapshot 1`] = `
149177
/* tslint:disable */
150178
export type ILocaleKeys = {
151179
common: {
152-
cancel: () => string, /* Cancel */
153-
},
180+
/* common.cancel */
181+
/* Cancel */
182+
cancel: () => string;
183+
};
154184
model: {
155185
player: {
156-
name: () => string, /* Name */
157-
},
158-
},
186+
/* model.player.name */
187+
/* Name */
188+
name: () => string;
189+
};
190+
};
159191
};
160192
const createProxyImpl = <R extends string>(
161193
t = (...[k]: unknown[]) => k as R,
@@ -184,13 +216,17 @@ exports[`generator nested data snapshot 1`] = `
184216
/* tslint:disable */
185217
export type ILocaleKeys = {
186218
common: {
187-
create: () => string, /* Create */
188-
},
219+
/* common.create */
220+
/* Create */
221+
create: () => string;
222+
};
189223
model: {
190224
user: {
191-
id: () => string, /* ID */
192-
},
193-
},
225+
/* model.user.id */
226+
/* ID */
227+
id: () => string;
228+
};
229+
};
194230
};
195231
const createProxyImpl = <R extends string>(
196232
t = (...[k]: unknown[]) => k as R,

tests/__snapshots__/reactBindings.spec.tsx.snap

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,14 @@ import React from 'react';
88
export type ILocaleKeys = {
99
common: {
1010
loggedIn: {
11-
message: (data: Record<'username', unknown>) => string, /* Hey, {{username}}, you have successfully logged in! */
12-
},
13-
},
14-
readingWarning: (data: Record<'reader' | 'writer', unknown>) => string, /* {{reader}} reads message from {{writer}} */
11+
/* common.loggedIn.message */
12+
/* Hey, {{username}}, you have successfully logged in! */
13+
message: (data: Record<'username', unknown>) => string;
14+
};
15+
};
16+
/* readingWarning */
17+
/* {{reader}} reads message from {{writer}} */
18+
readingWarning: (data: Record<'reader' | 'writer', unknown>) => string;
1519
};
1620
const createProxyImpl = <R extends string>(
1721
t = (...[k]: unknown[]) => k as R,

0 commit comments

Comments
 (0)