Skip to content

Add localized hash style that returns structured data with locale-specific keys#47

Merged
markets merged 9 commits intomasterfrom
copilot/fix-7e6cc5a9-b6c2-4b56-b073-d21d66a8c40b
Aug 12, 2025
Merged

Add localized hash style that returns structured data with locale-specific keys#47
markets merged 9 commits intomasterfrom
copilot/fix-7e6cc5a9-b6c2-4b56-b073-d21d66a8c40b

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Aug 11, 2025

This PR implements a new "hash" style option that returns structured hash data with localized keys, addressing the feature request to provide programmatic access to time components while maintaining locale support.

What's Changed

The :hash style now returns time components as a hash with localized symbolic keys and numeric values:

# English locale (default)
timeago(Date.today.prev_day(425), style: :hash)
# => {:years=>1, :months=>4}

# Spanish locale - keys are localized
timeago(Date.today.prev_day(425), style: :hash, locale: :es)
# => {:años=>1, :meses=>4}

# French locale
timeago(Date.today.prev_day(425), style: :hash, locale: :fr)
# => {:années=>1, :mois=>4}

Key Features

  • Localized Keys: Hash keys are automatically translated based on the locale setting
  • Full Option Support: Works with all existing options including depth, threshold, and only
  • Special Cases: Both array and hash styles now properly handle today/yesterday/tomorrow scenarios:
    • Array style: ["today"], ["yesterday"], ["tomorrow"]
    • Hash style: {días: 0} for today, {días: 1} for yesterday/tomorrow (localized)
  • CLI Integration: Available via command line with --style hash option and locale support
  • Backward Compatible: No changes to existing functionality
  • Optimized Performance: Reuses existing methods to eliminate code duplication
  • DRY Implementation: Special cases unified for both array and hash styles using shared helper methods

Examples

# Basic usage with localization
timeago('2020-1-1', '2021-3-15', style: :hash, locale: :es)
# => {:años=>1, :meses=>2}

# With depth control
timeago('2020-1-1', '2021-3-15', style: :hash, depth: 1, locale: :fr)
# => {:années=>1}

# With "only" option
timeago('2020-1-1', '2021-1-1', style: :hash, only: :months, locale: :es)
# => {:meses=>12}

# Special cases maintain localization for both styles
timeago(Date.today, style: :hash, locale: :es)
# => {:días=>0}

timeago(Date.today, style: :array, locale: :es)
# => ["hoy"]

CLI Usage

$ timeago 2020-1-1 2021-3-15 --style hash --locale es
{:años=>1, :meses=>2}

$ timeago 2020-1-1 2021-1-1 --style hash --only months --locale fr
{:mois=>12}

This enhancement enables developers to easily access individual time components for calculations, formatting, or data processing with full internationalization support, eliminating the need to parse formatted strings.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI self-assigned this Aug 11, 2025
Copilot AI and others added 2 commits August 11, 2025 22:46
Co-authored-by: markets <576701+markets@users.noreply.github.com>
Co-authored-by: markets <576701+markets@users.noreply.github.com>
Copilot AI changed the title [WIP] add new "hash" style, which returns structured hash data, ie: { years: 1, months: 2} Add new "hash" style that returns structured hash data Aug 11, 2025
Copilot AI requested a review from markets August 11, 2025 22:49
@markets
Copy link
Copy Markdown
Owner

markets commented Aug 11, 2025

@copilot Not bad, but:

  • I want to keep the locales working: {años: 2, semanas: 1}
  • I think we can refactor (reuse) current methods, like build_time_ago_slots and build_only_slots to avoid creating a new method

Co-authored-by: markets <576701+markets@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Aug 11, 2025

@copilot Not bad, but:

  • I want to keep the locales working: {años: 2, semanas: 1}
  • I think we can refactor (reuse) current methods, like build_time_ago_slots and build_only_slots to avoid cre...

Implemented both requested changes in commit 05a2c5b:

Localized hash keys: Hash style now returns keys in the target language:

timeago('2020-1-1', '2021-3-15', style: :hash, locale: :es)
# => {:años=>1, :meses=>2}

Refactored to reuse existing methods: Removed the duplicate build_hash_output method and modified build_time_ago_slots and build_only_slots to support both string and hash output formats. This eliminates code duplication while maintaining all functionality.

The hash style now works seamlessly with all existing options (depth, threshold, only) and provides localized keys for all supported languages.

Copilot AI changed the title Add new "hash" style that returns structured hash data Add localized hash style that returns structured data with locale-specific keys Aug 11, 2025
Comment thread lib/jekyll-timeago/core.rb Outdated
…tion

Co-authored-by: markets <576701+markets@users.noreply.github.com>
Copilot AI requested a review from markets August 11, 2025 23:40
Comment thread lib/jekyll-timeago/core.rb Outdated
…Y code

Co-authored-by: markets <576701+markets@users.noreply.github.com>
Copilot AI requested a review from markets August 11, 2025 23:53
@markets markets changed the title Add localized hash style that returns structured data with locale-specific keys Add localized hash style that returns structured data with locale-specific keys Aug 12, 2025
@markets markets marked this pull request as ready for review August 12, 2025 00:18
@markets markets merged commit fc4bd10 into master Aug 12, 2025
12 checks passed
@markets markets deleted the copilot/fix-7e6cc5a9-b6c2-4b56-b073-d21d66a8c40b branch August 12, 2025 13:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants