Skip to content

Commit

Permalink
Merge pull request #2 from sergix44/fix-capitalization
Browse files Browse the repository at this point in the history
Fix capitalization
  • Loading branch information
sergix44 authored May 16, 2024
2 parents d999338 + b5117a2 commit 174a2e3
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 9 deletions.
8 changes: 6 additions & 2 deletions src/translator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,12 @@ const translate = (translation: string | object, replace: object = {}, locale: s
}

Object.keys(replace).forEach(key => {
const value = replace[key]
translation = translation.toString().replace(':' + key, value)
const value = replace[key]?.toString()

translation = translation.toString()
.replace(':' + key, value)
.replace(':' + key.charAt(0).toUpperCase() + key.slice(1), value.charAt(0).toUpperCase() + value.slice(1))
.replace(':' + key.toUpperCase(), value.toUpperCase())
})

return translation
Expand Down
5 changes: 4 additions & 1 deletion tests/exporter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ test('exports complex locale', async () => {
"password": "The provided password is incorrect.",
"foo": {"level1": {"level2": "baren"}},
"arr": ["foo", "bar"],
"multiline": "Lorem ipsum dolor sit amet."
"multiline": "Lorem ipsum dolor sit amet.",
"accepted_1": "The :attribute must be accepted.",
"accepted_2": "The :Attribute must be accepted.",
"accepted_3": "The :ATTRIBUTE must be accepted.",
},
"nested": {"cars": {"car": {"is_electric": "Electric", "foo": {"level1": {"level2": "barpt"}}}}},
"domain": {
Expand Down
15 changes: 9 additions & 6 deletions tests/fixtures/lang/en/auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
'password' => 'The provided password is incorrect.',
'foo' => [
'level1' => [
'level2' => 'baren'
]
'level2' => 'baren',
],
],
'arr' => ['foo', 'bar'],
'multiline' => 'Lorem ' .
'ipsum ' .
'dolor ' .
'sit ' .
'multiline' => 'Lorem '.
'ipsum '.
'dolor '.
'sit '.
'amet.',
'accepted_1' => 'The :attribute must be accepted.',
'accepted_2' => 'The :Attribute must be accepted.',
'accepted_3' => 'The :ATTRIBUTE must be accepted.',
];
18 changes: 18 additions & 0 deletions tests/trans.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,22 @@ test('trans return object for partial translation paths', async () => {
const r = trans('domain.car.foo.level1') as Object

expect(r).toEqual({level2: 'barpt'})
})

test('trans works with capitalization lowercase', async () => {
const r = trans('auth.accepted_1', {'attribute': 'email'})

expect(r).toBe('The email must be accepted.')
})

test('trans works with capitalization ucfirst', async () => {
const r = trans('auth.accepted_2', {'attribute': 'email'})

expect(r).toBe('The Email must be accepted.')
})

test('trans works with capitalization uppercase', async () => {
const r = trans('auth.accepted_3', {'attribute': 'email'})

expect(r).toBe('The EMAIL must be accepted.')
})

0 comments on commit 174a2e3

Please sign in to comment.