Skip to content

Commit

Permalink
fix: fix issue localizely#118 add eol config to set generate files li…
Browse files Browse the repository at this point in the history
…ne separator.
  • Loading branch information
Lochinor committed Oct 26, 2024
1 parent ab55f76 commit 532fd38
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 14 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ flutter_intl:
arb_dir: lib/l10n # Optional. Sets the directory of your ARB resource files. Provided value should be a valid path on your system. Default: lib/l10n
output_dir: lib/generated # Optional. Sets the directory of generated localization files. Provided value should be a valid path on your system. Default: lib/generated
use_deferred_loading: false # Optional. Must be set to true to generate localization code that is loaded with deferred loading. Default: false
eol: 'CRLF' # Optional. Line separator, CR, LF, CRLF
localizely: # Optional settings if you use Localizely platform. Read more: https://localizely.com/blog/flutter-localization-step-by-step/?tab=automated-using-flutter-intl
project_id: # Get it from the https://app.localizely.com/projects page.
branch: # Get it from the “Branches” page on the Localizely platform, in case branching is enabled and you want to use a non-main branch.
Expand Down
13 changes: 13 additions & 0 deletions lib/src/config/pubspec_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class PubspecConfig {
String? _arbDir;
String? _outputDir;
bool? _useDeferredLoading;
String? _lineSeparator;
LocalizelyConfig? _localizelyConfig;

PubspecConfig() {
Expand Down Expand Up @@ -49,6 +50,16 @@ class PubspecConfig {
_useDeferredLoading = flutterIntlConfig['use_deferred_loading'] is bool
? flutterIntlConfig['use_deferred_loading']
: null;

String lineSeparator = flutterIntlConfig['eol'] is String
? flutterIntlConfig['eol']
: '';
_lineSeparator = {
'CR': '\r',
'LF': '\n',
'CRLF': '\r\n',
}[lineSeparator.toUpperCase()];

_localizelyConfig =
LocalizelyConfig.fromConfig(flutterIntlConfig['localizely']);
}
Expand All @@ -65,6 +76,8 @@ class PubspecConfig {

bool? get useDeferredLoading => _useDeferredLoading;

String? get lineSeparator => _lineSeparator;

LocalizelyConfig? get localizelyConfig => _localizelyConfig;
}

Expand Down
8 changes: 6 additions & 2 deletions lib/src/generator/generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class Generator {
late String _outputDir;
late bool _useDeferredLoading;
late bool _otaEnabled;
String? _lineSeparator;

/// Creates a new generator with configuration from the 'pubspec.yaml' file.
Generator() {
Expand Down Expand Up @@ -67,6 +68,8 @@ class Generator {

_otaEnabled =
pubspecConfig.localizelyConfig?.otaEnabled ?? defaultOtaEnabled;

_lineSeparator = pubspecConfig.lineSeparator;
}

/// Generates localization files.
Expand All @@ -88,7 +91,8 @@ class Generator {
var locales = _orderLocales(getLocales(_arbDir));
var content =
generateL10nDartFileContent(_className, labels, locales, _otaEnabled);
var formattedContent = formatDartContent(content, 'l10n.dart');
var formattedContent =
formatDartContent(content, 'l10n.dart', _lineSeparator);

await updateL10nDartFile(formattedContent, _outputDir);

Expand Down Expand Up @@ -150,6 +154,6 @@ class Generator {
var arbFiles = getArbFiles(_arbDir).map((file) => file.path).toList();

var helper = IntlTranslationHelper(_useDeferredLoading);
helper.generateFromArb(outputDir, dartFiles, arbFiles);
helper.generateFromArb(outputDir, dartFiles, arbFiles, _lineSeparator);
}
}
15 changes: 8 additions & 7 deletions lib/src/generator/intl_translation_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ class IntlTranslationHelper {
generation.generatedFilePrefix = '';
}

void generateFromArb(
String outputDir, List<String> dartFiles, List<String> arbFiles) {
void generateFromArb(String outputDir, List<String> dartFiles,
List<String> arbFiles, String? lineSeparator) {
var allMessages = dartFiles.map((file) => extraction.parseFile(File(file)));
for (var messageMap in allMessages) {
messageMap.forEach(
Expand All @@ -72,14 +72,14 @@ class IntlTranslationHelper {
_loadData(arbFile, messagesByLocale);
}
messagesByLocale.forEach((locale, data) {
_generateLocaleFile(locale, data, outputDir);
_generateLocaleFile(locale, data, outputDir, lineSeparator);
});

var fileName = '${generation.generatedFilePrefix}messages_all.dart';
var mainImportFile = File(path.join(outputDir, fileName));

var content = generation.generateMainImportFile();
var formattedContent = formatDartContent(content, fileName);
var formattedContent = formatDartContent(content, fileName, lineSeparator);

mainImportFile.writeAsStringSync(formattedContent);
}
Expand All @@ -103,8 +103,8 @@ class IntlTranslationHelper {
generation.allLocales.add(locale);
}

void _generateLocaleFile(
String locale, List<Map> localeData, String targetDir) {
void _generateLocaleFile(String locale, List<Map> localeData,
String targetDir, String? lineSeparator) {
var translations = <TranslatedMessage>[];
for (var jsonTranslations in localeData) {
jsonTranslations.forEach((id, messageData) {
Expand All @@ -114,7 +114,8 @@ class IntlTranslationHelper {
}
});
}
generation.generateIndividualMessageFile(locale, translations, targetDir);
generation.generateIndividualMessageFile(
locale, translations, targetDir, lineSeparator);
}

/// Regenerate the original IntlMessage objects from the given [data]. For
Expand Down
11 changes: 8 additions & 3 deletions lib/src/intl_translation/generate_localized.dart
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,15 @@ class MessageGeneration {

/// Generate a file <[generated_file_prefix]>_messages_<[locale]>.dart
/// for the [translations] in [locale] and put it in [targetDir].
void generateIndividualMessageFile(String basicLocale,
Iterable<TranslatedMessage> translations, String targetDir) {
void generateIndividualMessageFile(
String basicLocale,
Iterable<TranslatedMessage> translations,
String targetDir,
String? lineSeparator) {
final fileName = '${generatedFilePrefix}messages_$basicLocale.dart';
final content = contentForLocale(basicLocale, translations);
final formattedContent = formatDartContent(content, fileName);
final formattedContent =
formatDartContent(content, fileName, lineSeparator);

// To preserve compatibility, we don't use the canonical version of the
// locale in the file name.
Expand Down Expand Up @@ -477,6 +481,7 @@ abstract class TranslatedMessage {

/// For backward compatibility, we still have the originalMessage API.
MainMessage? get originalMessage => originalMessages?.first;

set originalMessage(MainMessage? m) {
if (m != null) {
originalMessages = [m];
Expand Down
4 changes: 2 additions & 2 deletions lib/src/utils/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ String formatJsonMessage(String jsonMessage) {
}

/// Formats Dart file content.
String formatDartContent(String content, String fileName) {
String formatDartContent(String content, String fileName, String? lineEnding) {
try {
var formatter = DartFormatter();
var formatter = DartFormatter(lineEnding: lineEnding);
return formatter.format(content);
} catch (e) {
info('Failed to format \'$fileName\' file.');
Expand Down

0 comments on commit 532fd38

Please sign in to comment.