Skip to content

Commit

Permalink
feat: lazy loading
Browse files Browse the repository at this point in the history
  • Loading branch information
Tienisto committed Oct 18, 2024
1 parent 002b57d commit 58f9da8
Show file tree
Hide file tree
Showing 138 changed files with 4,784 additions and 2,377 deletions.
12 changes: 12 additions & 0 deletions slang/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
## 4.0.0

**Number formats and Lazy loading**

On web, [Deferred loading](https://dart.dev/language/libraries#lazily-loading-a-library) is used to reduce initial load time.

- **Breaking:** `setLocale`, `setLocaleRaw`, and `useDeviceLocale` returns a Future, use `-Sync` suffix for synchronous calls
- **Breaking:** `output_format` removed, always generates multiple files now
- **Breaking:** deprecated functions in `LocaleSettings` (`supportedLocales`, `supportedLocalesRaw`) removed
- **Breaking:** defining contexts (enums) is no longer allowed in `build.yaml` or `slang.yaml` (deprecated in v3.19.0)
- **Breaking:** enums specified in `context` are no longer transformed into pascal case keeping the original case

## 3.32.0

- feat: add syntax to escape linked translations (#248) @Fasust
Expand Down
2 changes: 0 additions & 2 deletions slang/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -442,8 +442,6 @@ targets:
| `pluralization`/`default_parameter` | `String` | default plural parameter [(i)](#-pluralization) | `n` |
| `pluralization`/`cardinal` | `List<String>` | entries which have cardinals | `[]` |
| `pluralization`/`ordinal` | `List<String>` | entries which have ordinals | `[]` |
| `<context>`/`enum` | `List<String>` | DEPRECATED: context forms [(i)](#-custom-contexts--enums) | no default |
| `<context>`/`paths` | `List<String>` | DEPRECATED: entries using this context | `[]` |
| `<context>`/`default_parameter` | `String` | default parameter name | `context` |
| `<context>`/`generate_enum` | `Boolean` | generate enum | `true` |
| `children of interfaces` | `Pairs of Alias:Path` | alias interfaces [(i)](#-interfaces) | `null` |
Expand Down
90 changes: 25 additions & 65 deletions slang/bin/slang.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import 'dart:io';

import 'package:collection/collection.dart';
import 'package:slang/builder/builder/slang_file_collection_builder.dart';
import 'package:slang/builder/builder/translation_map_builder.dart';
import 'package:slang/builder/model/enums.dart';
import 'package:slang/builder/model/raw_config.dart';
import 'package:slang/builder/model/slang_file_collection.dart';
import 'package:slang/src/builder/builder/slang_file_collection_builder.dart';
import 'package:slang/src/builder/builder/translation_map_builder.dart';
import 'package:slang/src/builder/generator_facade.dart';
import 'package:slang/src/builder/model/raw_config.dart';
import 'package:slang/src/builder/model/slang_file_collection.dart';
import 'package:slang/src/builder/utils/file_utils.dart';
import 'package:slang/src/builder/utils/path_utils.dart';
import 'package:slang/src/runner/analyze.dart';
Expand Down Expand Up @@ -317,78 +315,45 @@ Future<void> generateTranslations({
// STEP 3: generate .g.dart content
final result = GeneratorFacade.generate(
rawConfig: fileCollection.config,
baseName: fileCollection.config.outputFileName.getFileNameNoExtension(),
translationMap: translationMap,
inputDirectoryHint: fileCollection.determineInputPath(),
);

// STEP 4: write output to hard drive
FileUtils.createMissingFolders(filePath: outputFilePath);
if (fileCollection.config.outputFormat == OutputFormat.singleFile) {
// single file
FileUtils.writeFile(
path: outputFilePath,
content: result.joinAsSingleOutput(),
);
} else {
// multiple files

FileUtils.writeFile(
path: BuildResultPaths.mainPath(outputFilePath),
content: result.main,
);
for (final entry in result.translations.entries) {
final locale = entry.key;
final localeTranslations = entry.value;
FileUtils.writeFile(
path: BuildResultPaths.mainPath(outputFilePath),
content: result.header,
path: BuildResultPaths.localePath(
outputPath: outputFilePath,
locale: locale,
),
content: localeTranslations,
);
for (final entry in result.translations.entries) {
final locale = entry.key;
final localeTranslations = entry.value;
FileUtils.writeFile(
path: BuildResultPaths.localePath(
outputPath: outputFilePath,
locale: locale,
),
content: localeTranslations,
);
}
if (result.flatMap != null) {
FileUtils.writeFile(
path: BuildResultPaths.flatMapPath(
outputPath: outputFilePath,
),
content: result.flatMap!,
);
}
}

if (verbose) {
print('');
if (fileCollection.config.outputFormat == OutputFormat.singleFile) {
print('Output: $outputFilePath');
} else {
print('Output:');
print(' -> $outputFilePath');
for (final locale in result.translations.keys) {
print(' -> ${BuildResultPaths.localePath(
outputPath: outputFilePath,
locale: locale,
)}');
}
if (result.flatMap != null) {
print(' -> ${BuildResultPaths.flatMapPath(
outputPath: outputFilePath,
)}');
}
print('');
print('Output:');
print(' -> $outputFilePath');
for (final locale in result.translations.keys) {
print(' -> ${BuildResultPaths.localePath(
outputPath: outputFilePath,
locale: locale,
)}');
}
print('');

if (stopwatch != null) {
print(
'${_GREEN}Translations generated successfully. ${stopwatch.elapsedSeconds}$_RESET');
}

final deprecatedContext = fileCollection.config.contexts
.firstWhereOrNull((c) => c.enumValues != null || c.paths.isNotEmpty);
if (deprecatedContext != null) {
print(
'$_YELLOW[Deprecated] Use explicit context modifiers instead of populating the config: ${deprecatedContext.enumName} (see: https://github.com/slang-i18n/slang/blob/main/slang/MIGRATION.md#use-context-modifier-since-3190)$_RESET');
}
}
}

Expand All @@ -403,11 +368,6 @@ extension on String {
String getFileName() {
return PathUtils.getFileName(this);
}

/// converts /some/path/file.json to file
String getFileNameNoExtension() {
return PathUtils.getFileNameNoExtension(this);
}
}

String? _lastPrint;
Expand Down
2 changes: 1 addition & 1 deletion slang/example/ios/Flutter/AppFrameworkInfo.plist
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@
<key>CFBundleVersion</key>
<string>1.0</string>
<key>MinimumOSVersion</key>
<string>8.0</string>
<string>12.0</string>
</dict>
</plist>
13 changes: 8 additions & 5 deletions slang/example/ios/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
archiveVersion = 1;
classes = {
};
objectVersion = 46;
objectVersion = 54;
objects = {

/* Begin PBXBuildFile section */
Expand Down Expand Up @@ -127,7 +127,7 @@
97C146E61CF9000F007C117D /* Project object */ = {
isa = PBXProject;
attributes = {
LastUpgradeCheck = 1020;
LastUpgradeCheck = 1510;
ORGANIZATIONNAME = "";
TargetAttributes = {
97C146ED1CF9000F007C117D = {
Expand Down Expand Up @@ -171,10 +171,12 @@
/* Begin PBXShellScriptBuildPhase section */
3B06AD1E1E4923F5004D2608 /* Thin Binary */ = {
isa = PBXShellScriptBuildPhase;
alwaysOutOfDate = 1;
buildActionMask = 2147483647;
files = (
);
inputPaths = (
"${TARGET_BUILD_DIR}/${INFOPLIST_PATH}",
);
name = "Thin Binary";
outputPaths = (
Expand All @@ -185,6 +187,7 @@
};
9740EEB61CF901F6004384FC /* Run Script */ = {
isa = PBXShellScriptBuildPhase;
alwaysOutOfDate = 1;
buildActionMask = 2147483647;
files = (
);
Expand Down Expand Up @@ -272,7 +275,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
SUPPORTED_PLATFORMS = iphoneos;
Expand Down Expand Up @@ -346,7 +349,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
MTL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = iphoneos;
Expand Down Expand Up @@ -395,7 +398,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
SUPPORTED_PLATFORMS = iphoneos;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1020"
LastUpgradeVersion = "1510"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
2 changes: 1 addition & 1 deletion slang/example/ios/Runner/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import UIKit
import Flutter

@UIApplicationMain
@main
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
Expand Down
4 changes: 4 additions & 0 deletions slang/example/ios/Runner/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,9 @@
<string>en</string>
<string>de</string>
</array>
<key>CADisableMinimumFrameDurationOnPhone</key>
<true/>
<key>UIApplicationSupportsIndirectInputEvents</key>
<true/>
</dict>
</plist>
Loading

0 comments on commit 58f9da8

Please sign in to comment.