Skip to content

Commit

Permalink
Merge pull request #78 from booking/jeongseok/fix-console-error
Browse files Browse the repository at this point in the history
[v2.2.2] Fix translation function issue related to curly braces
  • Loading branch information
o-star authored and GitHub Enterprise committed Feb 12, 2025
2 parents f9e0ae1 + 513911d commit 88f59fc
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 20 deletions.
2 changes: 1 addition & 1 deletion packages/js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "airport-js",
"version": "2.2.0",
"version": "2.2.2",
"author": "jeongseok oh <jeongseok.ooh@navercorp.com>, junsoo choi <junsoo.choi@navercorp.com>",
"license": "MIT",
"types": "./build/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/js/src/Airport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export class Airport<L extends ReadonlyArray<string>, G extends LS<L> | PartialL
})

translated = translated?.replace(new RegExp(`\\{(.[^\\}]*)\\}`, 'gi'), (match, p1) => {
if (Object.keys(variableMap).every(variableKey => !p1?.includes(variableKey))) return match
if (Object.keys(variableMap ?? {}).every(variableKey => !p1?.includes(variableKey))) return match
return eval(
`${variableEntries
.map(([key, value]) => {
Expand Down
18 changes: 9 additions & 9 deletions packages/js/src/__tests__/Airport-currency.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Currency } from '../types'
describe('Airport currency functions test', () => {
const supportedLocales = ['ko-KR', 'en-US', 'ja-JP'] as const

let airport: Airport<typeof supportedLocales>
let airport: Airport<typeof supportedLocales, {}>
beforeEach(() => {
airport = new Airport({
supportedLocales,
Expand Down Expand Up @@ -43,7 +43,7 @@ describe('Airport currency functions test', () => {

const value = 100000

const enUsFormatted = airport.fc(value, null, null, null, 'en-US')
const enUsFormatted = airport.fc(value, undefined, undefined, undefined, 'en-US')
const enUsUsdIntlFormatted = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' }).format(value)

expect(enUsFormatted).toBe(enUsUsdIntlFormatted)
Expand Down Expand Up @@ -165,7 +165,7 @@ describe('Airport currency functions test', () => {

const value = 100000

const krwPrice = localAirport.fc(value, null, Currency.JPY)
const krwPrice = localAirport.fc(value, undefined, Currency.JPY)
const exchangedValue = (value / (usdToJpy / usd)) * (usdToKrw / usd)
const koKrKrwIntlFormatted = new Intl.NumberFormat('ko-KR', { style: 'currency', currency: 'KRW' }).format(exchangedValue)

Expand Down Expand Up @@ -193,7 +193,7 @@ describe('Airport currency functions test', () => {

const value = 100000

const krwPrice = localAirport.fc(value, null, Currency.JPY)
const krwPrice = localAirport.fc(value, undefined, Currency.JPY)
const koKrKrwIntlFormatted = new Intl.NumberFormat('ko-KR', { style: 'currency', currency: 'JPY' }).format(value)

expect(krwPrice).toBe(koKrKrwIntlFormatted)
Expand Down Expand Up @@ -221,7 +221,7 @@ describe('Airport currency functions test', () => {

const value = 100000

const krwPrice = localAirport.fc(value, null, Currency.JPY)
const krwPrice = localAirport.fc(value, undefined, Currency.JPY)
const exchangedValue = (value / (krwToJyp / krw)) * (krw / krw)
const koKrKrwIntlFormatted = new Intl.NumberFormat('ko-KR', { style: 'currency', currency: 'KRW' }).format(exchangedValue)

Expand All @@ -247,14 +247,14 @@ describe('Airport currency functions test', () => {
test('airport.fc() can format isFixedCurrency with baseCurrency', () => {
const value = 100000

const koKrIntlBasedFormatted = airport.fc(value, null, Currency.KRW, true)
const koKrIntlBasedFormatted = airport.fc(value, undefined, Currency.KRW, true)
const koKrKrwIntlFormatted = new Intl.NumberFormat('ko-KR', { style: 'currency', currency: 'KRW' }).format(value)

expect(koKrIntlBasedFormatted).toBe(koKrKrwIntlFormatted)

airport.changeLocale('en-US')

const enUsIntlBasedFormatted = airport.fc(value, null, Currency.KRW, true)
const enUsIntlBasedFormatted = airport.fc(value, undefined, Currency.KRW, true)
const enUsKrwIntlFormatted = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'KRW' }).format(value)
expect(enUsIntlBasedFormatted).toBe(enUsKrwIntlFormatted)

Expand All @@ -272,13 +272,13 @@ describe('Airport currency functions test', () => {
},
})

const koKrLsBasedFormatted = formattedCurrencyAirport.fc(value, null, Currency.KRW, true)
const koKrLsBasedFormatted = formattedCurrencyAirport.fc(value, undefined, Currency.KRW, true)
const koKrIntlNumberFormatted = new Intl.NumberFormat('ko-KR').format(value)
expect(koKrLsBasedFormatted).toBe(`${koKrIntlNumberFormatted}원입니다.`)

formattedCurrencyAirport.changeLocale('en-US')

const enUsLsBasedFormatted = formattedCurrencyAirport.fc(value, null, Currency.KRW, true)
const enUsLsBasedFormatted = formattedCurrencyAirport.fc(value, undefined, Currency.KRW, true)
const enUsIntlNumberFormatted = new Intl.NumberFormat('en-US').format(value)
expect(enUsLsBasedFormatted).toBe(`${enUsIntlNumberFormatted}원입니다.`)
})
Expand Down
2 changes: 1 addition & 1 deletion packages/js/src/__tests__/Airport-performance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Currency } from '../types'
describe('Airport performance test', () => {
const supportedLocales = ['ko-KR', 'en-US', 'ja-JP'] as const

let airport: Airport<typeof supportedLocales>
let airport: Airport<typeof supportedLocales, {}>
beforeEach(() => {
airport = new Airport({
supportedLocales,
Expand Down
28 changes: 22 additions & 6 deletions packages/js/src/__tests__/Airport-translation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ describe('Airport translation functions test', () => {
locale: 'ja',
fallbackLocale: 'ko',
})

const LS = createPartialLS({
onlyKoEn: {
ko: 'korean',
Expand All @@ -152,7 +152,7 @@ describe('Airport translation functions test', () => {
locale: 'ja',
fallbackLocale: 'ko',
})

const LS = createPartialLS({
onlyEn: {
en: 'english',
Expand All @@ -162,15 +162,31 @@ describe('Airport translation functions test', () => {
expect(airport.t(LS.onlyEn)).toBe('')
})

test('airport should not cause any errors related to curly braces', () => {
const airport = new Airport({
supportedLocales,
globalLS,
locale: 'ja',
fallbackLocale: 'ko',
})

const LS = createPartialLS({
onlyKo: {
ko: `형식: {"test": {"title": "", "alt": ""}, ...}`,
},
})

expect(airport.t(LS.onlyKo)).toBe('형식: {"test": {"title": "", "alt": ""}, ...}')
})



test('Airport supports partialLS', () => {
const partialLS = createPartialLS({
koTest: { ko: '부분 언어 집합' },
enTest: { en: 'This is english LS'}
})
// expect(airport.t(partialLS.koTest)).toBe('부분 언어 집합')
// expect(airport.t(partialLS.enTest)).toBe(undefined)
// TODO: Airport partialLS 기능 아직 미완성 상태. 완성 시 테스트 케이스 완성 예정

expect(airport.t(partialLS.koTest)).toBe('부분 언어 집합')
expect(airport.t(partialLS.enTest)).toBe("")
})
})
4 changes: 2 additions & 2 deletions packages/react/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "airport-react",
"version": "2.2.1",
"version": "2.2.2",
"author": "jeongseok oh <jeongseok.ooh@navercorp.com>, junsoo choi <junsoo.choi@navercorp.com>",
"license": "MIT",
"types": "./build/index.d.ts",
Expand All @@ -20,7 +20,7 @@
"build": "yarn clean && npx rollup -c"
},
"dependencies": {
"airport-js": "^2.2.0"
"airport-js": "^2.2.2"
},
"peerDependencies": {
"react": "^16.8.0 || ^17.0.0 || ^18.0.0",
Expand Down

0 comments on commit 88f59fc

Please sign in to comment.