<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-GB">
	<id>https://witterpedia.thesamsmith.co.uk/index.php?action=history&amp;feed=atom&amp;title=Module%3AI18n</id>
	<title>Module:I18n - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://witterpedia.thesamsmith.co.uk/index.php?action=history&amp;feed=atom&amp;title=Module%3AI18n"/>
	<link rel="alternate" type="text/html" href="https://witterpedia.thesamsmith.co.uk/index.php?title=Module:I18n&amp;action=history"/>
	<updated>2026-04-19T20:58:40Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.0</generator>
	<entry>
		<id>https://witterpedia.thesamsmith.co.uk/index.php?title=Module:I18n&amp;diff=76&amp;oldid=prev</id>
		<title>Fecsmith: 1 revision imported</title>
		<link rel="alternate" type="text/html" href="https://witterpedia.thesamsmith.co.uk/index.php?title=Module:I18n&amp;diff=76&amp;oldid=prev"/>
		<updated>2025-03-10T23:35:09Z</updated>

		<summary type="html">&lt;p&gt;1 revision imported&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en-GB&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 23:35, 10 March 2025&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;4&quot; class=&quot;diff-notice&quot; lang=&quot;en-GB&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(No difference)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;!-- diff cache key witterpedia:diff:1.41:old-12:rev-76 --&gt;
&lt;/table&gt;</summary>
		<author><name>Fecsmith</name></author>
	</entry>
	<entry>
		<id>https://witterpedia.thesamsmith.co.uk/index.php?title=Module:I18n&amp;diff=12&amp;oldid=prev</id>
		<title>Fecsmith: Created page with &quot;require( &#039;strict&#039; )  local i18n = {}  local metatable = {} local methodtable = {}  metatable.__index = methodtable  local libraryUtil = require( &#039;libraryUtil&#039; ) local checkType = libraryUtil.checkType  --- Cache table containing i18n data --- e.g. cache[&#039;en&#039;][&#039;SMW&#039;] will get you the SMW table in English local cache = {}  --- Cache language codes for reuse local languages = {}   --- Retrieve dataset namespace from key prefix --- --- @param key string The translation key -...&quot;</title>
		<link rel="alternate" type="text/html" href="https://witterpedia.thesamsmith.co.uk/index.php?title=Module:I18n&amp;diff=12&amp;oldid=prev"/>
		<updated>2025-03-10T01:16:58Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;require( &amp;#039;strict&amp;#039; )  local i18n = {}  local metatable = {} local methodtable = {}  metatable.__index = methodtable  local libraryUtil = require( &amp;#039;libraryUtil&amp;#039; ) local checkType = libraryUtil.checkType  --- Cache table containing i18n data --- e.g. cache[&amp;#039;en&amp;#039;][&amp;#039;SMW&amp;#039;] will get you the SMW table in English local cache = {}  --- Cache language codes for reuse local languages = {}   --- Retrieve dataset namespace from key prefix --- --- @param key string The translation key -...&amp;quot;&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en-GB&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 01:16, 10 March 2025&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;4&quot; class=&quot;diff-notice&quot; lang=&quot;en-GB&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(No difference)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;!-- diff cache key witterpedia:diff:1.41:old-75:rev-12 --&gt;
&lt;/table&gt;</summary>
		<author><name>Fecsmith</name></author>
	</entry>
	<entry>
		<id>https://witterpedia.thesamsmith.co.uk/index.php?title=Module:I18n&amp;diff=75&amp;oldid=prev</id>
		<title>SCW&gt;Alistair3149 at 06:25, 7 September 2024</title>
		<link rel="alternate" type="text/html" href="https://witterpedia.thesamsmith.co.uk/index.php?title=Module:I18n&amp;diff=75&amp;oldid=prev"/>
		<updated>2024-09-07T06:25:42Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;require( &amp;#039;strict&amp;#039; )&lt;br /&gt;
&lt;br /&gt;
local i18n = {}&lt;br /&gt;
&lt;br /&gt;
local metatable = {}&lt;br /&gt;
local methodtable = {}&lt;br /&gt;
&lt;br /&gt;
metatable.__index = methodtable&lt;br /&gt;
&lt;br /&gt;
local libraryUtil = require( &amp;#039;libraryUtil&amp;#039; )&lt;br /&gt;
local checkType = libraryUtil.checkType&lt;br /&gt;
&lt;br /&gt;
--- Cache table containing i18n data&lt;br /&gt;
--- e.g. cache[&amp;#039;en&amp;#039;][&amp;#039;SMW&amp;#039;] will get you the SMW table in English&lt;br /&gt;
local cache = {}&lt;br /&gt;
&lt;br /&gt;
--- Cache language codes for reuse&lt;br /&gt;
local languages = {}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
--- Retrieve dataset namespace from key prefix&lt;br /&gt;
---&lt;br /&gt;
--- @param key string The translation key&lt;br /&gt;
--- @return string&lt;br /&gt;
local function getNamespace( key )&lt;br /&gt;
    local namespace = string.match( key, &amp;#039;([^_]*)&amp;#039; )&lt;br /&gt;
    return namespace&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
--- Retrieve a list of applicable language codes&lt;br /&gt;
---&lt;br /&gt;
--- @return table&lt;br /&gt;
local function getLanguageCodes()&lt;br /&gt;
    if #languages &amp;gt; 0 then return languages end&lt;br /&gt;
    local mwlang = mw.language.getContentLanguage()&lt;br /&gt;
    local langCodes = { mwlang:getCode() }&lt;br /&gt;
&lt;br /&gt;
    local fallbackLangCodes = mwlang:getFallbackLanguages()&lt;br /&gt;
    if next( fallbackLangCodes ) ~= nil then&lt;br /&gt;
        for _, fallbackLangCode in pairs( fallbackLangCodes ) do&lt;br /&gt;
            table.insert( langCodes, fallbackLangCode )&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    --mw.log( string.format( &amp;#039;🌐 [i18n] Setting language chain: %s&amp;#039;, table.concat( langCodes, &amp;#039;→&amp;#039; ) ) )&lt;br /&gt;
    return langCodes&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
--- Loads a dataset and saves it to the cache&lt;br /&gt;
---&lt;br /&gt;
--- @param lang string&lt;br /&gt;
--- @param namespace string&lt;br /&gt;
--- @return table|nil { data = &amp;quot;The dataset&amp;quot;, keys = &amp;quot;Translation key mapped to index&amp;quot; }&lt;br /&gt;
local function load( lang, namespace )&lt;br /&gt;
    -- Init language cache if it does not exist&lt;br /&gt;
    if cache[ lang ] == nil then&lt;br /&gt;
        cache[ lang ] = {}&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if cache[ lang ][ namespace ] then&lt;br /&gt;
        return cache[ lang ][ namespace ]&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    local datasetName = string.format( &amp;#039;Module:i18n/%s/%s.json&amp;#039;, namespace, lang )&lt;br /&gt;
    local success, data = pcall( mw.loadJsonData, datasetName )&lt;br /&gt;
&lt;br /&gt;
    if not success then&lt;br /&gt;
        --mw.log( string.format( &amp;#039;🚨 [i18n] Loading dataset[%s][%s]: %s not found on wiki&amp;#039;, lang, namespace, datasetName ) )&lt;br /&gt;
        -- Cache the empty result so we do not run mw.loadJsonData again&lt;br /&gt;
        cache[ lang ][ namespace ] = {}&lt;br /&gt;
        return&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    cache[ lang ][ namespace ] = data&lt;br /&gt;
    --mw.log( string.format( &amp;#039;⌛ [i18n] Loading dataset[%s][%s]: %s&amp;#039;, lang, namespace, datasetName ) )&lt;br /&gt;
&lt;br /&gt;
    return cache[ lang ][ namespace ]&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
--- Returns translated message (or key if returnKey is enabled)&lt;br /&gt;
---&lt;br /&gt;
--- @param key string The translation key&lt;br /&gt;
--- @param options table|nil Optional options&lt;br /&gt;
--- @return string|nil&lt;br /&gt;
function methodtable.translate( self, key, options )&lt;br /&gt;
    options = options or {&lt;br /&gt;
        [&amp;#039;returnKey&amp;#039;] = true&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    checkType( &amp;#039;Module:i18n.translate&amp;#039;, 1, self, &amp;#039;table&amp;#039; )&lt;br /&gt;
    checkType( &amp;#039;Module:i18n.translate&amp;#039;, 2, key, &amp;#039;string&amp;#039; )&lt;br /&gt;
    checkType( &amp;#039;Module:i18n.translate&amp;#039;, 3, options, &amp;#039;table&amp;#039; )&lt;br /&gt;
&lt;br /&gt;
    --mw.log( string.format( &amp;#039;🔍 [i18n] Looking for message: %s&amp;#039;, key ) )&lt;br /&gt;
&lt;br /&gt;
    local namespace = getNamespace( key )&lt;br /&gt;
    if namespace == nil then&lt;br /&gt;
        -- No namespace found error&lt;br /&gt;
        --mw.log( string.format( &amp;#039;❌ [i18n] Namespace cannot be found from: %s&amp;#039;, key ) )&lt;br /&gt;
        if options[&amp;#039;returnKey&amp;#039;] == true then&lt;br /&gt;
            return key&lt;br /&gt;
        else&lt;br /&gt;
            return&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    languages = getLanguageCodes()&lt;br /&gt;
&lt;br /&gt;
    local message&lt;br /&gt;
    local i = 1&lt;br /&gt;
&lt;br /&gt;
    while ( message == nil and i &amp;lt;= #languages ) do&lt;br /&gt;
        local dataset = load( languages[ i ], namespace )&lt;br /&gt;
        if dataset then&lt;br /&gt;
            local match = dataset[ key ]&lt;br /&gt;
            if match then&lt;br /&gt;
                message = match&lt;br /&gt;
                --mw.log( string.format( &amp;#039;✅ [i18n] Found message: %s&amp;#039;, message ) )&lt;br /&gt;
            end&lt;br /&gt;
        end&lt;br /&gt;
        i = i + 1&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if message == nil then&lt;br /&gt;
        --mw.log( string.format( &amp;#039;❌ [i18n] Could not found message: %s&amp;#039;, key ) )&lt;br /&gt;
        if options[&amp;#039;returnKey&amp;#039;] == true then&lt;br /&gt;
            message = key&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    return message&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
--- New Instance&lt;br /&gt;
---&lt;br /&gt;
--- @return table i18n&lt;br /&gt;
function i18n.new( self )&lt;br /&gt;
    local instance = {}&lt;br /&gt;
&lt;br /&gt;
    setmetatable( instance, metatable )&lt;br /&gt;
&lt;br /&gt;
    return instance&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
return i18n&lt;/div&gt;</summary>
		<author><name>SCW&gt;Alistair3149</name></author>
	</entry>
</feed>