jekyll-l10n 1.7.1 → 3.0.0
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.
- checksums.yaml +4 -4
- data/README.md +44 -0
- data/lib/jekyll-l10n/configuration/schema.rb +308 -0
- data/lib/jekyll-l10n/configuration.rb +165 -0
- data/lib/jekyll-l10n/constants.rb +45 -10
- data/lib/jekyll-l10n/errors.rb +7 -0
- data/lib/jekyll-l10n/extraction/compendium_merger.rb +9 -11
- data/lib/jekyll-l10n/extraction/compendium_translator.rb +27 -11
- data/lib/jekyll-l10n/extraction/config_loader.rb +28 -58
- data/lib/jekyll-l10n/extraction/coverage_report.rb +129 -0
- data/lib/jekyll-l10n/extraction/dom_text_extractor.rb +2 -31
- data/lib/jekyll-l10n/extraction/extractor.rb +72 -65
- data/lib/jekyll-l10n/extraction/html_string_extractor.rb +24 -10
- data/lib/jekyll-l10n/extraction/logger.rb +6 -5
- data/lib/jekyll-l10n/extraction/result_saver.rb +5 -10
- data/lib/jekyll-l10n/instrumentation.rb +172 -139
- data/lib/jekyll-l10n/jekyll/generator.rb +47 -61
- data/lib/jekyll-l10n/jekyll/localized_page.rb +27 -49
- data/lib/jekyll-l10n/jekyll/post_write_html_reprocessor.rb +53 -48
- data/lib/jekyll-l10n/jekyll/post_write_processor.rb +2 -1
- data/lib/jekyll-l10n/jekyll/regeneration_checker.rb +70 -50
- data/lib/jekyll-l10n/jekyll/url_filter.rb +13 -13
- data/lib/jekyll-l10n/po_file/entry_parser.rb +155 -0
- data/lib/jekyll-l10n/po_file/fuzzy_matcher.rb +67 -53
- data/lib/jekyll-l10n/po_file/loader.rb +20 -11
- data/lib/jekyll-l10n/po_file/manager.rb +32 -17
- data/lib/jekyll-l10n/po_file/merger.rb +2 -1
- data/lib/jekyll-l10n/po_file/path_builder.rb +11 -1
- data/lib/jekyll-l10n/po_file/reader.rb +40 -508
- data/lib/jekyll-l10n/po_file/writer.rb +31 -16
- data/lib/jekyll-l10n/translation/block_text_extractor.rb +3 -7
- data/lib/jekyll-l10n/translation/html_translator.rb +136 -157
- data/lib/jekyll-l10n/translation/libre_translate_languages.rb +73 -0
- data/lib/jekyll-l10n/translation/libre_translator.rb +134 -78
- data/lib/jekyll-l10n/translation/page_translation_loader.rb +13 -29
- data/lib/jekyll-l10n/translation/request_slots.rb +69 -0
- data/lib/jekyll-l10n/translation/translation_progress.rb +77 -0
- data/lib/jekyll-l10n/translation/translator.rb +49 -78
- data/lib/jekyll-l10n/utils/baseurl_normalizer.rb +108 -0
- data/lib/jekyll-l10n/utils/debug_logger.rb +4 -51
- data/lib/jekyll-l10n/utils/error_handler.rb +9 -24
- data/lib/jekyll-l10n/utils/external_link_icon_preserver.rb +5 -62
- data/lib/jekyll-l10n/utils/html_parser.rb +24 -31
- data/lib/jekyll-l10n/utils/html_text_utils.rb +146 -91
- data/lib/jekyll-l10n/utils/logger_formatter.rb +6 -4
- data/lib/jekyll-l10n/utils/msgstr_validator.rb +74 -0
- data/lib/jekyll-l10n/utils/page_locales_config.rb +165 -223
- data/lib/jekyll-l10n/utils/po_entry_converter.rb +0 -43
- data/lib/jekyll-l10n/utils/site_config_accessor.rb +82 -42
- data/lib/jekyll-l10n/utils/translation_resolver.rb +56 -6
- data/lib/jekyll-l10n/utils/url_path_builder.rb +5 -27
- data/lib/jekyll-l10n/utils/url_transformer.rb +5 -11
- data/lib/jekyll-l10n/version.rb +1 -1
- data/lib/jekyll-l10n.rb +67 -176
- metadata +12 -3
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require_relative '../constants'
|
|
3
4
|
require_relative '../translation/page_translation_loader'
|
|
4
5
|
require_relative '../translation/html_translator'
|
|
5
6
|
require_relative '../utils/page_locales_config'
|
|
@@ -9,55 +10,52 @@ require_relative '../utils/external_link_icon_preserver'
|
|
|
9
10
|
|
|
10
11
|
module Jekyll
|
|
11
12
|
module L10n
|
|
12
|
-
#
|
|
13
|
+
# Translates the localized HTML files again after extraction.
|
|
13
14
|
#
|
|
14
|
-
#
|
|
15
|
-
#
|
|
16
|
-
#
|
|
17
|
-
#
|
|
15
|
+
# Localized pages are first translated while Jekyll renders them, using the
|
|
16
|
+
# PO files as they were at the start of the build. Extraction then runs and
|
|
17
|
+
# may add translations, for example on the first build or through machine
|
|
18
|
+
# translation. PostWriteProcessor calls this class when extraction wrote PO
|
|
19
|
+
# files, so this build's output already uses the new translations.
|
|
18
20
|
#
|
|
19
|
-
#
|
|
20
|
-
#
|
|
21
|
-
#
|
|
22
|
-
# * Create HtmlTranslator with proper configuration
|
|
23
|
-
# * Apply translations to HTML documents
|
|
24
|
-
# * Preserve external link icons from original HTML
|
|
25
|
-
# * Write translated HTML back to files
|
|
26
|
-
# * Handle errors gracefully with logging
|
|
21
|
+
# It only covers localized pages in `site.pages`. Pages that incremental
|
|
22
|
+
# mode skipped are not there; they pick up the new translations on the
|
|
23
|
+
# next build.
|
|
27
24
|
#
|
|
28
25
|
# @example
|
|
29
|
-
#
|
|
30
|
-
# reprocessor.reprocess_localized_pages
|
|
31
|
-
# # Reads localized HTML, translates, preserves icons, writes back
|
|
26
|
+
# PostWriteHtmlReprocessor.new(site).reprocess_localized_pages
|
|
32
27
|
class PostWriteHtmlReprocessor
|
|
33
28
|
attr_reader :site
|
|
34
29
|
|
|
35
|
-
#
|
|
36
|
-
#
|
|
37
|
-
# @param site [Jekyll::Site] Jekyll site object
|
|
30
|
+
# @param site [Jekyll::Site] A site that Jekyll has already written
|
|
38
31
|
def initialize(site)
|
|
39
32
|
@site = site
|
|
40
33
|
@dest = SiteConfigAccessor.dest(@site)
|
|
41
34
|
end
|
|
42
35
|
|
|
43
|
-
#
|
|
36
|
+
# Reads each localized file whose translations changed since the render
|
|
37
|
+
# pass, translates it, keeps the external link icons of the English page,
|
|
38
|
+
# and writes it back. Pages that carry a matching +translations_digest+
|
|
39
|
+
# from the Translator are skipped.
|
|
44
40
|
#
|
|
45
|
-
#
|
|
46
|
-
#
|
|
47
|
-
# preserves external link icon styling from the original HTML, and writes
|
|
48
|
-
# the translated HTML back to disk.
|
|
41
|
+
# Logs "Applied new translations to N of M localized pages" only when at
|
|
42
|
+
# least one page was reprocessed.
|
|
49
43
|
#
|
|
50
44
|
# @return [void]
|
|
51
45
|
def reprocess_localized_pages
|
|
52
46
|
localized_files = find_localized_html_files
|
|
53
47
|
return if localized_files.empty?
|
|
54
48
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
localized_files.each do |html_path, locale, original_url|
|
|
59
|
-
translate_html_file(html_path, locale, original_url)
|
|
49
|
+
reprocessed = 0
|
|
50
|
+
localized_files.each do |html_path, locale, original_url, page|
|
|
51
|
+
reprocessed += 1 if translate_html_file(html_path, locale, original_url, page)
|
|
60
52
|
end
|
|
53
|
+
|
|
54
|
+
return unless reprocessed.positive?
|
|
55
|
+
|
|
56
|
+
msg = "Applied new translations to #{reprocessed} of #{localized_files.length} " \
|
|
57
|
+
'localized pages'
|
|
58
|
+
Jekyll.logger.info Constants::LOG_PREFIX, msg
|
|
61
59
|
end
|
|
62
60
|
|
|
63
61
|
private
|
|
@@ -72,42 +70,49 @@ module Jekyll
|
|
|
72
70
|
original_url = page.data['original_url']
|
|
73
71
|
html_path = page.destination(@dest)
|
|
74
72
|
|
|
75
|
-
results << [html_path, locale, original_url] if File.exist?(html_path)
|
|
73
|
+
results << [html_path, locale, original_url, page] if File.exist?(html_path)
|
|
76
74
|
end
|
|
77
75
|
|
|
78
76
|
results
|
|
79
77
|
end
|
|
80
78
|
|
|
81
|
-
|
|
82
|
-
def translate_html_file(html_path, locale, original_url)
|
|
83
|
-
# rubocop:enable Metrics/AbcSize
|
|
84
|
-
original_html = FileOperations.read_utf8(html_path)
|
|
79
|
+
def translate_html_file(html_path, locale, original_url, page = nil)
|
|
85
80
|
original_page = find_original_page(original_url)
|
|
86
81
|
|
|
87
82
|
return unless original_page
|
|
88
83
|
|
|
89
|
-
config = PageLocalesConfig.new(original_page
|
|
84
|
+
config = PageLocalesConfig.new(SiteConfigAccessor.effective_page_data(original_page))
|
|
90
85
|
translations = PageTranslationLoader.load(@site, locale, original_url, config)
|
|
91
86
|
|
|
92
87
|
return if translations.nil? || translations.empty?
|
|
93
88
|
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
config.translatable_attributes,
|
|
97
|
-
debug_logging: config.debug_logging?
|
|
98
|
-
)
|
|
89
|
+
# Skip when the page was already rendered with these same translations
|
|
90
|
+
return if skip_reprocess?(page, translations)
|
|
99
91
|
|
|
100
|
-
|
|
101
|
-
translated_html = translator.translate(original_html, translations, locale, baseurl)
|
|
92
|
+
translator = HtmlTranslator.from_config(config)
|
|
102
93
|
|
|
103
|
-
|
|
104
|
-
# Preserve external link icons from the original HTML after translation
|
|
105
|
-
translated_html = ExternalLinkIconPreserver.preserve(original_html, translated_html)
|
|
106
|
-
FileOperations.write_utf8(html_path, translated_html)
|
|
107
|
-
end
|
|
94
|
+
write_translated_html(html_path, translator, translations, locale)
|
|
108
95
|
rescue StandardError => e
|
|
109
96
|
error_msg = "Failed to reprocess #{html_path}: #{e.message}"
|
|
110
|
-
Jekyll.logger.warn
|
|
97
|
+
Jekyll.logger.warn Constants::LOG_PREFIX, error_msg
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def skip_reprocess?(page, translations)
|
|
101
|
+
page.respond_to?(:translations_digest) &&
|
|
102
|
+
page.translations_digest == translations.hash
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def write_translated_html(html_path, translator, translations, locale)
|
|
106
|
+
# Read only now, so pages skipped above cost no file read.
|
|
107
|
+
original_html = FileOperations.read_utf8(html_path)
|
|
108
|
+
baseurl = @site.config['baseurl'] || ''
|
|
109
|
+
translated_html = translator.translate(original_html, translations, locale, baseurl)
|
|
110
|
+
return unless translated_html
|
|
111
|
+
|
|
112
|
+
# Preserve external link icons from the original HTML after translation
|
|
113
|
+
translated_html = ExternalLinkIconPreserver.preserve(original_html, translated_html)
|
|
114
|
+
FileOperations.write_utf8(html_path, translated_html)
|
|
115
|
+
true
|
|
111
116
|
end
|
|
112
117
|
|
|
113
118
|
def find_original_page(url)
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require_relative '../configuration'
|
|
3
4
|
require_relative '../extraction/extractor'
|
|
4
5
|
require_relative 'post_write_html_reprocessor'
|
|
5
6
|
|
|
@@ -77,7 +78,7 @@ module Jekyll
|
|
|
77
78
|
#
|
|
78
79
|
# @return [Boolean] True if at least one page has with_locales set to true
|
|
79
80
|
def any_pages_localized?
|
|
80
|
-
@site.pages.any? { |page| page
|
|
81
|
+
@site.pages.any? { |page| Configuration.localizable?(page) }
|
|
81
82
|
end
|
|
82
83
|
end
|
|
83
84
|
end
|
|
@@ -1,62 +1,53 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require_relative '../configuration'
|
|
4
|
+
require_relative '../constants'
|
|
3
5
|
require_relative '../utils/site_config_accessor'
|
|
4
6
|
require_relative '../po_file/path_builder'
|
|
5
7
|
require_relative '../utils/locale_url_formatter'
|
|
8
|
+
require_relative '../utils/url_path_builder'
|
|
9
|
+
require_relative '../utils/logger_formatter'
|
|
6
10
|
|
|
7
11
|
module Jekyll
|
|
8
12
|
module L10n
|
|
9
|
-
#
|
|
13
|
+
# Decides whether a localized page needs rebuilding when incremental mode is on.
|
|
10
14
|
#
|
|
11
|
-
#
|
|
12
|
-
#
|
|
13
|
-
#
|
|
15
|
+
# Incremental mode is the plugin's own `jekyll_l10n.incremental` setting, not
|
|
16
|
+
# `jekyll build --incremental`. When it is on, the checker compares the
|
|
17
|
+
# previous output file with the files the page depends on, and asks for a
|
|
18
|
+
# rebuild when any of them is newer: the source page, the page PO file, the
|
|
19
|
+
# locale compendium, or `_config.yml`.
|
|
14
20
|
#
|
|
15
|
-
#
|
|
16
|
-
# -
|
|
17
|
-
# -
|
|
18
|
-
#
|
|
19
|
-
# -
|
|
21
|
+
# Limits to know about:
|
|
22
|
+
# - It compares modification times, not file contents.
|
|
23
|
+
# - It looks for PO files under the directory passed to {#initialize}. The
|
|
24
|
+
# Generator passes nothing, so a custom `locales_dir` is never checked.
|
|
25
|
+
# - The page PO file exists only when `update_compendium` is false; the
|
|
26
|
+
# compendium merge deletes it otherwise.
|
|
20
27
|
#
|
|
21
|
-
#
|
|
22
|
-
# This significantly speeds up rebuilds when only a few files have changed.
|
|
23
|
-
#
|
|
24
|
-
# @example Usage
|
|
28
|
+
# @example
|
|
25
29
|
# checker = RegenerationChecker.new(site)
|
|
26
30
|
# if checker.should_regenerate?(page, 'es')
|
|
27
31
|
# # Create localized_page and add to site.pages
|
|
28
32
|
# end
|
|
29
33
|
#
|
|
30
34
|
class RegenerationChecker
|
|
31
|
-
#
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
#
|
|
36
|
-
# @param site [Jekyll::Site] The Jekyll site object
|
|
37
|
-
# @param locales_dir [String] Directory containing PO files (defaults to
|
|
38
|
-
# DEFAULT_LOCALES_DIR = "_locales")
|
|
39
|
-
def initialize(site, locales_dir = DEFAULT_LOCALES_DIR)
|
|
35
|
+
# @param site [Jekyll::Site] The Jekyll site
|
|
36
|
+
# @param locales_dir [String] Directory, relative to the site source, that
|
|
37
|
+
# holds the PO files. Defaults to Constants::DEFAULT_LOCALES_DIR.
|
|
38
|
+
def initialize(site, locales_dir = Constants::DEFAULT_LOCALES_DIR)
|
|
40
39
|
@site = site
|
|
41
40
|
@locales_dir = locales_dir
|
|
42
41
|
@source = SiteConfigAccessor.source(@site)
|
|
43
42
|
@destination = @site&.config&.dig('destination') || ''
|
|
44
43
|
end
|
|
45
44
|
|
|
46
|
-
#
|
|
47
|
-
#
|
|
48
|
-
# Returns true if:
|
|
49
|
-
# - Incremental builds are disabled (always regenerate)
|
|
50
|
-
# - Output file doesn't exist yet
|
|
51
|
-
# - Source page has been modified since output was generated
|
|
52
|
-
# - PO translation files have been modified since output was generated
|
|
53
|
-
# - Jekyll configuration has been modified since output was generated
|
|
45
|
+
# Returns true when incremental mode is off, when the page has no previous
|
|
46
|
+
# output, or when a file the page depends on is newer than that output.
|
|
54
47
|
#
|
|
55
|
-
#
|
|
56
|
-
#
|
|
57
|
-
# @
|
|
58
|
-
# @param locale [String] The locale code for the variant (e.g., 'es', 'fr')
|
|
59
|
-
# @return [Boolean] true if the page should be regenerated, false if output is current
|
|
48
|
+
# @param page [Jekyll::Page] The source (English) page
|
|
49
|
+
# @param locale [String] Locale code, e.g. 'es' or 'pt_BR'
|
|
50
|
+
# @return [Boolean] true if the localized page must be rebuilt
|
|
60
51
|
def should_regenerate?(page, locale)
|
|
61
52
|
return true unless incremental_enabled?
|
|
62
53
|
return true unless dest_path_exists?(page, locale)
|
|
@@ -68,43 +59,72 @@ module Jekyll
|
|
|
68
59
|
config_modified?(dest_mtime)
|
|
69
60
|
end
|
|
70
61
|
|
|
62
|
+
# Logs a warning when `_config.yml` still has the old `localization_gettext`
|
|
63
|
+
# key. The plugin ignores that key, so without the warning a site would
|
|
64
|
+
# lose incremental mode silently. Call it once per build, not per page.
|
|
65
|
+
#
|
|
66
|
+
# @return [void]
|
|
67
|
+
def warn_legacy_config
|
|
68
|
+
return unless @site&.config.is_a?(Hash) && @site.config.key?('localization_gettext')
|
|
69
|
+
|
|
70
|
+
LoggerFormatter.warn('RegenerationChecker',
|
|
71
|
+
'The `localization_gettext` key in _config.yml is ignored. ' \
|
|
72
|
+
'Set `jekyll_l10n.incremental` instead.')
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
# Output file of a localized page, relative to the site destination. The
|
|
76
|
+
# Generator adds it to `site.keep_files` for skipped pages, so Jekyll's
|
|
77
|
+
# cleanup step does not delete their previous output.
|
|
78
|
+
#
|
|
79
|
+
# This must produce the same path as LocalizedPage#destination.
|
|
80
|
+
#
|
|
81
|
+
# It is the English page's own output path, as Jekyll computes it, under
|
|
82
|
+
# the locale segment, since the localized page's URL is the English URL
|
|
83
|
+
# with that segment in front (LocalizedPage#url).
|
|
84
|
+
#
|
|
85
|
+
# @param page [Jekyll::Page] The source (English) page
|
|
86
|
+
# @param locale [String] Locale code, e.g. 'es' or 'pt_BR'
|
|
87
|
+
# @return [String] Path such as "es/about/index.html" or "es/about.html"
|
|
88
|
+
# (no leading slash)
|
|
89
|
+
def relative_dest_path(page, locale)
|
|
90
|
+
output_path = page.destination(@destination).delete_prefix(@destination)
|
|
91
|
+
"#{LocaleUrlFormatter.to_url_segment(locale)}#{output_path}"
|
|
92
|
+
end
|
|
93
|
+
|
|
71
94
|
private
|
|
72
95
|
|
|
73
96
|
def incremental_enabled?
|
|
74
97
|
return false if @site&.config.nil?
|
|
75
98
|
|
|
76
|
-
|
|
77
|
-
'incremental') ||
|
|
78
|
-
@site.config.dig('localization_gettext', 'incremental') ||
|
|
79
|
-
false
|
|
80
|
-
localization_config == true
|
|
99
|
+
SiteConfigAccessor.site_settings(@site)['incremental'] == true
|
|
81
100
|
end
|
|
82
101
|
|
|
102
|
+
# A directory at the path is the layout of an earlier release
|
|
103
|
+
# (es/nav.html/index.html), so the page is written again.
|
|
83
104
|
def dest_path_exists?(page, locale)
|
|
84
|
-
File.
|
|
105
|
+
File.file?(dest_path(page, locale))
|
|
85
106
|
end
|
|
86
107
|
|
|
87
108
|
def dest_path(page, locale)
|
|
88
|
-
#
|
|
89
|
-
# Uses BCP 47 hyphenated locale segment to match LocalizedPage#destination output.
|
|
90
|
-
url_locale = LocaleUrlFormatter.to_url_segment(locale)
|
|
91
|
-
url = "/#{url_locale}#{page.url.chomp('/')}/"
|
|
92
|
-
"#{@destination}#{url}index.html"
|
|
109
|
+
"#{@destination}/#{relative_dest_path(page, locale)}"
|
|
93
110
|
end
|
|
94
111
|
|
|
95
112
|
def source_modified?(page, dest_mtime)
|
|
96
|
-
|
|
113
|
+
# Jekyll 4 gives Page#path relative to the site source. Resolve it there,
|
|
114
|
+
# not against the current directory. An absolute path stays unchanged.
|
|
115
|
+
page_mtime = File.mtime(File.expand_path(page.path, @source))
|
|
97
116
|
page_mtime > dest_mtime
|
|
98
117
|
rescue StandardError
|
|
99
118
|
true
|
|
100
119
|
end
|
|
101
120
|
|
|
102
121
|
def po_files_modified?(page, locale, dest_mtime)
|
|
103
|
-
#
|
|
104
|
-
|
|
122
|
+
# Use the same page PO path PageTranslationLoader reads, which comes
|
|
123
|
+
# from the page URL, not from the source file name.
|
|
124
|
+
po_page_path = UrlPathBuilder.url_to_po_page_path(page.url)
|
|
125
|
+
page_po_path = PoPathBuilder.build(@source, @locales_dir, locale, po_page_path)
|
|
105
126
|
return true if file_modified?(page_po_path, dest_mtime)
|
|
106
127
|
|
|
107
|
-
# Check compendium PO file
|
|
108
128
|
compendium_po_path = PoPathBuilder.build(@source, @locales_dir, locale, nil)
|
|
109
129
|
file_modified?(compendium_po_path, dest_mtime)
|
|
110
130
|
end
|
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require 'liquid'
|
|
4
|
+
require_relative '../configuration'
|
|
4
5
|
require_relative '../constants'
|
|
5
6
|
require_relative '../utils/error_handler'
|
|
6
7
|
require_relative '../utils/locale_name_resolver'
|
|
7
8
|
require_relative '../utils/locale_url_formatter'
|
|
9
|
+
require_relative '../utils/site_config_accessor'
|
|
8
10
|
|
|
9
11
|
module Jekyll
|
|
10
12
|
module L10n
|
|
@@ -26,7 +28,7 @@ module Jekyll
|
|
|
26
28
|
# @example Usage in Jekyll Liquid templates
|
|
27
29
|
# <!-- Add locale prefix to URL -->
|
|
28
30
|
# <a href="{{ '/about/' | locale_url: 'es_ES' }}">Acerca de</a>
|
|
29
|
-
# <!-- Produces: /es-
|
|
31
|
+
# <!-- Produces: /es-es/about/ -->
|
|
30
32
|
#
|
|
31
33
|
# <!-- Switch to different locale variant -->
|
|
32
34
|
# <a href="{{ page.url | switch_locale_url: 'fr' }}">Français</a>
|
|
@@ -40,7 +42,7 @@ module Jekyll
|
|
|
40
42
|
# Generate a locale-prefixed URL for the given URL and locale
|
|
41
43
|
#
|
|
42
44
|
# Adds a locale prefix in BCP 47 hyphenated form to the given URL. For example,
|
|
43
|
-
# '/about/' becomes '/es-
|
|
45
|
+
# '/about/' becomes '/es-es/about/' for locale 'es_ES'. The filter handles
|
|
44
46
|
# various edge cases and only applies the prefix when appropriate.
|
|
45
47
|
#
|
|
46
48
|
# The method performs several checks to determine if prefixing is appropriate:
|
|
@@ -55,7 +57,7 @@ module Jekyll
|
|
|
55
57
|
# prefixing is not appropriate
|
|
56
58
|
# @example
|
|
57
59
|
# <!-- Explicit underscore locale converted to hyphen in URL -->
|
|
58
|
-
# {{ '/about/' | locale_url: 'es_ES' }} # => '/es-
|
|
60
|
+
# {{ '/about/' | locale_url: 'es_ES' }} # => '/es-es/about/'
|
|
59
61
|
#
|
|
60
62
|
# <!-- Current page's locale -->
|
|
61
63
|
# {{ '/about/' | locale_url }} # => '/es/about/' (if page locale is 'es')
|
|
@@ -70,7 +72,7 @@ module Jekyll
|
|
|
70
72
|
# Generate a URL for the given locale variant of the current page
|
|
71
73
|
#
|
|
72
74
|
# Switches the current URL to a different locale variant. For example, if the current
|
|
73
|
-
# page is '/es-
|
|
75
|
+
# page is '/es-es/about/', calling this with 'fr' returns '/fr/about/'. This is useful
|
|
74
76
|
# for building language switcher menus.
|
|
75
77
|
#
|
|
76
78
|
# This filter first extracts the base URL (without locale prefix) from the given URL,
|
|
@@ -80,7 +82,7 @@ module Jekyll
|
|
|
80
82
|
# @param target_locale [String] The target locale code (e.g., 'es_ES', 'fr', 'pt_BR')
|
|
81
83
|
# @return [String] The URL for the target locale, or original URL if locale is invalid
|
|
82
84
|
# @example
|
|
83
|
-
# <!-- Current page is /es-
|
|
85
|
+
# <!-- Current page is /es-es/about/, switch to French -->
|
|
84
86
|
# {{ page.url | switch_locale_url: 'fr' }} # => '/fr/about/'
|
|
85
87
|
#
|
|
86
88
|
# <!-- Build a language switcher -->
|
|
@@ -116,7 +118,7 @@ module Jekyll
|
|
|
116
118
|
# @example
|
|
117
119
|
# {{ page.url | locale_menu }}
|
|
118
120
|
# # => [{"code"=>"en","name"=>"English","url"=>"/about/","current"=>false},
|
|
119
|
-
# # {"code"=>"es_ES","name"=>"Spanish (Spain)","url"=>"/es-
|
|
121
|
+
# # {"code"=>"es_ES","name"=>"Spanish (Spain)","url"=>"/es-es/about/","current"=>true}]
|
|
120
122
|
def locale_menu(url)
|
|
121
123
|
menu_locales.map do |locale|
|
|
122
124
|
{
|
|
@@ -192,29 +194,27 @@ module Jekyll
|
|
|
192
194
|
return false if locale.nil? || locale.empty?
|
|
193
195
|
return true if locale == 'en' # English is always valid
|
|
194
196
|
|
|
195
|
-
# Get configured locales from page or site config
|
|
196
197
|
locales_config = configured_locales
|
|
197
198
|
|
|
198
|
-
#
|
|
199
|
-
# before comparing, so 'es-ES' matches 'es_ES' and vice versa.
|
|
199
|
+
# Compare URL forms so 'es-ES' matches 'es_ES' and the other way round.
|
|
200
200
|
url_locale = LocaleUrlFormatter.to_url_segment(locale)
|
|
201
201
|
locales_config.any? { |l| LocaleUrlFormatter.to_url_segment(l) == url_locale }
|
|
202
202
|
end
|
|
203
203
|
|
|
204
|
+
# Locales of the page being rendered. A Hash comes from Liquid and already
|
|
205
|
+
# has the site defaults merged in; a page object needs the same merge.
|
|
204
206
|
def configured_locales
|
|
205
207
|
page = @context.registers[:page]
|
|
206
208
|
|
|
207
|
-
# Try to get from page data first (most specific)
|
|
208
209
|
if page
|
|
209
210
|
locales = if page.is_a?(Hash)
|
|
210
|
-
|
|
211
|
+
Configuration::Schema.normalize(page[Configuration::PAGE_DATA_KEY], source: nil)['locales']
|
|
211
212
|
else
|
|
212
|
-
|
|
213
|
+
SiteConfigAccessor.page_locales_data(page)['locales']
|
|
213
214
|
end
|
|
214
215
|
return locales if locales && !locales.empty?
|
|
215
216
|
end
|
|
216
217
|
|
|
217
|
-
# Fallback to empty array if not configured
|
|
218
218
|
[]
|
|
219
219
|
end
|
|
220
220
|
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Jekyll
|
|
4
|
+
module L10n
|
|
5
|
+
# Parses the text of a GNU Gettext PO file into a translation hash.
|
|
6
|
+
#
|
|
7
|
+
# Each entry is read in three steps, in this order:
|
|
8
|
+
# 1. The comment lines above `msgid`: reference (`#:`), fuzzy flag (`#,`)
|
|
9
|
+
# and previous msgid (`#|`).
|
|
10
|
+
# 2. The `msgid` and `msgstr` fields, each with its continuation lines.
|
|
11
|
+
# 3. The stored value, whose shape depends on the mode.
|
|
12
|
+
#
|
|
13
|
+
# Modes and the value stored for each msgid:
|
|
14
|
+
# * :simple — the msgstr String
|
|
15
|
+
# * :references — { msgstr:, reference: } (reference only when present)
|
|
16
|
+
# * :merge — { msgstr:, reference:, fuzzy:, previous_msgid: }
|
|
17
|
+
#
|
|
18
|
+
# Entries with an empty msgid (the header) are skipped.
|
|
19
|
+
#
|
|
20
|
+
# @example
|
|
21
|
+
# PoEntryParser.parse(%(msgid "Hello"\nmsgstr "Hola"\n), :simple)
|
|
22
|
+
# # => { "Hello" => "Hola" }
|
|
23
|
+
class PoEntryParser
|
|
24
|
+
MODES = %i[simple references merge].freeze
|
|
25
|
+
MSGID_PATTERN = /^msgid ['"](.*)['"] *$/.freeze
|
|
26
|
+
MSGSTR_PATTERN = /^msgstr ['"](.*)['"] *$/.freeze
|
|
27
|
+
|
|
28
|
+
# @param content [String] PO file text
|
|
29
|
+
# @param mode [Symbol] One of MODES
|
|
30
|
+
# @return [Hash] Translations keyed by msgid
|
|
31
|
+
def self.parse(content, mode)
|
|
32
|
+
raise ArgumentError, "unknown PO parse mode: #{mode.inspect}" unless MODES.include?(mode)
|
|
33
|
+
|
|
34
|
+
new(content.to_s.split("\n"), mode).parse
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def initialize(lines, mode)
|
|
38
|
+
@lines = lines
|
|
39
|
+
@mode = mode
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# @return [Hash] Translations keyed by msgid
|
|
43
|
+
def parse
|
|
44
|
+
translations = {}
|
|
45
|
+
idx = 0
|
|
46
|
+
idx = parse_line(idx, translations) while idx < @lines.length
|
|
47
|
+
translations
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
private
|
|
51
|
+
|
|
52
|
+
# Parses the entry that starts at idx, if any, and returns the next line
|
|
53
|
+
# to read.
|
|
54
|
+
def parse_line(idx, translations)
|
|
55
|
+
return idx + 1 unless @lines[idx].strip.start_with?('msgid ')
|
|
56
|
+
|
|
57
|
+
metadata = @mode == :simple ? {} : read_comments(idx)
|
|
58
|
+
parse_entry(idx, translations, metadata)
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# --- Step 1: comment lines above msgid ---
|
|
62
|
+
|
|
63
|
+
# Reads the comment and blank lines above msgid_idx, nearest first. When a
|
|
64
|
+
# kind of comment appears more than once, the topmost one wins.
|
|
65
|
+
def read_comments(msgid_idx)
|
|
66
|
+
metadata = { reference: nil, fuzzy: false, previous_msgid: nil }
|
|
67
|
+
(msgid_idx - 1).downto(0) do |idx|
|
|
68
|
+
line = @lines[idx].strip
|
|
69
|
+
break unless line.empty? || line.start_with?('#')
|
|
70
|
+
|
|
71
|
+
read_comment(line, metadata)
|
|
72
|
+
end
|
|
73
|
+
@mode == :merge ? metadata : { reference: metadata[:reference] }
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def read_comment(line, metadata)
|
|
77
|
+
if line.start_with?('#:')
|
|
78
|
+
metadata[:reference] = line.sub(/^#:\s*/, '').strip
|
|
79
|
+
elsif line.start_with?('#,') && line.include?('fuzzy')
|
|
80
|
+
metadata[:fuzzy] = true
|
|
81
|
+
elsif line.start_with?('#|') && line.include?('msgid')
|
|
82
|
+
metadata[:previous_msgid] = previous_msgid(line)
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def previous_msgid(line)
|
|
87
|
+
raw = line.sub(/^#\|\s*msgid\s*/, '').strip
|
|
88
|
+
quote = raw[0]
|
|
89
|
+
return raw unless ['"', "'"].include?(quote) && raw.end_with?(quote)
|
|
90
|
+
|
|
91
|
+
unescape(raw[1...-1], quote)
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
# --- Step 2: msgid and msgstr fields ---
|
|
95
|
+
|
|
96
|
+
# Reads msgid and the msgstr right after it. A msgid with no msgstr on the
|
|
97
|
+
# next line is dropped, and that next line is skipped too.
|
|
98
|
+
def parse_entry(idx, translations, metadata)
|
|
99
|
+
msgid, idx = read_field(idx, MSGID_PATTERN)
|
|
100
|
+
return idx + 1 unless idx < @lines.length && @lines[idx].strip.start_with?('msgstr ')
|
|
101
|
+
|
|
102
|
+
msgstr, idx = read_field(idx, MSGSTR_PATTERN)
|
|
103
|
+
store(translations, msgid, msgstr, metadata)
|
|
104
|
+
idx
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
# @return [Array(String, Integer)] The field's value and the next line
|
|
108
|
+
def read_field(idx, pattern)
|
|
109
|
+
line = @lines[idx].strip
|
|
110
|
+
match = line.match(pattern)
|
|
111
|
+
quote = match ? line[match.begin(1) - 1] : '"'
|
|
112
|
+
values = match ? [unescape(match[1], quote)] : []
|
|
113
|
+
read_continuations(idx + 1, values, quote)
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
# Appends the quoted lines that follow a field to values.
|
|
117
|
+
#
|
|
118
|
+
# @return [Array(String, Integer)] The joined value and the next line
|
|
119
|
+
def read_continuations(idx, values, quote)
|
|
120
|
+
while idx < @lines.length && continuation?(@lines[idx].strip)
|
|
121
|
+
values << unescape(@lines[idx].strip[1...-1], quote)
|
|
122
|
+
idx += 1
|
|
123
|
+
end
|
|
124
|
+
[values.join, idx]
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
def continuation?(line)
|
|
128
|
+
return false if line.empty? || line.start_with?('#', 'msgid', 'msgstr')
|
|
129
|
+
|
|
130
|
+
(line.start_with?('"') && line.end_with?('"')) ||
|
|
131
|
+
(line.start_with?("'") && line.end_with?("'"))
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
# Unescapes a quoted PO value. Escaped quotes are replaced before escaped
|
|
135
|
+
# backslashes: the other order would read `\\"` as an escaped quote.
|
|
136
|
+
def unescape(str, quote)
|
|
137
|
+
str.gsub("\\#{quote}", quote).gsub('\\\\', '\\')
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
# --- Step 3: the stored value ---
|
|
141
|
+
|
|
142
|
+
def store(translations, msgid, msgstr, metadata)
|
|
143
|
+
return if msgid.empty?
|
|
144
|
+
|
|
145
|
+
translations[msgid] = @mode == :simple ? msgstr : entry(msgstr, metadata)
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
def entry(msgstr, metadata)
|
|
149
|
+
entry = { msgstr: msgstr }.merge(metadata.compact)
|
|
150
|
+
entry[:comment] = nil if @mode == :merge || metadata[:reference]
|
|
151
|
+
entry
|
|
152
|
+
end
|
|
153
|
+
end
|
|
154
|
+
end
|
|
155
|
+
end
|