Last active
August 7, 2022 14:13
-
-
Save Rubgrsch/8d0f7af369533f15a7102597447ae0c0 to your computer and use it in GitHub Desktop.
Find unused localeKeys in your WoW addon
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- LocaleKeysCleanUp.lua | |
-- Author: Rubgrsch | |
-- License: MIT | |
-- This code helps you find unused localeKeys in your WoW addon. | |
-- What may cause false positive | |
-- 1. keys in comments | |
-- 2. keys in several lines | |
-- 3. keys doesn't match any patterns, e.g. L[ThisIsVariable] | |
-- Notes: | |
-- 1. Filenames in output only suggest one file that contains it. You will need to search in files. | |
-- 2. The code is for Windows OS. You can find OS specific code with searching "io.popen" | |
-- 3. The code will not find redundant keys among locale files. | |
local strfind, pairs, ipairs, strmatch, print = string.find, pairs, ipairs, string.match, print | |
-- codePath is the dir (or file) of your addon. localePathT is a array of locale files or dirs. | |
-- exceptionPathT is a array of files or dirs that should't be processed (e.g. Libraries). | |
-- localePatternT is a array of regex patterns that match your locale keys. | |
-- You should use absolute path. | |
local codePath = [[path\to\elvui-master]] | |
local localePathT = {[[path\to\elvui-master\ElvUI_Config\locales]],[[path\to\elvui-master\ElvUI\locales]]} | |
local exceptionPathT = {[[path\to\elvui-master\ElvUI_Config\Libraries]],[[path\to\elvui-master\ElvUI\Libraries]]} | |
local localePatternT = {'L%[%".-%"%]', "L%[%'.-%'%]"} | |
localePathT = localePathT or {} | |
local codefilenameT = {} -- filenames of lua source files, [filename] = true | |
for filename in io.popen([[dir ]]..codePath..[[ /b /s /a-d]]):lines() do -- Change this if you're not on Windows | |
if(strfind(filename,"%.lua$")) then codefilenameT[filename] = true end | |
end | |
local localefilenameT = {} -- filenames of locale files, [filename] = true | |
for _,path in ipairs(localePathT) do | |
for filename in io.popen([[dir ]]..path..[[ /b /s /a-d]]):lines() do -- Change this if you're not on Windows | |
if(strfind(filename,"%.lua$")) then localefilenameT[filename] = true end | |
end | |
end | |
-- Remove exceptions | |
if exceptionPathT and type(exceptionPathT) == "table" and (exceptionPathT) ~= {} then | |
for _,exceptionName in ipairs(exceptionPathT) do | |
for filename in pairs(codefilenameT) do | |
if(strfind(filename,exceptionName,1,true)) then codefilenameT[filename] = nil end | |
end | |
for filename in pairs(localefilenameT) do | |
if(strfind(filename,exceptionName,1,true)) then localefilenameT[filename] = nil end | |
end | |
end | |
end | |
-- Remove locale files in lua source file | |
for _,exceptionName in ipairs(localePathT) do | |
for filename in pairs(codefilenameT) do | |
if(strfind(filename,exceptionName,1,true)) then codefilenameT[filename] = nil end | |
end | |
end | |
-- Find locale keys in lua source files | |
local codeKey = {} -- locale keys found in lua source files, [key] = filename | |
for filename in pairs(codefilenameT) do | |
local file = io.open(filename, "r") | |
for line in file:lines() do | |
-- Multi locales can exist in the same line, use a loop instead | |
local left,right,flag,result = 1 | |
repeat | |
flag = 0 | |
for _,pattern in ipairs(localePatternT) do | |
result = strfind(line,pattern,left) | |
if(result) then | |
left,right = strfind(line,pattern,left) | |
codeKey[strmatch(line,pattern,left)] = filename | |
left = right + 1 | |
flag = 1 | |
break | |
end | |
end | |
until flag == 0 | |
end | |
file:close() | |
end | |
-- Find locale keys in locale files | |
local localeKey = {} -- locale keys found in locale files, [key] = filename | |
for filename in pairs(localefilenameT) do | |
local file = io.open(filename, "r") | |
for line in file:lines() do | |
-- Multi locales can exist in the same line, use a loop instead | |
local left,right,flag,result = 1 | |
repeat | |
flag = 0 | |
for _,pattern in ipairs(localePatternT) do | |
result = strfind(line,pattern,left) | |
if(result) then | |
left,right = strfind(line,pattern,left) | |
localeKey[strmatch(line,pattern,left)] = filename | |
left = right + 1 | |
flag = 1 | |
break | |
end | |
end | |
until flag == 0 | |
end | |
file:close() | |
end | |
-- Output | |
local count = 0 | |
for key,filename in pairs(codeKey) do | |
if(not localeKey[key]) then | |
print("Unlocalized Key \t"..key.."\t in code file: "..filename) | |
count = count + 1 | |
end | |
end | |
for key,filename in pairs(localeKey) do | |
if(not codeKey[key]) then | |
print("Unused Key \t"..key.."\t in locale file: "..filename) | |
count = count + 1 | |
end | |
end | |
if count == 0 then | |
print("No unused key found!") | |
else | |
print("Found "..count.."!") | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
find a key ,to use it local
^GET