-
Notifications
You must be signed in to change notification settings - Fork 9
Fluent-to-PO / PO-to-Fluent conversion actions #650
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
iangmaia
wants to merge
11
commits into
trunk
Choose a base branch
from
iangmaia/fluent-po-conversion-actions
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
49e2c72
Add Fluent to PO conversion actions for GlotPress integration
iangmaia b52a3c6
Better handle multi-line comments
iangmaia be90081
Add generated comment to PO header
iangmaia 97999a4
Add `allow_empty_file` to po 2 fluent action
iangmaia 8187521
Replace `poparser` with `gettext`
iangmaia 6d2d686
Remove unneeded comments
iangmaia 9271cd4
Add `FluentLocalizationHelper` tests
iangmaia 46068d0
Add `PoToFluentAction` tests
iangmaia 36ebf05
Add tests for `FluentToPoAction`
iangmaia 9a1bd6a
Fix duplicated branch with generic fallback
iangmaia 5525869
Update CHANGELOG.md
iangmaia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
lib/fastlane/plugin/wpmreleasetoolkit/actions/common/fluent_to_po_action.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'gettext' | ||
require 'gettext/po_parser' | ||
require 'gettext/po' | ||
require 'twitter_cldr' | ||
|
||
module Fastlane | ||
module Actions | ||
class FluentToPoAction < Action | ||
def self.run(params) | ||
require_relative '../../helper/fluent_localization_helper' | ||
|
||
UI.message('Converting Fluent file to PO format...') | ||
|
||
input_path = File.expand_path(params[:input_file]) | ||
output_path = File.expand_path(params[:output_file]) | ||
|
||
UI.user_error!("Input file does not exist: #{input_path}") unless File.exist?(input_path) | ||
|
||
# Parse the Fluent file | ||
fluent_entries = Helper::FluentLocalizationHelper.parse_fluent_file(input_path) | ||
UI.message("Found #{fluent_entries.length} entries in Fluent file") | ||
|
||
# Convert to PO format | ||
po_content = Helper::FluentLocalizationHelper.fluent_to_po( | ||
fluent_file: File.basename(input_path), | ||
fluent_entries: fluent_entries, | ||
locale: params[:locale], | ||
project_name: params[:project_name], | ||
project_version: params[:project_version] | ||
) | ||
|
||
File.write(output_path, po_content, encoding: 'utf-8') | ||
|
||
UI.success("Successfully converted Fluent file to PO: #{output_path}") | ||
|
||
output_path | ||
end | ||
|
||
def self.description | ||
'Convert Fluent (.ftl) files to PO (.po) format for GlotPress integration' | ||
end | ||
|
||
def self.authors | ||
['Automattic'] | ||
end | ||
|
||
def self.available_options | ||
[ | ||
FastlaneCore::ConfigItem.new( | ||
key: :input_file, | ||
description: 'Path to the input Fluent (.ftl) file', | ||
optional: false, | ||
type: String | ||
), | ||
FastlaneCore::ConfigItem.new( | ||
key: :output_file, | ||
description: 'Path to the output PO (.po) file', | ||
optional: false, | ||
type: String | ||
), | ||
FastlaneCore::ConfigItem.new( | ||
key: :locale, | ||
description: 'Target locale (e.g., en-US, es-ES)', | ||
optional: false, | ||
type: String | ||
), | ||
FastlaneCore::ConfigItem.new( | ||
key: :project_name, | ||
description: 'Project name for the PO header', | ||
optional: true, | ||
type: String, | ||
default_value: '' | ||
), | ||
FastlaneCore::ConfigItem.new( | ||
key: :project_version, | ||
description: 'Project version for the PO header', | ||
optional: true, | ||
type: String, | ||
default_value: '' | ||
), | ||
] | ||
end | ||
|
||
def self.is_supported?(platform) | ||
true | ||
end | ||
end | ||
end | ||
end |
74 changes: 74 additions & 0 deletions
74
lib/fastlane/plugin/wpmreleasetoolkit/actions/common/po_to_fluent_action.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'gettext' | ||
require 'gettext/po_parser' | ||
require 'gettext/po' | ||
|
||
module Fastlane | ||
module Actions | ||
class PoToFluentAction < Action | ||
def self.run(params) | ||
require_relative '../../helper/fluent_localization_helper' | ||
|
||
UI.message('Converting PO file to Fluent format...') | ||
|
||
input_path = File.expand_path(params[:input_file]) | ||
output_path = File.expand_path(params[:output_file]) | ||
|
||
UI.user_error!("Input file does not exist: #{input_path}") unless File.exist?(input_path) | ||
|
||
# Parse the PO file | ||
po_entries = Helper::FluentLocalizationHelper.parse_po_file(input_path) | ||
|
||
# Convert to Fluent format | ||
fluent_content = Helper::FluentLocalizationHelper.po_to_fluent(po_entries) | ||
|
||
if !params[:allow_empty_file] && fluent_content.strip.empty? | ||
UI.message('No translated content found in PO file') | ||
return | ||
end | ||
|
||
File.write(output_path, fluent_content, encoding: 'utf-8') | ||
UI.success("Successfully converted PO file to Fluent: #{output_path}") | ||
|
||
output_path | ||
end | ||
|
||
def self.description | ||
'Convert PO (.po) files to Fluent (.ftl) format after translation' | ||
end | ||
|
||
def self.authors | ||
['Automattic'] | ||
end | ||
|
||
def self.available_options | ||
[ | ||
FastlaneCore::ConfigItem.new( | ||
key: :input_file, | ||
description: 'Path to the input PO (.po) file', | ||
optional: false, | ||
type: String | ||
), | ||
Comment on lines
+47
to
+52
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 What about using fastlane's built-in |
||
FastlaneCore::ConfigItem.new( | ||
key: :output_file, | ||
description: 'Path to the output Fluent (.ftl) file', | ||
optional: false, | ||
type: String | ||
), | ||
FastlaneCore::ConfigItem.new( | ||
key: :allow_empty_file, | ||
description: 'Whether to allow an empty file when no translated content is found', | ||
optional: true, | ||
type: Boolean, | ||
default_value: false | ||
), | ||
] | ||
end | ||
|
||
def self.is_supported?(platform) | ||
true | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick: While it's nice to have concise name for actions so they don't become a mouthful, I feel like we could use a bit more descriptive action names—especially so that their goal is not too mysterious when we see those action names without much context attached, like when called in a
Fastfile
or when runningfastlane actions
to list all the available actions…Maybe
convert_fluent_translation_file_to_po_format
/convert_po_translation_file_to_fluent_format
, orconvert_strings_file_fluent_to_po
/convert_strings_file_po_to_fluent
? Sure, that's a bit more verbose to type, but also less ambiguous—especially for people who never heard of Fluent and that file format and would see that action in the while without knowing that Fluent and Po are terms related to translations and strings file formats?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed 👍 I also like the side effect of grouping similar actions by name (
convert_*
).What about
convert_fluent_strings_file_to_po
vs.convert_po_strings_file_to_fluent
? 🤔There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, I like that naming suggestion