Skip to content

i18n文件夹需要默认在根目录下,否则会抛出错误 : [i18n] ensure run initI18n() in app.js before using I18n library #26

@raymond-yan

Description

@raymond-yan

我是参照官方示例,编译wxss和i18n文件,但是打包i18n/locales.js的路径不是在小程序的根目录下,即不在 dist/i18n,而是在分包 dist/card/i18n下,导致我运行分包内的页面时抛出以下异常。

[i18n] ensure run initI18n() in app.js before using I18n library

之后需要在初始化的时候,强制调用initI18n,并传入实际的i18n路径,例如:

import { initI18n } from '@miniprogram-i18n/core';

// 因i18n文件夹在分包内,不在根目录下,需要手动指定编译后的位置。
initI18n('./i18n/locales.js', '');

这里有两个建议,一个是官方文档可以给出一些指引,告诉用户,默认是寻找根目录下的i18n路径,否则需要调用初始化方法,并指定实际的i18n文件夹路径。

第二个是initI18n明明只有第一个参数有用,但是在src中确是需要传入两个参数,我看源码也只是这样写:

export function initI18n(locales: any, currentLocale: string) {
  innerGlobals.i18nInstance = new I18nRuntimeBase(locales.translations, locales.defaultLocale, locales.fallbackLocale)
  return innerGlobals.i18nInstance
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions