@umituz/react-native-localization 2.1.0 → 2.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,13 +1,23 @@
1
1
  {
2
2
  "name": "@umituz/react-native-localization",
3
- "version": "2.1.0",
3
+ "version": "2.2.1",
4
4
  "description": "English-only localization system for React Native apps with i18n support",
5
5
  "main": "./src/index.ts",
6
6
  "types": "./src/index.ts",
7
7
  "scripts": {
8
8
  "typecheck": "tsc --noEmit --skipLibCheck",
9
9
  "lint": "tsc --noEmit",
10
- "prepublishOnly": "node scripts/prepublish.js"
10
+ "locales:generate": "node scripts/createLocaleLoaders.js",
11
+ "locales:generate:lang": "node scripts/createLocaleLoaders.js",
12
+ "prepublishOnly": "node scripts/prepublish.js",
13
+ "version:patch": "npm version patch -m 'chore: release v%s'",
14
+ "version:minor": "npm version minor -m 'chore: release v%s'",
15
+ "version:major": "npm version major -m 'chore: release v%s'",
16
+ "i18n:setup": "node scripts/setup-languages.js",
17
+ "i18n:translate": "node scripts/translate-missing.js",
18
+ "i18n:check": "node scripts/check-translations.js",
19
+ "i18n:analyze": "node scripts/analyze-keys.js",
20
+ "i18n:remove-unused": "node scripts/remove-unused-keys.js"
11
21
  },
12
22
  "keywords": [
13
23
  "react-native",
@@ -2,7 +2,7 @@
2
2
  * i18n Initializer
3
3
  *
4
4
  * Handles i18n configuration and initialization
5
- * - Resource building
5
+ * - Auto-discovers project translations
6
6
  * - i18n setup
7
7
  * - React i18next integration
8
8
  */
@@ -15,39 +15,44 @@ import { TranslationLoader } from './TranslationLoader';
15
15
  export class I18nInitializer {
16
16
  private static reactI18nextInitialized = false;
17
17
 
18
+ /**
19
+ * Auto-discover project translations from common paths
20
+ */
21
+ private static loadProjectTranslations(): Record<string, any> {
22
+ const possiblePaths = [
23
+ './src/locales/en-US', // App structure
24
+ './locales/en-US', // Alternative app structure
25
+ '../src/locales/en-US', // Relative from package
26
+ ];
27
+
28
+ for (const path of possiblePaths) {
29
+ try {
30
+ // eslint-disable-next-line @typescript-eslint/no-require-imports
31
+ const translations = require(path);
32
+ return translations.default || translations;
33
+ } catch {
34
+ // Try next path
35
+ }
36
+ }
37
+
38
+ return {};
39
+ }
40
+
18
41
  /**
19
42
  * Build resources object for all supported languages
20
43
  */
21
44
  private static buildResources(): Record<string, { translation: any }> {
22
45
  const resources: Record<string, { translation: any }> = {};
23
46
  const packageTranslations = TranslationLoader.loadPackageTranslations();
24
- const projectTranslations = TranslationLoader.loadProjectTranslations();
25
-
26
- // Build resources for each supported language
27
- for (const lang of SUPPORTED_LANGUAGES) {
28
- const langCode = lang.code;
29
- const packageTranslation = langCode === 'en-US' ? (packageTranslations['en-US'] || {}) : {};
30
- const projectTranslation = projectTranslations[langCode] || {};
31
-
32
- // For en-US, merge package and project translations
33
- // For other languages, use project translations only (fallback to en-US handled by i18n)
34
- if (langCode === 'en-US') {
35
- resources[langCode] = {
36
- translation: TranslationLoader.mergeTranslations(packageTranslation, projectTranslation),
37
- };
38
- } else if (projectTranslation && Object.keys(projectTranslation).length > 0) {
39
- resources[langCode] = {
40
- translation: projectTranslation,
41
- };
42
- }
43
- }
44
-
45
- // Ensure en-US is always present
46
- if (!resources['en-US']) {
47
- resources['en-US'] = {
48
- translation: packageTranslations['en-US'] || {},
49
- };
50
- }
47
+ const projectTranslations = this.loadProjectTranslations();
48
+
49
+ // For en-US, merge package and project translations
50
+ resources['en-US'] = {
51
+ translation: TranslationLoader.mergeTranslations(
52
+ packageTranslations['en-US'] || {},
53
+ projectTranslations
54
+ ),
55
+ };
51
56
 
52
57
  return resources;
53
58
  }
@@ -1,10 +1,7 @@
1
1
  /**
2
2
  * Translation Loader
3
3
  *
4
- * Handles loading of translations from different sources
5
- * - Package translations
6
- * - Project translations
7
- * - Resource merging
4
+ * Handles loading of translations from package only
8
5
  */
9
6
 
10
7
  export class TranslationLoader {
@@ -21,14 +18,6 @@ export class TranslationLoader {
21
18
  }
22
19
  }
23
20
 
24
- /**
25
- * Load project translations for all supported languages
26
- * Currently returns empty as projects manage their own translations
27
- */
28
- static loadProjectTranslations(): Record<string, any> {
29
- return {};
30
- }
31
-
32
21
  /**
33
22
  * Merge package defaults with project-specific translations
34
23
  */
@@ -1,17 +1,15 @@
1
1
  /**
2
- * i18n Configuration Entry Point
2
+ * i18n Configuration
3
3
  *
4
- * Delegates to I18nInitializer for setup
5
- * Exports i18n instance and utility functions
4
+ * Auto-initializes i18n with project translations
6
5
  */
7
6
 
8
- import i18n from 'i18next';
9
7
  import { I18nInitializer } from './I18nInitializer';
8
+ import i18n from 'i18next';
10
9
 
11
- // Initialize i18n immediately
10
+ // Initialize i18n automatically
12
11
  I18nInitializer.initialize();
13
12
 
14
- // Export utility functions
13
+ // Export for advanced usage
15
14
  export const addTranslationResources = I18nInitializer.addTranslationResources;
16
-
17
15
  export default i18n;
@@ -14,24 +14,13 @@ import i18n from '../config/i18n';
14
14
  * Hook for translation functionality
15
15
  */
16
16
  export const useTranslationFunction = (): ((key: string, options?: any) => string) => {
17
- // Always call useTranslation hook (React hooks rules)
18
- const translationResult = useTranslation(undefined, { i18n });
19
-
20
- // Use react-i18next if available, otherwise fallback to direct i18n
21
- if (translationResult?.t && typeof translationResult.t === 'function' && i18n.isInitialized) {
22
- return (key: string, options?: any): string => {
23
- const result = translationResult.t(key, options);
17
+ // Use direct i18n.t for reliability (no React context issues)
18
+ return (key: string, options?: any): string => {
19
+ if (i18n.isInitialized && typeof i18n.t === 'function') {
20
+ const result = i18n.t(key, options);
24
21
  return typeof result === 'string' ? result : String(result);
25
- };
26
- } else {
27
- return (key: string, options?: any): string => {
28
- // Fallback to direct i18n.t
29
- if (i18n.isInitialized && typeof i18n.t === 'function') {
30
- const result = i18n.t(key, options);
31
- return typeof result === 'string' ? result : String(result);
32
- }
33
- // Final fallback: return key
34
- return key;
35
- };
36
- }
22
+ }
23
+ // Final fallback: return key
24
+ return key;
25
+ };
37
26
  };