ra-core
Version:
Core components of react-admin, a frontend Framework for building admin applications on top of REST services, using ES6, React
47 lines • 1.91 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.useGetResourceLabel = void 0;
const useResourceDefinitions_1 = require("./useResourceDefinitions.cjs");
const i18n_1 = require("../i18n/index.cjs");
const inflection_1 = require("inflection");
/**
* A hook which returns function to get a translated resource name. It will use the label option of the `Resource` component if it was provided.
*
* @returns {GetResourceLabel} A function which takes a resource name and an optional number indicating the number of items (used for pluralization) and returns a translated string.
* @example
* const Menu = () => {
* const resources = useResourceDefinitions();
* const getResourceLabel = useGetResourceLabel();
*
* return (
* <ul>
* {Object.keys(resources).map(name => (
* <li key={name}>
* {getResourceLabel(name, 2)}
* </li>
* ))}
* </ul>
* )
* }
*/
const useGetResourceLabel = () => {
const translate = (0, i18n_1.useTranslate)();
const definitions = (0, useResourceDefinitions_1.useResourceDefinitions)();
return (resource, count = 2) => {
const resourceDefinition = definitions[resource];
const label = translate(`resources.${resource}.name`, {
smart_count: count,
_: resourceDefinition &&
resourceDefinition.options &&
resourceDefinition.options.label
? translate(resourceDefinition.options.label, {
smart_count: count,
_: resourceDefinition.options.label,
})
: (0, inflection_1.humanize)((0, inflection_1.inflect)(resource, count)),
});
return label;
};
};
exports.useGetResourceLabel = useGetResourceLabel;
//# sourceMappingURL=useGetResourceLabel.js.map