Advertisement
Guest User

Untitled

a guest
Jan 21st, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.16 KB | None | 0 0
  1. UNSOLICITED CODE FROM RANDOM PROJECT WITHOUT EXPLANATION FTW, am i right? :)
  2.  
  3. ---- locstr.lua ----
  4. --[[
  5. StringPool.Add( tableOfStrings )
  6.  
  7. Adds the entries in tableOfStrings to the StringPool.
  8. Format:
  9. tableOfStrings = { ['stringid'] = { ['en'] = 'string', ['zh'] = 'string' ... }, ... }
  10. StringPool.Add( tableOfStrings)
  11.  
  12. locstr( stringId, ... )
  13.  
  14. Looks up a localized string entry from the multi-language string pool,
  15. StringPool, based on the stringId and the current HostLanguage
  16. setting (taken from STRAT_LANGUAGE enviroment variable; default: en).
  17.  
  18. The lookup is basically: StringPool[stringId][HostLanguage][stringId
  19.  
  20. If there is no "stringId" listed under HostLanguage, then it checks
  21. for StringPool[stringId][stringId].
  22.  
  23. If that cannot be found, it will return the string "??HostLanguage/stringId??"
  24.  
  25. Also, for non-translatable strings, it checks for an entry:
  26. StringPool[stringId]
  27.  
  28. When called with additional arguments, assumes that the string
  29. has format options (see http://lua-users.org/wiki/StringLibraryTutorial …
  30. for more details on how to use Lua format options).
  31.  
  32. e.g.
  33.  
  34. StringPool.Add({ ['en'] = { ['string1'] = "Name = %s, Playerid = %u" } })
  35. locstr('string1', "Oliver", 3204)
  36. output:
  37. Name = Oliver, Playerid = 3204
  38.  
  39. Try to use "%u" for numbers you know are unsigned instead of %d (signed)
  40. ]]--
  41.  
  42. StringPool = {}
  43.  
  44. function StringPool.Add( tableOfStrings )
  45. for id, translations in pairs(tableOfStrings)
  46. do
  47. StringPool[id] = translations
  48. end
  49. end
  50.  
  51. function locstr(stringId, arg1, ...)
  52. -- Look up our language
  53. local stringEnt = StringPool[stringId]
  54. if stringEnt == nil then
  55. return error("locstr: StringPool["..stringId.."] does not have entries for '"..HostLanguage.."'")
  56. end
  57.  
  58. -- If stringEnt is itself a string, it is non-translatable
  59. local locString
  60. if type(stringEnt) == 'table' then
  61. if stringEnt[HostLanguage] ~= nil then
  62. locString = stringEnt[HostLanguage]
  63. elseif HostLanguage ~= 'en' and stringEnt['en'] ~= nil then
  64. warn("locstr: Using default 'en' entry for missing "..stringId.."/"..HostLanguage.." string")
  65. locString = stringEnt['en']
  66. else
  67. error("locstr: No string with id " .. stringId)
  68. return "??"..stringId.."/"..HostLanguage.."??"
  69. end
  70. end
  71.  
  72. if arg1 then
  73. return string.format(locString, arg1, ...)
  74. end
  75.  
  76. return locString
  77. end
  78.  
  79. --[[ -- Localization Code Testing
  80.  
  81. StringTableExample =
  82. {
  83. ['test1'] = -- Simple string test
  84. {
  85. ['en'] = "en: Hello World", ['zh'] = "zh: Hello World"
  86. }
  87. ,['test2'] = -- Fall thru test
  88. {
  89. -- Chinese translation mission to test fall thru
  90. ['en'] = "en: Test 2"
  91. }
  92. ,['test3'] = -- Single argument format test
  93. {
  94. ['en'] = "en: Hello, Mr %s", ['zh'] = "zh: Hello, Mr %s"
  95. }
  96. ,['test4'] = -- Multiple argument format test
  97. {
  98. ['en'] = "en: Hello, Mr %s, how are the %u kids?"
  99. ,['zh'] = "en: Hello, Mr %s, how are the %u kids?"
  100. }
  101. }
  102.  
  103. function StringTableTest(id, ...)
  104. print("- "..id.."/"..HostLanguage..": " .. locstr(StringTableExample, id, ...))
  105. end
  106.  
  107. function StringTableTests(language)
  108. HostLanguage = language
  109. StringTableTest("test1")
  110. StringTableTest("test2", "FAIL")
  111. StringTableTest("test3", "Tester")
  112. StringTableTest("test4", "Tested", 5)
  113. pcall(StringTableTest, "test5", "This should fail")
  114. end
  115.  
  116. StringTableTests("en")
  117. StringTableTests("zh")
  118. StringTableTests("xx")
  119. ]]--
  120.  
  121. ----localization.lua----
  122. StringPool.Add({
  123.  
  124. -- Training Facility Names (or numbers)
  125. ['ukTraining'] = { ['en'] = 'British Training', ['zh'] = 'B军训练' }
  126. ,['frTraining'] = { ['en'] = 'French Training', ['zh'] = 'A军训练' }
  127. ,['deTraining'] = { ['en'] = 'German Training', ['zh'] = 'C军训练' }
  128.  
  129. -- The forts at Area 51
  130. ,['ukFort'] = { ['en'] = 'Fort Montgomery', ['zh'] = '孟德利堡垒' }
  131. ,['frFort'] = { ['en'] = 'Fort De Gaulle', ['zh'] = '迪高要塞' }
  132. ,['deFort'] = { ['en'] = 'Fort Rommel', ['zh'] = '罗麦尔要塞' }
  133.  
  134. ----- auto-ao-placement strings.
  135. --
  136.  
  137. ,['aoStats'] =
  138. {
  139. -- Generate stats line, e.g. "5 ALLIED AOs available: 3 placed, 1 spare, 1 required"
  140. ['en'] = "%u %s AOs available: %u placed, %u spare, %u required"
  141. ,['zh'] = "%u 个 %s AO可用: %u 个已部署, %u 个剩余, 必须部署 %u 个。"
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement