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

Implemeted: i18n for centralized use (#134) #145

Merged
merged 7 commits into from
Sep 21, 2023

Conversation

sanskar345
Copy link
Contributor

@sanskar345 sanskar345 commented Sep 7, 2023

closes #134

Steps to verify --

Description -

- Unable to access the app files in the pulgin:

I tried accessing the app files in the plugin but only able to access the node-modules folder files.

How i tried to accessing it - by requiring all the files using require.context and giving the path of the apps location.

Tried to find different ways how it can be done but found nothing.

- Shifted i18n logic to dxp-components in index.ts. This is because:
When taking localeMessages from the app as plugin options and when passing it in context to another location (lets say i18n.ts) and importing the context in the file error occurs of contex.localeMessages used before initialization this is beacause when importing this file from the index.ts the file (i18n.ts) runs first and then the rest logic (of creating the context and assigning localeMessages) in index.ts.

- Removed the function (to load localeMessages from the locales folder and creating an object):

function loadLocaleMessages(): LocaleMessages<VueMessageType> {
  const locales = require.context('./locales', true, /[A-Za-z0-9-_,\s]+\.json$/i)
  const messages: LocaleMessages<VueMessageType> = {}
  locales.keys().forEach(key => {
    const matched = key.match(/([A-Za-z0-9-_]+)\./i)
    if (matched && matched.length > 1) {
      const locale = matched[1]
      messages[locale] = locales(key)
    }
  })
  return messages
}

The above function just creates an object of JSONs present in the locales file like - { en: { }, es: {} }

Now with the new approach i am just exporting this JSONs from locales folder by creating index.ts and exporting the required object { en: { } } by importing the JSONs in the index.ts

Now importing this object (exported from the locales/index.ts) and passing it dxp options.

Note - for importing JSONs in typescript added "resolveJsonModule": true configuration in ts config.

- How process and require can be used in the ts files of dxp:

by declaring them like :-

declare var process: any;
in the file

Here we are telling the TypeScript compiler that there is already a variable named process with the type any. This is useful when you have variables introduced by sources that the compiler is not be aware of.

@sanskar345 sanskar345 marked this pull request as ready for review September 11, 2023 05:31
@sanskar345 sanskar345 marked this pull request as draft September 15, 2023 10:45
@ravilodhi ravilodhi merged commit e15105d into hotwax:main Sep 21, 2023
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement i18n Library for Centralized Use
2 participants