Faça sua pergunta e obtenha um resumo do documento referenciando esta página e o provedor AI de sua escolha
Este documento está desatualizado, a versão base foi atualizada em 23 de agosto de 2025.
Ir para a documentação em inglêsHistórico de versões
- "Histórico inicial"v5.5.1029/06/2025
O conteúdo desta página foi traduzido com uma IA.
Veja a última versão do conteúdo original em inglêsSe você tiver uma ideia para melhorar esta documentação, sinta-se à vontade para contribuir enviando uma pull request no GitHub.
Link do GitHub para a documentaçãoCopiar o Markdown do documento para a área de transferência
Enumeração / Pluralização
Como a Enumeração Funciona
No Intlayer, a enumeração é realizada através da função enu, que mapeia chaves específicas para seu conteúdo correspondente. Essas chaves podem representar valores numéricos, intervalos ou identificadores personalizados. Quando usada com React Intlayer ou Next Intlayer, o conteúdo apropriado é selecionado automaticamente com base na localidade da aplicação e nas regras definidas.
Configurando a Enumeração
Para configurar a enumeração no seu projeto Intlayer, você precisa criar um módulo de conteúdo que inclua definições de enumeração. Aqui está um exemplo de uma enumeração simples para o número de carros:
Copiar o código para a área de transferência
import { enu, type Dictionary } from "intlayer";
const carEnumeration = {
key: "car_count",
content: {
numberOfCar: enu({
"<-1": "Menos que menos um carro",
"-1": "Menos um carro",
"0": "Nenhum carro",
"1": "Um carro",
">5": "Alguns carros",
">19": "Muitos carros",
"fallback": "Valor padrão", // Opcional
}),
},
} satisfies Dictionary;
export default carEnumeration;
Neste exemplo, enu mapeia várias condições para conteúdos específicos. Quando usado em um componente React, o Intlayer pode automaticamente escolher o conteúdo apropriado com base na variável fornecida.
A ordem de declaração é importante nas enumerações do Intlayer. A primeira declaração válida é a que será selecionada. Se múltiplas condições se aplicarem, certifique-se de que estão ordenadas corretamente para evitar comportamentos inesperados.
Se nenhum valor padrão (fallback) for declarado, a função retornará undefined caso nenhuma chave corresponda.
Usando Enumeração com React Intlayer
To use enumeration in a React component, you can leverage the useIntlayer hook from the react-intlayer package. This hook retrieves the correct content based on the specified ID. Here's an example of how to use it:
Copiar o código para a área de transferência
import type { FC } from "react";
import { useIntlayer } from "react-intlayer";
const CarComponent: FC = () => {
const { numberOfCar } = useIntlayer("car_count");
return (
<div>
<p>
{
numberOfCar(0) // Output: No cars
}
</p>
<p>
{
numberOfCar(6) // Output: Some cars
}
</p>
<p>
{
numberOfCar(20) // Output: Many cars
}
</p>
<p>
{
numberOfCar(0.01) // Output: Fallback value
}
</p>
</div>
);
};
To use enumeration in Next.js Client Components, retrieve it via the useIntlayer hook. Here's an example:
Copiar o código para a área de transferência
"use client";
import type { FC } from "react";
import { useIntlayer } from "next-intlayer";
const CarComponent: FC = () => {
const { numberOfCar } = useIntlayer("car_count");
return (
<div>
<p>{numberOfCar(6)}</p>
</div>
);
};
export default CarComponent;
To use enumeration in Vue components, retrieve it via the useIntlayer hook. Here's an example:
Copiar o código para a área de transferência
To use enumeration in Svelte components, retrieve it via the useIntlayer hook. The store is accessed with $. Here's an example:
Copiar o código para a área de transferência
To use enumeration in Preact components, retrieve it via the useIntlayer hook. Here's an example:
Copiar o código para a área de transferência
import type { FC } from "preact";
import { useIntlayer } from "preact-intlayer";
const CarComponent: FC = () => {
const { numberOfCar } = useIntlayer("car_count");
return (
<div>
<p>{numberOfCar(6)}</p>
</div>
);
};
export default CarComponent;
To use enumeration in SolidJS components, retrieve it via the useIntlayer hook. Here's an example:
Copiar o código para a área de transferência
import type { Component } from "solid-js";
import { useIntlayer } from "solid-intlayer";
const CarComponent: Component = () => {
const { numberOfCar } = useIntlayer("car_count");
return (
<div>
<p>{numberOfCar(6)}</p>
</div>
);
};
export default CarComponent;
To use enumeration in Angular components, retrieve it via the useIntlayer hook. Here's an example:
Copiar o código para a área de transferência
To use enumeration with vanilla-intlayer, retrieve it via the useIntlayer hook. Here's an example:
Copiar o código para a área de transferência
import { installIntlayer, useIntlayer } from "vanilla-intlayer";
installIntlayer();
const content = useIntlayer("car_count").onChange((newContent) => {
document.getElementById("cars")!.textContent = newContent.numberOfCar(6);
});
// Initial render
document.getElementById("cars")!.textContent = content.numberOfCar(6);
Recursos Adicionais
Para informações mais detalhadas sobre configuração e uso, consulte os seguintes recursos:
Estes recursos fornecem mais informações sobre a configuração e uso do Intlayer em diferentes ambientes e com vários frameworks.
Using Ordinal Enumeration
To use this in a React component, call the enumeration with the last digit of the number to get the correct suffix, then pass the full count as the insertion value:
Copiar o código para a área de transferência
import type { FC } from "react";
import { useIntlayer } from "react-intlayer";
const RankingComponent: FC<{ count: number }> = ({ count }) => {
const { ordinal } = useIntlayer("ranking_component");
// Get the last digit to determine the correct suffix
const lastDigit = Math.abs(count) % 10;
return (
<div>
<p>
{
ordinal(lastDigit)({ count }) // e.g., "5th place" for count=5
}
</p>
</div>
);
};
To use this in Next.js Client Components, call the enumeration with the last digit of the number to get the correct suffix, then pass the full count as the insertion value:
Copiar o código para a área de transferência
"use client";
import type { FC } from "react";
import { useIntlayer } from "next-intlayer";
const RankingComponent: FC<{ count: number }> = ({ count }) => {
const { ordinal } = useIntlayer("ranking_component");
const lastDigit = Math.abs(count) % 10;
return (
<div>
<p>{ordinal(lastDigit)({ count })}</p>
</div>
);
};
export default RankingComponent;
To use this in Vue components, call the enumeration with the last digit of the number to get the correct suffix, then pass the full count as the insertion value:
Copiar o código para a área de transferência
To use this in Svelte components, call the enumeration with the last digit of the number to get the correct suffix, then pass the full count as the insertion value:
Copiar o código para a área de transferência
To use this in Preact components, call the enumeration with the last digit of the number to get the correct suffix, then pass the full count as the insertion value:
Copiar o código para a área de transferência
import type { FC } from "preact";
import { useIntlayer } from "preact-intlayer";
const RankingComponent: FC<{ count: number }> = ({ count }) => {
const { ordinal } = useIntlayer("ranking_component");
const lastDigit = Math.abs(count) % 10;
return (
<div>
<p>{ordinal(lastDigit)({ count })}</p>
</div>
);
};
export default RankingComponent;
To use this in SolidJS components, call the enumeration with the last digit of the number to get the correct suffix, then pass the full count as the insertion value:
Copiar o código para a área de transferência
import type { Component } from "solid-js";
import { useIntlayer } from "solid-intlayer";
const RankingComponent: Component<{ count: number }> = (props) => {
const { ordinal } = useIntlayer("ranking_component");
return (
<div>
<p>{ordinal(Math.abs(props.count) % 10)({ count: props.count })}</p>
</div>
);
};
export default RankingComponent;
To use this in Angular components, call the enumeration with the last digit of the number to get the correct suffix, then pass the full count as the insertion value:
Copiar o código para a área de transferência
To use this with vanilla-intlayer, call the enumeration with the last digit of the number to get the correct suffix, then pass the full count as the insertion value:
Copiar o código para a área de transferência
import { installIntlayer, useIntlayer } from "vanilla-intlayer";
installIntlayer();
const content = useIntlayer("ranking_component");
const lastDigit = Math.abs(5) % 10;
document.getElementById("ranking")!.textContent = content.ordinal(lastDigit)({
count: 5,
});
Additional Resources
For more detailed information on configuration and usage, refer to the following resources:
These resources provide further insights into the setup and usage of Intlayer in different environments and with various frameworks.
