Skip to content

Commit

Permalink
Merge pull request #1801 from SUI-Components/fix/interpolateWithMulti…
Browse files Browse the repository at this point in the history
…pleNestingKeys

Fix/interpolate with multiple nesting keys
  • Loading branch information
ferransimon authored Aug 2, 2024
2 parents 6019b3c + e8ed174 commit 5b41211
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions packages/sui-i18n/src/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import {slugify} from '@s-ui/js/lib/string/slugify.js'

import DefaultAdapter from './adapters/default.js'

const INTERPOLATE_REGEX = /%\[(?<key>[\S\s]*?)\b\](?<children>[\S\s]*?)\[\1\]%/gi

export default class Rosetta {
constructor({adapter = new DefaultAdapter()} = {}) {
this._culture = null
Expand Down Expand Up @@ -195,14 +197,13 @@ export default class Rosetta {

// Interpolate each text chunk, returning an array of all the transformed chunks.
interpolate(key, values = {}) {
// Redeclare the RegExp on each call to make it stateless
const interpolateRegExp = /%\[(?<key>[\S\s]*?)\b\](?<children>[\S\s]*?)\[\1\]%/gi

// Perform basic replace for static values
const str = this.t(key, values)

// Identify all the occurrences which are like: %[key]children[key]%, save {key, children} in a group for every match
const matches = str.matchAll(interpolateRegExp)
// Reset the state of the regex to start from the beginning
INTERPOLATE_REGEX.lastIndex = 0
const matches = str.matchAll(INTERPOLATE_REGEX)

let remaining = str

Expand All @@ -211,7 +212,9 @@ export default class Rosetta {
let {key, children} = match.groups

// Handle nested matches
if (interpolateRegExp.test(children)) {
// We need to reset the lastIndex to 0 to start the search from the beginning
INTERPOLATE_REGEX.lastIndex = 0
if (INTERPOLATE_REGEX.test(children)) {
children = this.interpolate(children, values)
}

Expand Down

0 comments on commit 5b41211

Please sign in to comment.