Skip to content

Commit

Permalink
feat(packages/sui-i18n): Reuse the interpolate regex and handle its s…
Browse files Browse the repository at this point in the history
…tatefull by reseting the lastIn
  • Loading branch information
Ferran Simon authored and Ferran Simon committed Aug 2, 2024
1 parent de91053 commit e8ed174
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 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,8 +212,9 @@ export default class Rosetta {
let {key, children} = match.groups

// Handle nested matches
const nestedRegExp = /%\[(?<key>[\S\s]*?)\b\](?<children>[\S\s]*?)\[\1\]%/gi
if (nestedRegExp.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 e8ed174

Please sign in to comment.