I have "switched" Latvian to simple foo, just not to freak you out. In reality of course there are different words in there. And don't try to ease this tree seeing that the endings for words are almost the same - it is just an coincidence.

If you don't want to count, there is 6 cases in the inner switch (without the #default), only for second one it should accept g and ģ (of course, both cases is OK), every other are only one accepting value.

If those invalid parameters are hard to detect, then it's ok - I can live without that - in that case simply return empty string.

{{#switch:{{lc:{{{1|}}}}}
|foo={{#switch:{{lc:{{{2|}}}}}|n=Foo|g|ģ=Foo|d=Foo|a=Foo|i=Foo|l=Foo|#default=Invalid}}
|foo={{#switch:{{lc:{{{2|}}}}}|n=Foo|g|ģ=Foo|d=Foo|a=Foo|i=Foo|l=Foo|#default=Invalid}}
|foo={{#switch:{{lc:{{{2|}}}}}|n=Foo|g|ģ=Foo|d=Foo|a=Foo|i=Foo|l=Foo|#default=Invalid}}
|foo={{#switch:{{lc:{{{2|}}}}}|n=Foo|g|ģ=Foo|d=Foo|a=Foo|i=Foo|l=Foo|#default=Invalid}}
|foo={{#switch:{{lc:{{{2|}}}}}|n=Foo|g|ģ=Foo|d=Foo|a=Foo|i=Foo|l=Foo|#default=Invalid}}
|foo=....
|foo=....
|foo=....
|foo=....
...here I should be able to make unlimited number of foos ....
|#default=Invalid
}}

And little example, how it looks like in reality

{{#switch:{{lc:{{{1|}}}}}
|1={{#switch:{{lc:{{{2|}}}}}|n=Janvāris|g|ģ=Janvāra|d=Janvārim|a=Janvāri|i=ar Janvāri|l=Janvārī|#default=Invalid}}
|2={{#switch:{{lc:{{{2|}}}}}|n=Februāris|g|ģ=Februāra|d=Februārim|a=Februāri|i=ar Februāri|l=Februārī|#default=Invalid}}
...
}}

So the code in Lua what I have in my non-Lua-coder mind (for those first two rows of example2), is:

{{#invoke:Whatever|main|{{{1|}}}|{{{2|}}}
|1|Janvāris|Janvāra|Janvārim|Janvāri|ar Janvāri|Janvārī
|2|Februāris|Februāra|Februārim|Februāri|ar Februāri|Februārī
...
}}

What would return:

  • {{#invoke:Whatever|main|2|d}} -> Februārim

Some more testcases:

  • {{#invoke:Whatever|main|2|D}} -> Februārim
  • {{#invoke:Whatever|main|2|Ģ}} -> Februāra
  • {{#invoke:Whatever|main|2|ģ}} (just making sure, everything with encoding is ok) -> Februāra
  • {{#invoke:Whatever|main|123|z}} (both are out of range) -> Invalid first and second parameter
  • {{#invoke:Whatever|main|1|z}} (second is out of range) -> Invalid second parameter
  • {{#invoke:Whatever|main|123|n}} (first is out of range) -> Invalid first parameter

Note. I have included {{lc:}} in the outer switch because there can be also alphabetical values, not only numbers, so yes - {{lc:}} is needed.