Skip to content

Instantly share code, notes, and snippets.

@erinlin
Last active September 22, 2015 04:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erinlin/56d8e8dbc4ddfe22ff4b to your computer and use it in GitHub Desktop.
Save erinlin/56d8e8dbc4ddfe22ff4b to your computer and use it in GitHub Desktop.
CoronaSDK-多國語 i18n strings, 整合 Android/iOS 中文繁體與簡體支援
-------------------------------------
-- i18n
-- Copyright (c) 2015 Erin Lin
-- erinylin.com
-- Licensed under the MIT license.
-------------------------------------
--[[
All strings files are under a folder names "locale"
Internationalization string file format:
Default
locale/strings.i18n
{
"hello" : "Hello",
"welcome" : "Welcome"
}
Traditional Chinese : zh_hant
locale/strings_zh_hant.i18n
{
"hello" : "哈囉",
"welcome" : "歡迎"
}
Simplified Chinese : zh_hans
locale/strings_zh_hans.i18n
{
"hello" : "哈啰",
"welcome" : "欢迎"
}
-------------------------------------
Usage:
-------------------------------------
local i18n = require( "i18n" )
-- set device language -> locale/strings_[device setting].i18n
i18n.setLocale()
-- set default language, -> locale/strings.i18n
i18n.setLocale("")
-- set specific language
i18n.setLocale("zh_hant") -> locale/strings_zh_hant.i18n
-- golbal localization string function L( str )
print( "hello", L("hello") )
print( "Current language:", i18n.language )
--]]
-------------------------------------
local json = require("json")
local M = {}
local string_gsub = string.gsub
local string_format = string.format
local string_find = string.find
function string_split(self, sep)
local sep, fields = sep or ":", {}
local pattern = string_format("([^%s]+)", sep)
string_gsub(self, pattern, function(c) fields[#fields+1] = c end)
return fields
end
local i18n = (function()
local I18N = {}
local I18N_mt = { __index = I18N }
local function loadTable(filename, dir)
local path = system.pathForFile( filename, dir or system.DocumentsDirectory)
if path then
local file = io.open( path, "r" )
if file then
local contents = file:read( "*a" )
io.close( file )
return (json.decode(contents))
end
end
print(filename, "file not found")
return nil
end
function I18N.new(locale, dir) -- The constructor
local resource = "strings"
local dir = dir or system.ResourceDirectory
local object = {
language = locale or nil,
strings = {}
}
local isSimulator = system.getInfo('environment') == 'simulator'
local function formatString( str )
str = string_gsub( str:lower(), "-", "_" )
local temp = string_split(str,"_")
str = temp[1]
-- 中文同步 繁體: zh_hant, 簡體:zh_hans
-- iOS9 fixed
local temp2 = temp[2]
if temp2=="tw" or temp2=="hk" then
str = str.."_hant"
elseif temp2=="cn" then
str = str.."_hans"
else
str = str.."_"..temp2
end
return str
end
if object.language==nil then
object.language = ""
if system.getInfo('platformName')=="iPhone OS" or isSimulator then
locale = system.getPreference( "ui", "language" )
else
locale = system.getPreference( "locale", "identifier" )
if not string_find( locale , "zh_" ) then
locale = system.getPreference( "locale", "language" )
end
end
locale = formatString( locale )
object.language = locale
end
if locale=="default" then object.language="" end
local files = {
"locale/"..resource..".i18n",
"locale/"..resource.."_"..object.language..".i18n"
}
local lang = locale
local strmap = {}
local origin = loadTable( files[1], dir )
assert(origin, "Default locale/strings.i18n file must be provide.")
local temp = loadTable( files[2], dir )
if temp then lang = object.language end
strmap = temp or strmap
-- fill in default value
for k,v in next, (origin) do
if not strmap[k] then
strmap[k]=v
end
end
object.strings = strmap
return setmetatable( object, I18N_mt ), (lang=="" and "default" or lang)
end
function I18N:getString(key)
return self.strings[key]
end
return I18N
end)()
M.setLocale = function( ... )
local _i18n
_i18n, M.language = i18n.new(... )
_G.L = function(str)
return _i18n:getString(str) or str
end
return M.language
end
return M
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment