@@ -222,6 +222,20 @@ export const localeCodeSchema = Z.string().refine((value) => localeCodes.include
222222 message : "Invalid locale code" ,
223223} ) ;
224224
225+
226+ /**
227+ * Resolves a locale code to its full locale representation.
228+ *
229+ * If the provided locale code is already a full locale code, it returns as is.
230+ * If the provided locale code is a short locale code, it returns the first corresponding full locale.
231+ * If the locale code is not found, it throws an error.
232+ *
233+ * @param {localeCodes } value - The locale code to resolve (either short or full)
234+ * @return {LocaleCodeFull } The resolved full locale code
235+ * @throws {Error } If the provided locale code is invalid.
236+ */
237+
238+
225239export const resolveLocaleCode = ( value : LocaleCode ) : LocaleCodeFull => {
226240 const existingFullLocaleCode = Object . values ( localeMap )
227241 . flat ( )
@@ -240,6 +254,15 @@ export const resolveLocaleCode = (value: LocaleCode): LocaleCodeFull => {
240254 throw new Error ( `Invalid locale code: ${ value } ` ) ;
241255} ;
242256
257+ /**
258+ * Determines the delimiter used in a locale code
259+ *
260+ * @param {string } locale - the locale string (e.g.,"en_US","en-GB")
261+ * @return { string | null } - The delimiter ("_" or "-") if found, otherwise `null`.
262+ */
263+
264+
265+
243266export const getLocaleCodeDelimiter = ( locale : string ) : LocaleDelimiter => {
244267 if ( locale . includes ( "_" ) ) {
245268 return "_" ;
@@ -250,6 +273,14 @@ export const getLocaleCodeDelimiter = (locale: string): LocaleDelimiter => {
250273 }
251274} ;
252275
276+ /**
277+ * Replaces the delimiter in a locale string with the specified delimiter.
278+ *
279+ * @param {string }locale - The locale string (e.g.,"en_US", "en-GB").
280+ * @param {"-" | "_" | null } [delimiter] - The new delimiter to replace the existing one.
281+ * @returns {string } The locale string with the replaced delimiter, or the original locale if no delimiter is provided.
282+ */
283+
253284export const resolveOverriddenLocale = ( locale : string , delimiter ?: LocaleDelimiter ) : string => {
254285 if ( ! delimiter ) {
255286 return locale ;
@@ -263,6 +294,14 @@ export const resolveOverriddenLocale = (locale: string, delimiter?: LocaleDelimi
263294 return locale . replace ( currentDelimiter , delimiter ) ;
264295} ;
265296
297+ /**
298+ * Normalizes a locale string by replacing underscores with hyphens
299+ * and removing the "r" in certain regional codes (e.g., "fr-rCA" → "fr-CA")
300+ *
301+ * @param {string } locale - The locale string (e.g.,"en_US", "en-GB").
302+ * @return {string } The normalized locale string.
303+ */
304+
266305export function normalizeLocale ( locale : string ) : string {
267306 return locale . replaceAll ( "_" , "-" ) . replace ( / ( [ a - z ] { 2 , 3 } - ) r / , "$1" ) ;
268307}
0 commit comments