Skip to content
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

Implemented language support for humanize. #70

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

Dogacel
Copy link

@Dogacel Dogacel commented Jun 16, 2018

Humanize now has language support on function func Time(then time.Time) string and func Ordinal(x int) string. The language files are located on /locals folder.

Local files

JSON files are pretty easy to understand. I've written the English version and the Turkish version. Magnitudes and Indicators are pretty easy to understand.

Ordinals

The hardest part to understand is the ordinals array of arrays. Ordinals array first contains a rule. This rule's first array string starts with % character. After the % character, a number or . (dot) is placed. Dot means the rule applies for all numbers' ordinals. If a number is given, there must be another positive number smaller than first number after the rule identifier separated by a whitespace.

Ex: %10 5

Note: rule %. must be placed on very top of the file without any extension rules.

This is means, apply rule for all numbers which is equal to 5 modulo 10.. The second string on the
array must be the ordinal suffix.

Ex: first = 1st therefore ["%10 1", "st"] is a correct rule.

But we know that also 11 = 1 mod 10 so the rules can be overwritten. If a rule matches the number, output is not returned. Parser continues to check other rules.

Ex: "ordinals": [ ["%10 1", "st"], ["%100 10", "th"]]

This ruleset first thinks ordinal for 11 must be st. After that it checks the second rule and overwrites st by th. Finally the extension rules are introduced. An extension rule is the boolean OR of the rules. Rule parser does not contain any AND or NOT gates because I did not know any other language that uses ordinals in format [number][ordinal suffix]. If they exist, please make an issue.

Ex: "ordinals": [["%10 4 %10 5 %10 6", "th"]]

This is equal to saying [["%10 4", "th"], ["%10 5", "th"], ["%10 6", "th"]] in a more organized way.

@Dogacel
Copy link
Author

Dogacel commented Sep 23, 2022

@dustin @flexd @AlekSi @annismckenzie Bump, any opinions?

@dolmen
Copy link

dolmen commented Mar 20, 2024

If you need i18n, use golang.org/x/text/message:

package main

import (
    "golang.org/x/text/language"
    "golang.org/x/text/message"
)

func main() {
    p := message.NewPrinter(language.English)
    p.Printf("%d\n", 1000)

    // Output:
    // 1,000
}

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