Skip to content

Commit 5a41912

Browse files
Allow locale to be a list
This allows an arbitrary-length of fallback chain for the user-requested locale.
1 parent 93f8cbe commit 5a41912

3 files changed

Lines changed: 37 additions & 15 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ When a value is not found, the lib has several fallback mechanisms:
105105

106106
The parents of a locale are found by splitting the locale by its hyphens. Other separation characters (spaces, underscores, etc) are not supported.
107107

108+
You can also pass a list of locales to `setLocale` to provide multiple options before the fallback locale is used.
109+
108110
Using language files
109111
====================
110112

i18n/init.lua

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,11 @@ local function assertFunctionOrNil(functionName, paramName, value)
6464
end
6565

6666
local function defaultPluralizeFunction(count)
67-
return plural.get(variants.root(i18n.getLocale()), count)
67+
local locale = i18n.getLocale()
68+
if type(locale) == "table" then
69+
locale = locale[1]
70+
end
71+
return plural.get(variants.root(locale), count)
6872
end
6973

7074
local function pluralize(t, data)
@@ -110,6 +114,13 @@ local function localizedTranslate(key, loc, data)
110114
return treatNode(node, data)
111115
end
112116

117+
local function concat(arr1, arr2)
118+
for i = 1, #arr2 do
119+
arr1[#arr1 + i] = arr2[i]
120+
end
121+
return arr1
122+
end
123+
113124
-- public interface
114125

115126
function i18n.set(key, value)
@@ -133,9 +144,19 @@ function i18n.translate(key, data)
133144
assertPresent('translate', 'key', key)
134145

135146
data = data or {}
136-
local usedLocale = data.locale or locale
147+
local usedLocales
148+
local locales = locale
149+
if type(locale) == 'string' then
150+
locales = {locale}
151+
end
152+
if isPresent(data.locale) then
153+
usedLocales = concat({data.locale}, locales)
154+
else
155+
usedLocales = concat({}, locales)
156+
end
137157

138-
local fallbacks = variants.fallbacks(usedLocale, fallbackLocale)
158+
table.insert(usedLocales, fallbackLocale)
159+
local fallbacks = variants.fallbacks(usedLocales)
139160
for i=1, #fallbacks do
140161
local value = localizedTranslate(key, fallbacks[i], data)
141162
if value then return value end
@@ -145,7 +166,7 @@ function i18n.translate(key, data)
145166
end
146167

147168
function i18n.setLocale(newLocale, newPluralizeFunction)
148-
assertPresent('setLocale', 'newLocale', newLocale)
169+
assertPresentOrTable('setLocale', 'newLocale', newLocale)
149170
assertFunctionOrNil('setLocale', 'newPluralizeFunction', newPluralizeFunction)
150171
locale = newLocale
151172
pluralizeFunction = newPluralizeFunction or defaultPluralizeFunction

i18n/variants.lua

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,18 @@ function variants.root(locale)
3131
return locale:match("[^%-]+")
3232
end
3333

34-
function variants.fallbacks(locale, fallbackLocale)
35-
if locale == fallbackLocale or
36-
variants.isParent(fallbackLocale, locale) then
37-
return variants.ancestry(locale)
34+
function variants.fallbacks(locales)
35+
local seen = {}
36+
local ancestry, length = {}, 0
37+
for index, value in ipairs(locales) do
38+
if not seen[value] then
39+
seen[value] = true
40+
local ancestry1, length1 = variants.ancestry(value)
41+
ancestry, length = concat(ancestry, length, ancestry1, length1)
42+
end
3843
end
39-
if variants.isParent(locale, fallbackLocale) then
40-
return variants.ancestry(fallbackLocale)
41-
end
42-
43-
local ancestry1, length1 = variants.ancestry(locale)
44-
local ancestry2, length2 = variants.ancestry(fallbackLocale)
4544

46-
return concat(ancestry1, length1, ancestry2, length2)
45+
return ancestry, length
4746
end
4847

4948
return variants

0 commit comments

Comments
 (0)