Skip to content

Commit d484e1b

Browse files
Merge pull request #245 from contentstack/bug/VB-797-lowercase-locale
[VB797] [LowercaseLocale] Added option in `addTags` to disable lowercasing of locale
2 parents 31f6b5a + 9b0b66a commit d484e1b

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

__test__/entry-editable.test.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,4 +519,45 @@ describe('Entry editable test', () => {
519519
})
520520
})
521521

522+
describe('useLowerCaseLocale option', () => {
523+
it('should preserve locale casing when useLowerCaseLocale is false', done => {
524+
const entry = {
525+
"uid": "test_uid",
526+
"locale": "en-US",
527+
"title": "Test Entry"
528+
}
529+
530+
addTags(entry, 'test_content_type', false, 'en-US', { useLowerCaseLocale: false })
531+
532+
expect((entry as any)['$']['title']).toEqual('data-cslp=test_content_type.test_uid.en-US.title')
533+
done()
534+
})
535+
536+
it('should lowercase locale when useLowerCaseLocale is explicitly true', done => {
537+
const entry = {
538+
"uid": "test_uid",
539+
"locale": "en-US",
540+
"title": "Test Entry"
541+
}
542+
543+
addTags(entry, 'test_content_type', false, 'en-US', { useLowerCaseLocale: true })
544+
545+
expect((entry as any)['$']['title']).toEqual('data-cslp=test_content_type.test_uid.en-us.title')
546+
done()
547+
})
548+
549+
it('should lowercase locale by default when options is not provided', done => {
550+
const entry = {
551+
"uid": "test_uid",
552+
"locale": "en-US",
553+
"title": "Test Entry"
554+
}
555+
556+
addTags(entry, 'test_content_type', false, 'en-US')
557+
558+
expect((entry as any)['$']['title']).toEqual('data-cslp=test_content_type.test_uid.en-us.title')
559+
done()
560+
})
561+
})
562+
522563
})

src/entry-editable.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,16 @@ interface AppliedVariants {
1717
* @param contentTypeUid - Content type UID (e.g. `blog_post`). Used as part of the tag path.
1818
* @param tagsAsObject - If true, tags are stored as objects (e.g. `{ "data-cslp": "..." }`); if false, as strings (e.g. `data-cslp=...`).
1919
* @param locale - Locale code for the tag path (default: `'en-us'`).
20+
* @param options.useLowerCaseLocale - Optional boolean to make locale case-insensitive.
2021
*/
21-
export function addTags(entry: EntryModel, contentTypeUid: string, tagsAsObject: boolean, locale: string = 'en-us'): void {
22+
export function addTags(entry: EntryModel, contentTypeUid: string, tagsAsObject: boolean, locale: string = 'en-us', options?: {
23+
useLowerCaseLocale?: boolean
24+
}): void {
25+
const { useLowerCaseLocale = true } = options || {};
2226
if (entry) {
2327
// handle case senstivity for contentTypeUid and locale
2428
contentTypeUid = contentTypeUid.toLowerCase();
25-
locale = locale.toLowerCase();
29+
locale = useLowerCaseLocale ? locale.toLowerCase() : locale;
2630

2731
const appliedVariants = entry._applied_variants || entry?.system?.applied_variants || null;
2832
entry.$ = getTag(entry, `${contentTypeUid}.${entry.uid}.${locale}`, tagsAsObject, locale, { _applied_variants: appliedVariants, shouldApplyVariant: !!appliedVariants, metaKey: '' })

0 commit comments

Comments
 (0)