Skip to content

Instantly share code, notes, and snippets.

@kinngh
Created December 11, 2024 12:29
Show Gist options
  • Save kinngh/c4d1a3ae17dc9b1aff9ad84fad9a5f55 to your computer and use it in GitHub Desktop.
Save kinngh/c4d1a3ae17dc9b1aff9ad84fad9a5f55 to your computer and use it in GitHub Desktop.
Convert given measurement into current locale's unit
{% comment %}
Measurement translation based on locale
- US/UK/Myanmar: Imperial (inches, feet)
- Rest of world: Metric (cm, m)
{% endcomment %}
{% assign current_measurement = "25 inches" %}
{% assign measurement = current_measurement | downcase | strip %}
{% assign locale = request.locale.iso_code | downcase %}
{% comment %} Define imperial system locales {% endcomment %}
{% assign imperial_locales = "en-us,en-gb,my-mm" | split: ',' %}
{% assign uses_imperial = false %}
{% for imperial_locale in imperial_locales %}
{% if locale contains imperial_locale %}
{% assign uses_imperial = true %}
{% break %}
{% endif %}
{% endfor %}
{% comment %} Extract numeric value and unit {% endcomment %}
{% assign numeric_value = measurement | replace: 'inches', '' | replace: 'inch', '' | replace: 'feet', '' | replace: 'foot', '' | replace: 'meters', '' | replace: 'meter', '' | replace: 'centimeters', '' | replace: 'centimeter', '' | replace: 'ft', '' | replace: 'in', '' | replace: 'm', '' | replace: 'cm', '' | strip | split: ' ' | first %}
{% assign unit = measurement | replace: numeric_value, '' | strip %}
{% comment %} Handle feet and inches format {% endcomment %}
{% if measurement contains 'ft' and measurement contains 'in' %}
{% assign feet_inches = measurement | split: ' ' %}
{% assign feet = feet_inches[0] | replace: 'ft', '' | times: 1 %}
{% assign inches = feet_inches[2] | replace: 'in', '' | times: 1 %}
{% assign total_inches = feet | times: 12 | plus: inches %}
{% if uses_imperial %}
{% comment %} Keep in imperial but format consistently {% endcomment %}
{% capture converted %}{{ feet }}ft {{ inches }}in{% endcapture %}
{% else %}
{% comment %} Convert to metric {% endcomment %}
{% assign cm_value = total_inches | times: 2.54 %}
{% assign m_value = cm_value | divided_by: 100.0 %}
{% if cm_value >= 100 %}
{% capture converted %}{{ m_value | round: 2 }}m{% endcapture %}
{% else %}
{% capture converted %}{{ cm_value | round: 1 }}cm{% endcapture %}
{% endif %}
{% endif %}
{% else %}
{% case unit %}
{% when 'in' or 'inch' or 'inches' %}
{% if uses_imperial %}
{% comment %} Keep in inches but format consistently {% endcomment %}
{% if numeric_value >= 12 %}
{% assign feet = numeric_value | divided_by: 12 | floor %}
{% assign remaining_inches = numeric_value | modulo: 12 %}
{% capture converted %}{{ feet }}ft {{ remaining_inches }}in{% endcapture %}
{% else %}
{% capture converted %}{{ numeric_value }}in{% endcapture %}
{% endif %}
{% else %}
{% comment %} Convert to metric {% endcomment %}
{% assign cm_value = numeric_value | times: 2.54 %}
{% if cm_value >= 100 %}
{% assign m_value = cm_value | divided_by: 100.0 %}
{% capture converted %}{{ m_value | round: 2 }}m{% endcapture %}
{% else %}
{% capture converted %}{{ cm_value | round: 1 }}cm{% endcapture %}
{% endif %}
{% endif %}
{% when 'cm' or 'centimeter' or 'centimeters' %}
{% if uses_imperial %}
{% comment %} Convert to imperial {% endcomment %}
{% assign inch_value = numeric_value | divided_by: 2.54 %}
{% if inch_value >= 12 %}
{% assign feet = inch_value | divided_by: 12 | floor %}
{% assign remaining_inches = inch_value | modulo: 12 | round %}
{% capture converted %}{{ feet }}ft {{ remaining_inches }}in{% endcapture %}
{% else %}
{% capture converted %}{{ inch_value | round: 1 }}in{% endcapture %}
{% endif %}
{% else %}
{% comment %} Keep in cm but format consistently {% endcomment %}
{% if numeric_value >= 100 %}
{% assign m_value = numeric_value | divided_by: 100.0 %}
{% capture converted %}{{ m_value | round: 2 }}m{% endcapture %}
{% else %}
{% capture converted %}{{ numeric_value }}cm{% endcapture %}
{% endif %}
{% endif %}
{% when 'm' or 'meter' or 'meters' %}
{% assign cm_value = numeric_value | times: 100 %}
{% if uses_imperial %}
{% comment %} Convert to imperial {% endcomment %}
{% assign inch_value = cm_value | divided_by: 2.54 %}
{% if inch_value >= 12 %}
{% assign feet = inch_value | divided_by: 12 | floor %}
{% assign remaining_inches = inch_value | modulo: 12 | round %}
{% capture converted %}{{ feet }}ft {{ remaining_inches }}in{% endcapture %}
{% else %}
{% capture converted %}{{ inch_value | round: 1 }}in{% endcapture %}
{% endif %}
{% else %}
{% comment %} Keep in meters but format consistently {% endcomment %}
{% capture converted %}{{ numeric_value }}m{% endcapture %}
{% endif %}
{% else %}
{% capture converted %}Invalid unit{% endcapture %}
{% endcase %}
{% endif %}
{{ converted }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment