Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for a Custom Hook that takes care of capitalization of the translated strings. #1163

Open
Wolk9 opened this issue Jun 17, 2024 · 0 comments

Comments

@Wolk9
Copy link

Wolk9 commented Jun 17, 2024

Is your feature related to a specific framework or general for this extension
Probably this request relates to other frameworks as well, but we use React-Native.

Is your feature request related to a problem? Please describe.
No, not a problem, but an enhancement

Describe the solution you'd like
We are using a custom hook that takes care of the capitalization of the translated string. Depending on the used hook-function it will capitalize the string accordingly. See the added code of the hook so you will understand the purpose and working.
It would be great if i18n-ally supports the use of a custom hook like this, or maybe even better, that this hook/functionality is embeded in i18n-ally itself as a new feature.

Additional context

This is the code of the hook:

import { useTranslation } from "react-i18next";

/**
 * Returns an object with translation functions for use in React components.
 *
 * @return {Object} An object containing four translation functions:
 *   - `t`: The original translation function from `useTranslation`.
 *   - `T`: Capitalizes the first letter of the translated string.
 *   - `TT`: Capitalizes the first letter of each word in the translated string.
 *   - `TTT`: Converts the translated string to uppercase.
 */

function useTranslate() {
  const { t } = useTranslation();

  const T = (key: string, params: object = {}) => {
    const translation = t(key, params);
    const result = translation.charAt(0).toUpperCase() + translation.slice(1);
    return result;
  };

  const TT = (key: string, params: object = {}) => {
    const translation = t(key, params);
    const result = translation
      .split(" ")
      .map((word) => word.charAt(0).toUpperCase() + word.slice(1))
      .join(" ");
    return result;
  };

  const TTT = (key: string, params: object = {}) => {
    const translation = t(key, params);
    const result = translation.toUpperCase();
    return result;
  };

  return { t, T, TT, TTT };
}

export default useTranslate;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

1 participant