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

How to use @intlify/core in NodeJS #1663

Open
3 tasks done
mahnunchik opened this issue Dec 29, 2023 · 2 comments
Open
3 tasks done

How to use @intlify/core in NodeJS #1663

mahnunchik opened this issue Dec 29, 2023 · 2 comments
Labels
good first issue Good for newcomers Status: PR Welcome Welcome to Pull Request Status: Proposal Request for comments

Comments

@mahnunchik
Copy link

Clear and concise description of the problem

Could you please add code samples and/or documentation how to use @intlify/core in bare NodeJS app.

Suggested solution

Code samples or documentation or tests in the repo https://github.com/intlify/vue-i18n-next/tree/master/packages/core

Alternative

No response

Additional context

It would be helpful to use the same i18n system in vue app and server side code.

Validations

@mahnunchik mahnunchik added the Status: Proposal Request for comments label Dec 29, 2023
@kazupon kazupon added good first issue Good for newcomers Status: PR Welcome Welcome to Pull Request labels Dec 31, 2023 — with Volta.net
Copy link
Member

kazupon commented Dec 31, 2023

Thank you for your feedback!

We are planning to migrate @intlify/core from vue-i18n-next to a separate package to make it completely framework-agnostic in the future. We will have it documented at that time.

There is no documentation, but here are some real world packages where @intlify/core is used. It may be of interest to you.

@mahnunchik
Copy link
Author

My example code of using @intlify/core in Telegram bot:

// i18n.js
import {
  createCoreContext,
  number,
  translate,
} from '@intlify/core';

import datetimeFormats from './datetimeFormats.js';
import numberFormats from './numberFormats.js';
import pluralRules from './pluralRules.js';

const messages = {};
// load messages

const contexts = new Map();

export function i18n(locale) {
  if (!contexts.has(locale)) {
    contexts.set(locale, createCoreContext({
      locale: locale,
      messages,
      datetimeFormats,
      numberFormats,
      pluralRules,
    }));
  }
  const i18nContext = contexts.get(locale);
  return {
    t(key, ...args) {
      return translate(i18nContext, key, ...args);
    },
    n(value, ...args) {
      return number(i18nContext, value, ...args);
    },
  };
}

Usage:

bot.use(async (ctx, next) => {
  // make t and n available on context
  Object.assign(ctx, i18n(ctx.user.locale));
  return next();
});

This allow me to share almost all translation logic between vue app and server side bot related code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers Status: PR Welcome Welcome to Pull Request Status: Proposal Request for comments
Projects
None yet
Development

No branches or pull requests

2 participants