Skip to content

Commit

Permalink
feat: add debug logs
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Sep 8, 2023
1 parent 8ab2fa3 commit 99c02bb
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/debug.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* @adonisjs/i18n
*
* (c) AdonisJS
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

import { debuglog } from 'node:util'

export default debuglog('adonisjs:i18n')
3 changes: 3 additions & 0 deletions src/edge_plugin_i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import type { PluginFn } from 'edge.js/types'

import debug from './debug.js'
import type { I18n } from './i18n.js'
import type { I18nManager } from './i18n_manager.js'

Expand All @@ -17,6 +18,8 @@ import type { I18nManager } from './i18n_manager.js'
* flash messages
*/
export const edgePluginI18n: (i18n: I18nManager) => PluginFn<undefined> = (i18n) => {
debug('registering edge helpers')

return (edge) => {
/**
* Reference to i18n for the default locale. HTTP requests
Expand Down
3 changes: 3 additions & 0 deletions src/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import type { Emitter } from '@adonisjs/core/events'

import debug from './debug.js'
import type { I18nManager } from './i18n_manager.js'
import { Formatter } from './formatters/values_formatter.js'
import type { MissingTranslationEventPayload } from './types/main.js'
Expand Down Expand Up @@ -116,6 +117,8 @@ export class I18n extends Formatter {
* Switch locale for the current instance
*/
switchLocale(locale: string) {
debug('switching locale from "%s" to "%s"', this.locale, locale)

super.switchLocale(locale)
this.localeTranslations = this.#i18nManager.getTranslationsFor(this.locale)
this.fallbackTranslations = this.#i18nManager.getTranslationsFor(this.fallbackLocale)
Expand Down
5 changes: 5 additions & 0 deletions src/i18n_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import type {
MissingTranslationEventPayload,
} from './types/main.js'

import debug from './debug.js'
import { I18n } from './i18n.js'
import { FsLoader } from './loaders/fs_loader.js'
import { IcuFormatter } from './formatters/icu_messages_formatter.js'
Expand Down Expand Up @@ -163,6 +164,8 @@ export class I18nManager {
* Reload translations from the registered loaders
*/
async reloadTranslations() {
debug('loading translations')

const translationsStack = await Promise.all(
Object.keys(this.#config.loaders)
.filter((loader) => {
Expand Down Expand Up @@ -289,6 +292,8 @@ export class I18nManager {
type: 'loader' | 'formatter',
callback: ManagerLoaderFactory | ManagerFormatterFactory
): void {
debug('adding custom %s', type)

if (type === 'loader') {
this.#loaders[name] = callback as ManagerLoaderFactory
} else {
Expand Down
6 changes: 6 additions & 0 deletions src/loaders/fs_loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { fileURLToPath } from 'node:url'
import { join, extname } from 'node:path'
import { readFile } from 'node:fs/promises'
import { flatten, fsReadAll } from '@poppinss/utils'

import debug from '../debug.js'
import type { FsLoaderOptions, Translations, TranslationsLoaderContract } from '../types/main.js'

/**
Expand Down Expand Up @@ -93,6 +95,8 @@ export class FsLoader implements TranslationsLoaderContract {
* Processes the message inside a JSON file
*/
async #processJSONFile(filePath: string, messagesBag: Record<string, any>) {
debug('loading translations from "%s"', filePath)

const contents = await readFile(join(this.#storageBasePath, filePath), 'utf-8')
const messages = this.#parseJSON(filePath, contents)
this.#processFileTranslations(filePath, messages, messagesBag)
Expand Down Expand Up @@ -127,6 +131,8 @@ export class FsLoader implements TranslationsLoaderContract {
* Processes the message inside a YAML file
*/
async #processYamlFile(filePath: string, messagesBag: Record<string, any>) {
debug('loading translations from "%s"', filePath)

const contents = await readFile(join(this.#storageBasePath, filePath), 'utf-8')
const messages = this.#parseYaml(filePath, contents)
this.#processFileTranslations(filePath, messages, messagesBag)
Expand Down

0 comments on commit 99c02bb

Please sign in to comment.