Page MenuHomePhabricator

Suggestion for new Lua function in mw.language library: language encoding
Closed, DeclinedPublic

Description

As @Amire80 asked me, I'd like to report about a problem and to suggest a solution.
I'm working on non English version of CS1 module, and I need to get a language code from a language name.
For example, the input is "Spanish" and the output should be "es". For now I written a table with 54 codes and made a loop on it, until the right code was found. The new function could help, and it's program code should be very simple.
Thank you.

Event Timeline

IKhitron created this task.Oct 28 2015, 4:11 PM
IKhitron updated the task description. (Show Details)
IKhitron raised the priority of this task from to Needs Triage.
IKhitron added subscribers: IKhitron, Amire80.
Restricted Application added a subscriber: Aklapper. · View Herald TranscriptOct 28 2015, 4:11 PM

Which extension is this about? I assume MediaWiki-extensions-Scribunto? Adding that project so the task would be found on workboards/when searching that project; if I'm wrong please edit the task.

I believe you are right.

Anomie triaged this task as Low priority.Oct 28 2015, 6:51 PM
Anomie added a subscriber: Anomie.

I note that mw.language.fetchLanguageNames() already returns the code-to-name mapping table for all known codes, so it would be easy enough to write a function to do this in a module.

Exactly, as I did. All I ask is a feature that does not make me to mention all the possible codes in each module.

Why would you have to mention all the possible codes in each module?

A simple version of your requested function would be

function langCodeFromName( name )
    for langCode, langName in pairs( mw.language.fetchLanguageNames( 'en' ) ) do
        if langName == name then
            return langCode
        end
    end
    return nil
end

No list of all possible codes in there.

If it's going to be used many times in a page and always using the English names, you could create a data-module like

local t = {}
for k, v in pairs( mw.language.fetchLanguageNames( 'en' ) ) do
    t[v] = k
end
return t

then just have your function be return mw.loadData( "Module:Whatever" )[name].

Because I didn't know there is a possibility to get this table. I missed this point in the manual. Thanks a lot. But still I prefer this task will stay on low priority so once it will be added to the interface.

Aklapper lowered the priority of this task from Low to Lowest.Oct 29 2015, 12:32 AM

I don't really see a need to add a built-in for something that it's trivial to write a module for.

Aklapper closed this task as Declined.Oct 29 2015, 12:21 PM
Aklapper claimed this task.

Declining as per T116925#1764622 and T116925#1762979