-
Notifications
You must be signed in to change notification settings - Fork 12
Dictionary system | WIP #2
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
Dictionary = class(ValueStorage) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should refactor all this code like this:
I see it working like this:
Now we can do There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Didn't better will be to create language enums and phrases in different files (not in class)?(one for each language in special directory) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will at least make it easier to add phrases in different languages to the repository. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes - it would be good to put all the phrases for each language in its own file. You could create a directory in napi/shared and come up with a way to do this. Then, you could use that data to set-up the Also, we are not creating the enums in the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also such system where all is Enum it's a wildness for me if we want to make it simple to use by develoler in his own gamemode. If languages are initialized in only one place, it creates certain limitations. Russian:Phrase("greetings", "Добро пожаловать!") -- config.lua it seems to be the most simplified and dev-friendly variant. If SetLanguage not exists then we take English Language, if phrase not exists then we took it in English. |
||
|
||
function Dictionary:__init() | ||
self:InitializeValueStorage(self) | ||
|
||
self:SetValue("current_language", "en") | ||
end | ||
|
||
function Dictionary:AddLanguage(lang) | ||
self:SetValue(lang, {}) | ||
end | ||
|
||
function Dictionary:AddPhrase(lang, id, phrase) | ||
local phrases = self:GetValue(lang) | ||
if not phrases then | ||
print("No language found with id " .. lang) | ||
end | ||
|
||
phrases[id] = phrase | ||
|
||
self:SetValue(lang, phrases) | ||
end | ||
|
||
function Dictionary:SetLanguage(lang) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we don't expect the current language to be changed during runtime, lets move this to the global config so it's easily configured.
Now we can access it with |
||
self:SetValue("current_language", lang) | ||
end | ||
|
||
function Dictionary:GetLanguage() | ||
return self:GetValue("current_language") | ||
end | ||
|
||
function Dictionary:GetPhrase(language, id) | ||
if not id then | ||
id = language | ||
language = self:GetLanguage() | ||
end | ||
|
||
local phrases = self:GetValue(language) | ||
if not phrases then | ||
print("Trying to get phrase on unlisted language") | ||
return | ||
end | ||
|
||
local phrase = phrases[id] | ||
if not phrase then | ||
print("Trying to get phrase by wrong identifier") | ||
return | ||
end | ||
|
||
return phrase | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't forget to initialize the singleton at the end of the file, with Dictionary = Dictionary() (applies to any/all singletons) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's no need to inherit ValueStorage for Dictionary since all we're doing with it is storing some data in this class. You could just have a
self.current_language
variable in this class which would be more readable and simple. We use ValueStorage for in-game entities for easily setting and getting data on them.