-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from koyopro/feature/i18n
Internationalization (I18n)
- Loading branch information
Showing
25 changed files
with
650 additions
and
38 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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,52 @@ | ||
type i18n = { | ||
t: (key: string, name: string) => string | undefined; | ||
}; | ||
export let i18n: i18n | undefined = undefined; | ||
|
||
/** | ||
* Represents a class for handling naming conventions. | ||
* | ||
* This class is intended to be inherited by the Model class. | ||
*/ | ||
export class Naming { | ||
/** | ||
* Gets the model name. | ||
* @returns An object with a `human` property representing the human-readable model name. | ||
*/ | ||
static get modelName() { | ||
const default_ = this.name; | ||
const key = `accelrecord.models.${this.name}`; | ||
return { | ||
/** | ||
* the human-readable model name. | ||
*/ | ||
get human() { | ||
return i18n?.t(key, "") || default_; | ||
}, | ||
}; | ||
} | ||
|
||
/** | ||
* Gets the human-readable attribute name for the specified attribute. | ||
* @param attribute - The attribute name. | ||
* @returns The human-readable attribute name. | ||
*/ | ||
static humanAttributeName(attribute: string) { | ||
const key = `accelrecord.attributes.${this.name}.${attribute}`; | ||
return i18n?.t(key, "") || toPascalCase(attribute); | ||
} | ||
} | ||
|
||
const toPascalCase = (str: string) => { | ||
return str | ||
.replace(/_./g, (s) => s[1].toUpperCase()) | ||
.replace(/^./, (s) => s.toUpperCase()); | ||
}; | ||
|
||
export const loadI18n = async () => { | ||
try { | ||
i18n = await import("i18next"); | ||
} catch (e) { | ||
// i18next not found | ||
} | ||
}; |
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
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
Oops, something went wrong.