Skip to content
This repository has been archived by the owner on Oct 27, 2022. It is now read-only.

Commit

Permalink
Merge pull request #54 from Nats-ji/i18n-support
Browse files Browse the repository at this point in the history
I18n support
  • Loading branch information
Mingming Cui authored Jan 26, 2021
2 parents cb3d68b + 3f28f15 commit 2caae94
Show file tree
Hide file tree
Showing 15 changed files with 3,642 additions and 235 deletions.
83 changes: 78 additions & 5 deletions mods/braindance_protocol/CPStyling.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,19 @@
-- SOFTWARE.

local CPStyle = {}
local currentFilePath = (...):gsub("CPStyling$","")

local function isModuleAvailable(module)
res = pcall(require,module)
if res then return true
elseif not(res) then
return false
end
end

if isModuleAvailable(currentFilePath.."png-lua/png") then
png = require(currentFilePath.."png-lua/png")
end

CPStyle.theme = {
Text = { 1.00, 0.38, 0.33, 1.00 },
Expand Down Expand Up @@ -77,8 +90,8 @@ CPStyle.theme = {
-- NavHighlight = { 0.00, 0.00, 0.00, 0.00 },
-- NavWindowingHighlight = { 0.00, 0.00, 0.00, 0.00 },
-- NavWindowingDimBg = { 0.00, 0.00, 0.00, 0.00 },
-- ModalWindowDimBg = { 0.00, 0.00, 0.00, 0.00 },
-- ModalWindowDarkening = { 0.00, 0.00, 0.00, 0.00 },
ModalWindowDimBg = { 0.00, 0.00, 0.00, 0.00 },
ModalWindowDarkening = { 0.00, 0.00, 0.00, 0.40 },
CPButton = { 0.06, 0.06, 0.12, 1.00 },
CPButtonHovered = { 0.43, 0.13, 0.13, 1.00 },
CPButtonActive = { 0.57, 0.16, 0.16, 1.00 },
Expand Down Expand Up @@ -343,15 +356,15 @@ function CPStyle.setThemeBegin()
-- CPStyle.colorBegin("NavHighlight" , CPStyle.theme.NavHighlight)
-- CPStyle.colorBegin("NavWindowingHighlight" , CPStyle.theme.NavWindowingHighlight)
-- CPStyle.colorBegin("NavWindowingDimBg" , CPStyle.theme.NavWindowingDimBg)
-- CPStyle.colorBegin("ModalWindowDimBg" , CPStyle.theme.ModalWindowDimBg)
-- CPStyle.colorBegin("ModalWindowDarkening" , CPStyle.theme.ModalWindowDarkening)
CPStyle.colorBegin("ModalWindowDimBg" , CPStyle.theme.ModalWindowDimBg)
CPStyle.colorBegin("ModalWindowDarkening" , CPStyle.theme.ModalWindowDarkening)
CPStyle.styleBegin("WindowRounding" , 0)
CPStyle.styleBegin("ScrollbarSize" , 9)
end

function CPStyle.setThemeEnd()
CPStyle.styleEnd(2)
CPStyle.colorEnd(39)
CPStyle.colorEnd(41)
end

function CPStyle.setFrameThemeBegin()
Expand Down Expand Up @@ -571,5 +584,65 @@ function CPStyle.CPRect(label, sizex, sizey, color, border_color, border_size, b
return press
end

function CPStyle.CPRect2(label, sizex, sizey, color)
CPStyle.colorBegin("ChildBg", color)
ImGui.BeginChild(label, sizex, sizey)
ImGui.EndChild()
CPStyle.colorEnd(1)
end

function CPStyle.CPDraw(name, image, scale)
ImGui.BeginGroup()
local basex, basey = ImGui.GetCursorPos()
local pixelx = 1
local pixely = 1
local cursorx = basex
local cursory = basey
local totalPixel = image.width*image.height
for i = 1, totalPixel do
ImGui.SetCursorPos(cursorx, cursory)
if image.pixels[pixely][pixelx][4] ~= 0 then
CPStyle.CPRect2("##"..name..i, scale, scale, image.pixels[pixely][pixelx])
end
pixelx = pixelx + 1
if pixelx > image.width then pixelx = 1 pixely = pixely + 1 end
cursorx = basex+(pixelx-1)*scale
cursory = basey+(pixely-1)*scale
end
ImGui.EndGroup()
end

function CPStyle.loadPNG(imagepath)
local imgraw = png(imagepath)
local img = {}
local x = {}
local y = {}
img.width = imgraw.width
img.height = imgraw.height
for i in pairs(imgraw.pixels) do
for t in pairs(imgraw.pixels[i]) do
y[t] = { imgraw.pixels[i][t].R/255, imgraw.pixels[i][t].G/255, imgraw.pixels[i][t].B/255, imgraw.pixels[i][t].A/255 }
end
x[i] = y
y = {}
end
img.pixels = x
return img
end

function CPStyle.fileExists(filename)
local f=io.open(filename,"r")
if (f~=nil) then io.close(f) return true else return false end
end

function CPStyle.getCWD(mod_name)
if CPStyle.fileExists("./bin/x64/plugins/cyber_engine_tweaks/mods/"..mod_name.."/init.lua") then
return "./bin/x64/plugins/cyber_engine_tweaks/mods/"..mod_name.."/"
elseif CPStyle.fileExists("./plugins/cyber_engine_tweaks/mods/"..mod_name.."/init.lua") then
return "./plugins/cyber_engine_tweaks/mods/"..mod_name.."/"
elseif CPStyle.fileExists("./"..mod_name.."/init.lua") then
return "./"..mod_name.."/"
end
end

return CPStyle
22 changes: 22 additions & 0 deletions mods/braindance_protocol/i18n/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
MIT License Terms
=================

Copyright (c) 2012 Enrique García Cota.

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
166 changes: 166 additions & 0 deletions mods/braindance_protocol/i18n/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
Forked from https://github.com/kikito/i18n.lua

i18n.lua
========

[![Build Status](https://travis-ci.org/kikito/i18n.lua.png?branch=master)](https://travis-ci.org/kikito/i18n.lua)

A very complete i18n lib for Lua

Description
===========

``` lua
i18n = require 'i18n'

-- loading stuff
i18n.set('en.welcome', 'welcome to this program')
i18n.load({
en = {
good_bye = "good-bye!",
age_msg = "your age is %{age}.",
phone_msg = {
one = "you have one new message.",
other = "you have %{count} new messages."
}
}
})
i18n.loadFile('path/to/your/project/i18n/de.lua') -- load German language file
i18n.loadFile('path/to/your/project/i18n/fr.lua') -- load French language file
-- section 'using language files' below describes structure of files

-- setting the translation context
i18n.setLocale('en') -- English is the default locale anyway

-- getting translations
i18n.translate('welcome') -- Welcome to this program
i18n('welcome') -- Welcome to this program
i18n('age_msg', {age = 18}) -- Your age is 18.
i18n('phone_msg', {count = 1}) -- You have one new message.
i18n('phone_msg', {count = 2}) -- You have 2 new messages.
i18n('good_bye') -- Good-bye!

```

Interpolation
=============

You can interpolate variables in 3 different ways:

``` lua
-- the most usual one
i18n.set('variables', 'Interpolating variables: %{name} %{age}')
i18n('variables', {name='john', 'age'=10}) -- Interpolating variables: john 10

i18n.set('lua', 'Traditional Lua way: %d %s')
i18n('lua', {1, 'message'}) -- Traditional Lua way: 1 message

i18n.set('combined', 'Combined: %<name>.q %<age>.d %<age>.o')
i18n('combined', {name='john', 'age'=10}) -- Combined: john 10 12k
```



Pluralization
=============

This lib implements the [unicode.org plural rules](http://cldr.unicode.org/index/cldr-spec/plural-rules). Just set the locale you want to use and it will deduce the appropiate pluralization rules:

``` lua
i18n = require 'i18n'

i18n.load({
en = {
msg = {
one = "one message",
other = "%{count} messages"
}
},
ru = {
msg = {
one = "1 сообщение",
few = "%{count} сообщения",
many = "%{count} сообщений",
other = "%{count} сообщения"
}
}
})

i18n('msg', {count = 1}) -- one message
i18n.setLocale('ru')
i18n('msg', {count = 5}) -- 5 сообщений
```

The appropiate rule is chosen by finding the 'root' of the locale used: for example if the current locale is 'fr-CA', the 'fr' rules will be applied.

If the provided functions are not enough (i.e. invented languages) it's possible to specify a custom pluralization function in the second parameter of setLocale. This function must return 'one', 'few', 'other', etc given a number.

Fallbacks
=========

When a value is not found, the lib has several fallback mechanisms:

* First, it will look in the current locale's parents. For example, if the locale was set to 'en-US' and the key 'msg' was not found there, it will be looked over in 'en'.
* Second, if the value is not found in the locale ancestry, a 'fallback locale' (by default: 'en') can be used. If the fallback locale has any parents, they will be looked over too.
* Third, if all the locales have failed, but there is a param called 'default' on the provided data, it will be used.
* Otherwise the translation will return nil.

The parents of a locale are found by splitting the locale by its hyphens. Other separation characters (spaces, underscores, etc) are not supported.

Using language files
====================

It might be a good idea to store each translation in a different file. This is supported via the 'i18n.loadFile' directive:

``` lua
i18n.loadFile('path/to/your/project/i18n/de.lua') -- German translation
i18n.loadFile('path/to/your/project/i18n/en.lua') -- English translation
i18n.loadFile('path/to/your/project/i18n/fr.lua') -- French translation
```

The German language file 'de.lua' should read:

``` lua
return {
de = {
good_bye = "Auf Wiedersehen!",
age_msg = "Ihr Alter beträgt %{age}.",
phone_msg = {
one = "Sie haben eine neue Nachricht.",
other = "Sie haben %{count} neue Nachrichten."
}
}
}
```

If desired, you can also store all translations in one single file (eg. 'translations.lua'):

``` lua
return {
de = {
good_bye = "Auf Wiedersehen!",
age_msg = "Ihr Alter beträgt %{age}.",
phone_msg = {
one = "Sie haben eine neue Nachricht.",
other = "Sie haben %{count} neue Nachrichten."
}
},
fr = {
good_bye = "Au revoir !",
age_msg = "Vous avez %{age} ans.",
phone_msg = {
one = "Vous avez une noveau message.",
other = "Vous avez %{count} noveaux messages."
}
},
}
```

Specs
=====
This project uses [busted](https://github.com/Olivine-Labs/busted) for its specs. If you want to run the specs, you will have to install it first. Then just execute the following from the root inspect folder:

busted
Loading

0 comments on commit 2caae94

Please sign in to comment.