-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add custom .lang file type parsing, replace .json for localization files
- Loading branch information
Showing
11 changed files
with
157 additions
and
36 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { Language } from './Language'; | ||
|
||
/** | ||
* Class for parsing `.lang` files | ||
* @private | ||
*/ | ||
export class LangFileParser | ||
{ | ||
private static parseBlock: RegExp = /(\[([a-zA-Z0-9_]+)\]([^]+)\[\/\2\])/; | ||
private static parseBlocks: RegExp = new RegExp(LangFileParser.parseBlock, 'g'); | ||
private static stripComments: RegExp = /^(?!$)\s*##.*\n|##.*$/gm; | ||
private static trimNewlines: RegExp = /^\n|\n$/g; | ||
|
||
/** | ||
* Parse a given language file string and return a Language | ||
* object containing all the parsed values | ||
*/ | ||
public static parseFile(langName: string, langFile: string): Language | ||
{ | ||
const lang: Language = new Language(langName); | ||
const blocks: string[] = langFile.match(LangFileParser.parseBlocks); | ||
for (const block of blocks) | ||
{ | ||
const match: RegExpMatchArray = block.match(LangFileParser.parseBlock); | ||
const raw: string = match[1].replace(/\r\n/g, '\n'); | ||
const key: string = match[2]; | ||
const value: string = match[3] | ||
.replace(/\r\n/g, '\n') | ||
.replace(LangFileParser.stripComments, '') | ||
.replace(LangFileParser.trimNewlines, '') | ||
.trim(); | ||
|
||
lang.strings[key] = value; | ||
lang.raw[key] = raw; | ||
} | ||
return lang; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/** | ||
* Holds the localization strings for a given language | ||
* @private | ||
*/ | ||
export class Language | ||
{ | ||
public lang: string; | ||
public strings: { [key: string]: string }; | ||
public raw: { [key: string]: string }; | ||
public constructor(lang: string) | ||
{ | ||
this.lang = lang; | ||
this.strings = {}; | ||
this.raw = {}; | ||
} | ||
|
||
/** | ||
* Concatenate another Language object's strings of the | ||
* same language with this Language object's strings, | ||
* saving them to this Language object's `strings` value | ||
*/ | ||
public concat(lang: Language): void | ||
{ | ||
if (lang.lang !== this.lang) | ||
throw new Error('Cannot concatenate strings for different languages.'); | ||
this.strings = { ...this.strings, ...lang.strings }; | ||
this.raw = { ...this.raw, ...lang.raw }; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
|
||
[CMD_HELP_SERVERONLY] [Server Only] [/CMD_HELP_SERVERONLY] | ||
[CMD_HELP_OWNERONLY] [Owner Only] [/CMD_HELP_OWNERONLY] | ||
[CMD_HELP_ALIASES] Aliases: {{ aliases }} [/CMD_HELP_ALIASES] | ||
|
||
[CMD_HELP_CODEBLOCK] | ||
## I feel ldif is the best codeblock language for | ||
## displaying all of the help commands but it could | ||
## be changed without any consequence if desired | ||
```ldif | ||
{{ serverOnly ?}} | ||
{{ ownerOnly ?}} | ||
Command: {{ commandName }} | ||
Description: {{ desc }} | ||
{{ aliasText ?}} | ||
Usage: {{ usage }} | ||
{{ info ?}} | ||
``` | ||
[/CMD_HELP_CODEBLOCK] |
This file was deleted.
Oops, something went wrong.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"CMD_HELP_SERVERONLY": "[Canjan Uhmo]", | ||
"CMD_HELP_OWNERONLY": "[Ufhan Uhmo]", | ||
"CMD_HELP_ALIASES": "Ymeycac: {{ aliases }}", | ||
"CMD_HELP_CODEBLOCK": "```ldif\n{{serverOnly}}{{ownerOnly}}Lussyht: {{commandName}}\nTaclnebdeuh: {{desc}}\n{{aliasText}}Icyka: {{usage}}\n{{info}}\n```" | ||
} | ||
|
||
[CMD_HELP_SERVERONLY] [Canjan Uhmo] [/CMD_HELP_SERVERONLY] | ||
[CMD_HELP_OWNERONLY] [Ufhan Uhmo] [/CMD_HELP_OWNERONLY] | ||
[CMD_HELP_ALIASES] Ymeycac: {{ aliases }} [/CMD_HELP_ALIASES] | ||
|
||
[CMD_HELP_CODEBLOCK] | ||
```ldif | ||
{{ serverOnly ?}} | ||
{{ ownerOnly ?}} | ||
Lussyht: {{ commandName }} | ||
nTaclnebdeuh: {{ desc }} | ||
{{ aliasText ?}} | ||
Icyka: {{ usage }} | ||
{{ info ?}} | ||
``` | ||
[/CMD_HELP_CODEBLOCK] |
This file was deleted.
Oops, something went wrong.
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